bpf_load.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. #include <libelf.h>
  6. #include <gelf.h>
  7. #include <errno.h>
  8. #include <unistd.h>
  9. #include <string.h>
  10. #include <stdbool.h>
  11. #include <stdlib.h>
  12. #include <linux/bpf.h>
  13. #include <linux/filter.h>
  14. #include <linux/perf_event.h>
  15. #include <sys/syscall.h>
  16. #include <sys/ioctl.h>
  17. #include <sys/mman.h>
  18. #include <poll.h>
  19. #include <ctype.h>
  20. #include "libbpf.h"
  21. #include "bpf_helpers.h"
  22. #include "bpf_load.h"
  23. #define DEBUGFS "/sys/kernel/debug/tracing/"
  24. static char license[128];
  25. static int kern_version;
  26. static bool processed_sec[128];
  27. int map_fd[MAX_MAPS];
  28. int prog_fd[MAX_PROGS];
  29. int event_fd[MAX_PROGS];
  30. int prog_cnt;
  31. int prog_array_fd = -1;
  32. static int populate_prog_array(const char *event, int prog_fd)
  33. {
  34. int ind = atoi(event), err;
  35. err = bpf_update_elem(prog_array_fd, &ind, &prog_fd, BPF_ANY);
  36. if (err < 0) {
  37. printf("failed to store prog_fd in prog_array\n");
  38. return -1;
  39. }
  40. return 0;
  41. }
  42. static int load_and_attach(const char *event, struct bpf_insn *prog, int size)
  43. {
  44. bool is_socket = strncmp(event, "socket", 6) == 0;
  45. bool is_kprobe = strncmp(event, "kprobe/", 7) == 0;
  46. bool is_kretprobe = strncmp(event, "kretprobe/", 10) == 0;
  47. bool is_tracepoint = strncmp(event, "tracepoint/", 11) == 0;
  48. bool is_xdp = strncmp(event, "xdp", 3) == 0;
  49. bool is_perf_event = strncmp(event, "perf_event", 10) == 0;
  50. enum bpf_prog_type prog_type;
  51. char buf[256];
  52. int fd, efd, err, id;
  53. struct perf_event_attr attr = {};
  54. attr.type = PERF_TYPE_TRACEPOINT;
  55. attr.sample_type = PERF_SAMPLE_RAW;
  56. attr.sample_period = 1;
  57. attr.wakeup_events = 1;
  58. if (is_socket) {
  59. prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
  60. } else if (is_kprobe || is_kretprobe) {
  61. prog_type = BPF_PROG_TYPE_KPROBE;
  62. } else if (is_tracepoint) {
  63. prog_type = BPF_PROG_TYPE_TRACEPOINT;
  64. } else if (is_xdp) {
  65. prog_type = BPF_PROG_TYPE_XDP;
  66. } else if (is_perf_event) {
  67. prog_type = BPF_PROG_TYPE_PERF_EVENT;
  68. } else {
  69. printf("Unknown event '%s'\n", event);
  70. return -1;
  71. }
  72. fd = bpf_prog_load(prog_type, prog, size, license, kern_version);
  73. if (fd < 0) {
  74. printf("bpf_prog_load() err=%d\n%s", errno, bpf_log_buf);
  75. return -1;
  76. }
  77. prog_fd[prog_cnt++] = fd;
  78. if (is_xdp || is_perf_event)
  79. return 0;
  80. if (is_socket) {
  81. event += 6;
  82. if (*event != '/')
  83. return 0;
  84. event++;
  85. if (!isdigit(*event)) {
  86. printf("invalid prog number\n");
  87. return -1;
  88. }
  89. return populate_prog_array(event, fd);
  90. }
  91. if (is_kprobe || is_kretprobe) {
  92. if (is_kprobe)
  93. event += 7;
  94. else
  95. event += 10;
  96. if (*event == 0) {
  97. printf("event name cannot be empty\n");
  98. return -1;
  99. }
  100. if (isdigit(*event))
  101. return populate_prog_array(event, fd);
  102. snprintf(buf, sizeof(buf),
  103. "echo '%c:%s %s' >> /sys/kernel/debug/tracing/kprobe_events",
  104. is_kprobe ? 'p' : 'r', event, event);
  105. err = system(buf);
  106. if (err < 0) {
  107. printf("failed to create kprobe '%s' error '%s'\n",
  108. event, strerror(errno));
  109. return -1;
  110. }
  111. strcpy(buf, DEBUGFS);
  112. strcat(buf, "events/kprobes/");
  113. strcat(buf, event);
  114. strcat(buf, "/id");
  115. } else if (is_tracepoint) {
  116. event += 11;
  117. if (*event == 0) {
  118. printf("event name cannot be empty\n");
  119. return -1;
  120. }
  121. strcpy(buf, DEBUGFS);
  122. strcat(buf, "events/");
  123. strcat(buf, event);
  124. strcat(buf, "/id");
  125. }
  126. efd = open(buf, O_RDONLY, 0);
  127. if (efd < 0) {
  128. printf("failed to open event %s\n", event);
  129. return -1;
  130. }
  131. err = read(efd, buf, sizeof(buf));
  132. if (err < 0 || err >= sizeof(buf)) {
  133. printf("read from '%s' failed '%s'\n", event, strerror(errno));
  134. return -1;
  135. }
  136. close(efd);
  137. buf[err] = 0;
  138. id = atoi(buf);
  139. attr.config = id;
  140. efd = perf_event_open(&attr, -1/*pid*/, 0/*cpu*/, -1/*group_fd*/, 0);
  141. if (efd < 0) {
  142. printf("event %d fd %d err %s\n", id, efd, strerror(errno));
  143. return -1;
  144. }
  145. event_fd[prog_cnt - 1] = efd;
  146. ioctl(efd, PERF_EVENT_IOC_ENABLE, 0);
  147. ioctl(efd, PERF_EVENT_IOC_SET_BPF, fd);
  148. return 0;
  149. }
  150. static int load_maps(struct bpf_map_def *maps, int len)
  151. {
  152. int i;
  153. for (i = 0; i < len / sizeof(struct bpf_map_def); i++) {
  154. map_fd[i] = bpf_create_map(maps[i].type,
  155. maps[i].key_size,
  156. maps[i].value_size,
  157. maps[i].max_entries,
  158. maps[i].map_flags);
  159. if (map_fd[i] < 0) {
  160. printf("failed to create a map: %d %s\n",
  161. errno, strerror(errno));
  162. return 1;
  163. }
  164. if (maps[i].type == BPF_MAP_TYPE_PROG_ARRAY)
  165. prog_array_fd = map_fd[i];
  166. }
  167. return 0;
  168. }
  169. static int get_sec(Elf *elf, int i, GElf_Ehdr *ehdr, char **shname,
  170. GElf_Shdr *shdr, Elf_Data **data)
  171. {
  172. Elf_Scn *scn;
  173. scn = elf_getscn(elf, i);
  174. if (!scn)
  175. return 1;
  176. if (gelf_getshdr(scn, shdr) != shdr)
  177. return 2;
  178. *shname = elf_strptr(elf, ehdr->e_shstrndx, shdr->sh_name);
  179. if (!*shname || !shdr->sh_size)
  180. return 3;
  181. *data = elf_getdata(scn, 0);
  182. if (!*data || elf_getdata(scn, *data) != NULL)
  183. return 4;
  184. return 0;
  185. }
  186. static int parse_relo_and_apply(Elf_Data *data, Elf_Data *symbols,
  187. GElf_Shdr *shdr, struct bpf_insn *insn)
  188. {
  189. int i, nrels;
  190. nrels = shdr->sh_size / shdr->sh_entsize;
  191. for (i = 0; i < nrels; i++) {
  192. GElf_Sym sym;
  193. GElf_Rel rel;
  194. unsigned int insn_idx;
  195. gelf_getrel(data, i, &rel);
  196. insn_idx = rel.r_offset / sizeof(struct bpf_insn);
  197. gelf_getsym(symbols, GELF_R_SYM(rel.r_info), &sym);
  198. if (insn[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) {
  199. printf("invalid relo for insn[%d].code 0x%x\n",
  200. insn_idx, insn[insn_idx].code);
  201. return 1;
  202. }
  203. insn[insn_idx].src_reg = BPF_PSEUDO_MAP_FD;
  204. insn[insn_idx].imm = map_fd[sym.st_value / sizeof(struct bpf_map_def)];
  205. }
  206. return 0;
  207. }
  208. int load_bpf_file(char *path)
  209. {
  210. int fd, i;
  211. Elf *elf;
  212. GElf_Ehdr ehdr;
  213. GElf_Shdr shdr, shdr_prog;
  214. Elf_Data *data, *data_prog, *symbols = NULL;
  215. char *shname, *shname_prog;
  216. if (elf_version(EV_CURRENT) == EV_NONE)
  217. return 1;
  218. fd = open(path, O_RDONLY, 0);
  219. if (fd < 0)
  220. return 1;
  221. elf = elf_begin(fd, ELF_C_READ, NULL);
  222. if (!elf)
  223. return 1;
  224. if (gelf_getehdr(elf, &ehdr) != &ehdr)
  225. return 1;
  226. /* clear all kprobes */
  227. i = system("echo \"\" > /sys/kernel/debug/tracing/kprobe_events");
  228. /* scan over all elf sections to get license and map info */
  229. for (i = 1; i < ehdr.e_shnum; i++) {
  230. if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
  231. continue;
  232. if (0) /* helpful for llvm debugging */
  233. printf("section %d:%s data %p size %zd link %d flags %d\n",
  234. i, shname, data->d_buf, data->d_size,
  235. shdr.sh_link, (int) shdr.sh_flags);
  236. if (strcmp(shname, "license") == 0) {
  237. processed_sec[i] = true;
  238. memcpy(license, data->d_buf, data->d_size);
  239. } else if (strcmp(shname, "version") == 0) {
  240. processed_sec[i] = true;
  241. if (data->d_size != sizeof(int)) {
  242. printf("invalid size of version section %zd\n",
  243. data->d_size);
  244. return 1;
  245. }
  246. memcpy(&kern_version, data->d_buf, sizeof(int));
  247. } else if (strcmp(shname, "maps") == 0) {
  248. processed_sec[i] = true;
  249. if (load_maps(data->d_buf, data->d_size))
  250. return 1;
  251. } else if (shdr.sh_type == SHT_SYMTAB) {
  252. symbols = data;
  253. }
  254. }
  255. /* load programs that need map fixup (relocations) */
  256. for (i = 1; i < ehdr.e_shnum; i++) {
  257. if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
  258. continue;
  259. if (shdr.sh_type == SHT_REL) {
  260. struct bpf_insn *insns;
  261. if (get_sec(elf, shdr.sh_info, &ehdr, &shname_prog,
  262. &shdr_prog, &data_prog))
  263. continue;
  264. insns = (struct bpf_insn *) data_prog->d_buf;
  265. processed_sec[shdr.sh_info] = true;
  266. processed_sec[i] = true;
  267. if (parse_relo_and_apply(data, symbols, &shdr, insns))
  268. continue;
  269. if (memcmp(shname_prog, "kprobe/", 7) == 0 ||
  270. memcmp(shname_prog, "kretprobe/", 10) == 0 ||
  271. memcmp(shname_prog, "tracepoint/", 11) == 0 ||
  272. memcmp(shname_prog, "xdp", 3) == 0 ||
  273. memcmp(shname_prog, "perf_event", 10) == 0 ||
  274. memcmp(shname_prog, "socket", 6) == 0)
  275. load_and_attach(shname_prog, insns, data_prog->d_size);
  276. }
  277. }
  278. /* load programs that don't use maps */
  279. for (i = 1; i < ehdr.e_shnum; i++) {
  280. if (processed_sec[i])
  281. continue;
  282. if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
  283. continue;
  284. if (memcmp(shname, "kprobe/", 7) == 0 ||
  285. memcmp(shname, "kretprobe/", 10) == 0 ||
  286. memcmp(shname, "tracepoint/", 11) == 0 ||
  287. memcmp(shname, "xdp", 3) == 0 ||
  288. memcmp(shname, "perf_event", 10) == 0 ||
  289. memcmp(shname, "socket", 6) == 0)
  290. load_and_attach(shname, data->d_buf, data->d_size);
  291. }
  292. close(fd);
  293. return 0;
  294. }
  295. void read_trace_pipe(void)
  296. {
  297. int trace_fd;
  298. trace_fd = open(DEBUGFS "trace_pipe", O_RDONLY, 0);
  299. if (trace_fd < 0)
  300. return;
  301. while (1) {
  302. static char buf[4096];
  303. ssize_t sz;
  304. sz = read(trace_fd, buf, sizeof(buf));
  305. if (sz > 0) {
  306. buf[sz] = 0;
  307. puts(buf);
  308. }
  309. }
  310. }
  311. #define MAX_SYMS 300000
  312. static struct ksym syms[MAX_SYMS];
  313. static int sym_cnt;
  314. static int ksym_cmp(const void *p1, const void *p2)
  315. {
  316. return ((struct ksym *)p1)->addr - ((struct ksym *)p2)->addr;
  317. }
  318. int load_kallsyms(void)
  319. {
  320. FILE *f = fopen("/proc/kallsyms", "r");
  321. char func[256], buf[256];
  322. char symbol;
  323. void *addr;
  324. int i = 0;
  325. if (!f)
  326. return -ENOENT;
  327. while (!feof(f)) {
  328. if (!fgets(buf, sizeof(buf), f))
  329. break;
  330. if (sscanf(buf, "%p %c %s", &addr, &symbol, func) != 3)
  331. break;
  332. if (!addr)
  333. continue;
  334. syms[i].addr = (long) addr;
  335. syms[i].name = strdup(func);
  336. i++;
  337. }
  338. sym_cnt = i;
  339. qsort(syms, sym_cnt, sizeof(struct ksym), ksym_cmp);
  340. return 0;
  341. }
  342. struct ksym *ksym_search(long key)
  343. {
  344. int start = 0, end = sym_cnt;
  345. int result;
  346. while (start < end) {
  347. size_t mid = start + (end - start) / 2;
  348. result = key - syms[mid].addr;
  349. if (result < 0)
  350. end = mid;
  351. else if (result > 0)
  352. start = mid + 1;
  353. else
  354. return &syms[mid];
  355. }
  356. if (start >= 1 && syms[start - 1].addr < key &&
  357. key < syms[start].addr)
  358. /* valid ksym */
  359. return &syms[start - 1];
  360. /* out of range. return _stext */
  361. return &syms[0];
  362. }