blob: 0d449d8032d1ace67ad780f6d2ae86ce7fc6010e [file] [log] [blame]
Alexei Starovoitov249b8122014-12-01 15:06:37 -08001#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>
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070011#include <stdlib.h>
Alexei Starovoitov249b8122014-12-01 15:06:37 -080012#include <linux/bpf.h>
13#include <linux/filter.h>
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070014#include <linux/perf_event.h>
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -080015#include <linux/netlink.h>
16#include <linux/rtnetlink.h>
17#include <sys/types.h>
18#include <sys/socket.h>
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070019#include <sys/syscall.h>
20#include <sys/ioctl.h>
21#include <sys/mman.h>
22#include <poll.h>
Alexei Starovoitov5bacd782015-05-19 16:59:05 -070023#include <ctype.h>
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -070024#include <assert.h>
Alexei Starovoitov249b8122014-12-01 15:06:37 -080025#include "libbpf.h"
Alexei Starovoitov249b8122014-12-01 15:06:37 -080026#include "bpf_load.h"
Joe Stringer205c8ad2016-12-08 18:46:19 -080027#include "perf-sys.h"
Alexei Starovoitov249b8122014-12-01 15:06:37 -080028
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070029#define DEBUGFS "/sys/kernel/debug/tracing/"
30
Alexei Starovoitov249b8122014-12-01 15:06:37 -080031static char license[128];
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070032static int kern_version;
Alexei Starovoitov249b8122014-12-01 15:06:37 -080033static bool processed_sec[128];
Joe Stringerd40fc182016-12-14 14:43:38 -080034char bpf_log_buf[BPF_LOG_BUF_SIZE];
Alexei Starovoitov249b8122014-12-01 15:06:37 -080035int map_fd[MAX_MAPS];
36int prog_fd[MAX_PROGS];
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070037int event_fd[MAX_PROGS];
Alexei Starovoitov249b8122014-12-01 15:06:37 -080038int prog_cnt;
Alexei Starovoitov5bacd782015-05-19 16:59:05 -070039int prog_array_fd = -1;
40
41static int populate_prog_array(const char *event, int prog_fd)
42{
43 int ind = atoi(event), err;
44
Joe Stringerd40fc182016-12-14 14:43:38 -080045 err = bpf_map_update_elem(prog_array_fd, &ind, &prog_fd, BPF_ANY);
Alexei Starovoitov5bacd782015-05-19 16:59:05 -070046 if (err < 0) {
47 printf("failed to store prog_fd in prog_array\n");
48 return -1;
49 }
50 return 0;
51}
Alexei Starovoitov249b8122014-12-01 15:06:37 -080052
53static int load_and_attach(const char *event, struct bpf_insn *prog, int size)
54{
Alexei Starovoitov249b8122014-12-01 15:06:37 -080055 bool is_socket = strncmp(event, "socket", 6) == 0;
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070056 bool is_kprobe = strncmp(event, "kprobe/", 7) == 0;
57 bool is_kretprobe = strncmp(event, "kretprobe/", 10) == 0;
Alexei Starovoitovc0766042016-04-06 18:43:29 -070058 bool is_tracepoint = strncmp(event, "tracepoint/", 11) == 0;
Brenden Blanco86af8b42016-07-19 12:16:51 -070059 bool is_xdp = strncmp(event, "xdp", 3) == 0;
Alexei Starovoitov1c47910e2016-09-01 18:37:25 -070060 bool is_perf_event = strncmp(event, "perf_event", 10) == 0;
David Ahern4f2e7ae2016-12-01 08:48:07 -080061 bool is_cgroup_skb = strncmp(event, "cgroup/skb", 10) == 0;
62 bool is_cgroup_sk = strncmp(event, "cgroup/sock", 11) == 0;
Joe Stringer43371c82016-12-14 14:43:39 -080063 size_t insns_cnt = size / sizeof(struct bpf_insn);
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070064 enum bpf_prog_type prog_type;
65 char buf[256];
66 int fd, efd, err, id;
67 struct perf_event_attr attr = {};
Alexei Starovoitov249b8122014-12-01 15:06:37 -080068
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070069 attr.type = PERF_TYPE_TRACEPOINT;
70 attr.sample_type = PERF_SAMPLE_RAW;
71 attr.sample_period = 1;
72 attr.wakeup_events = 1;
73
74 if (is_socket) {
75 prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
76 } else if (is_kprobe || is_kretprobe) {
77 prog_type = BPF_PROG_TYPE_KPROBE;
Alexei Starovoitovc0766042016-04-06 18:43:29 -070078 } else if (is_tracepoint) {
79 prog_type = BPF_PROG_TYPE_TRACEPOINT;
Brenden Blanco86af8b42016-07-19 12:16:51 -070080 } else if (is_xdp) {
81 prog_type = BPF_PROG_TYPE_XDP;
Alexei Starovoitov1c47910e2016-09-01 18:37:25 -070082 } else if (is_perf_event) {
83 prog_type = BPF_PROG_TYPE_PERF_EVENT;
David Ahern4f2e7ae2016-12-01 08:48:07 -080084 } else if (is_cgroup_skb) {
85 prog_type = BPF_PROG_TYPE_CGROUP_SKB;
86 } else if (is_cgroup_sk) {
87 prog_type = BPF_PROG_TYPE_CGROUP_SOCK;
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070088 } else {
89 printf("Unknown event '%s'\n", event);
Alexei Starovoitov249b8122014-12-01 15:06:37 -080090 return -1;
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070091 }
Alexei Starovoitov249b8122014-12-01 15:06:37 -080092
Joe Stringer43371c82016-12-14 14:43:39 -080093 fd = bpf_load_program(prog_type, prog, insns_cnt, license, kern_version,
Joe Stringerd40fc182016-12-14 14:43:38 -080094 bpf_log_buf, BPF_LOG_BUF_SIZE);
Alexei Starovoitov5bacd782015-05-19 16:59:05 -070095 if (fd < 0) {
Joe Stringerd40fc182016-12-14 14:43:38 -080096 printf("bpf_load_program() err=%d\n%s", errno, bpf_log_buf);
Alexei Starovoitov5bacd782015-05-19 16:59:05 -070097 return -1;
98 }
99
100 prog_fd[prog_cnt++] = fd;
101
David Ahern4f2e7ae2016-12-01 08:48:07 -0800102 if (is_xdp || is_perf_event || is_cgroup_skb || is_cgroup_sk)
Brenden Blanco86af8b42016-07-19 12:16:51 -0700103 return 0;
104
Alexei Starovoitov5bacd782015-05-19 16:59:05 -0700105 if (is_socket) {
106 event += 6;
107 if (*event != '/')
108 return 0;
109 event++;
110 if (!isdigit(*event)) {
111 printf("invalid prog number\n");
112 return -1;
113 }
114 return populate_prog_array(event, fd);
115 }
116
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700117 if (is_kprobe || is_kretprobe) {
118 if (is_kprobe)
119 event += 7;
120 else
121 event += 10;
122
Alexei Starovoitov5bacd782015-05-19 16:59:05 -0700123 if (*event == 0) {
124 printf("event name cannot be empty\n");
125 return -1;
126 }
127
128 if (isdigit(*event))
129 return populate_prog_array(event, fd);
130
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700131 snprintf(buf, sizeof(buf),
132 "echo '%c:%s %s' >> /sys/kernel/debug/tracing/kprobe_events",
133 is_kprobe ? 'p' : 'r', event, event);
134 err = system(buf);
135 if (err < 0) {
136 printf("failed to create kprobe '%s' error '%s'\n",
137 event, strerror(errno));
138 return -1;
139 }
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700140
Alexei Starovoitovc0766042016-04-06 18:43:29 -0700141 strcpy(buf, DEBUGFS);
142 strcat(buf, "events/kprobes/");
143 strcat(buf, event);
144 strcat(buf, "/id");
145 } else if (is_tracepoint) {
146 event += 11;
147
148 if (*event == 0) {
149 printf("event name cannot be empty\n");
150 return -1;
151 }
152 strcpy(buf, DEBUGFS);
153 strcat(buf, "events/");
154 strcat(buf, event);
155 strcat(buf, "/id");
156 }
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700157
158 efd = open(buf, O_RDONLY, 0);
159 if (efd < 0) {
160 printf("failed to open event %s\n", event);
161 return -1;
162 }
163
164 err = read(efd, buf, sizeof(buf));
165 if (err < 0 || err >= sizeof(buf)) {
166 printf("read from '%s' failed '%s'\n", event, strerror(errno));
167 return -1;
168 }
169
170 close(efd);
171
172 buf[err] = 0;
173 id = atoi(buf);
174 attr.config = id;
175
Joe Stringer205c8ad2016-12-08 18:46:19 -0800176 efd = sys_perf_event_open(&attr, -1/*pid*/, 0/*cpu*/, -1/*group_fd*/, 0);
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700177 if (efd < 0) {
178 printf("event %d fd %d err %s\n", id, efd, strerror(errno));
179 return -1;
180 }
181 event_fd[prog_cnt - 1] = efd;
182 ioctl(efd, PERF_EVENT_IOC_ENABLE, 0);
183 ioctl(efd, PERF_EVENT_IOC_SET_BPF, fd);
184
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800185 return 0;
186}
187
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700188static int load_maps(struct bpf_map_def *maps, int len,
189 const char **map_names, fixup_map_cb fixup_map)
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800190{
191 int i;
192
193 for (i = 0; i < len / sizeof(struct bpf_map_def); i++) {
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700194 if (fixup_map)
195 fixup_map(&maps[i], map_names[i], i);
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800196
Martin KaFai Laufb30d4b2017-03-22 10:00:35 -0700197 if (maps[i].type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
198 maps[i].type == BPF_MAP_TYPE_HASH_OF_MAPS) {
199 int inner_map_fd = map_fd[maps[i].inner_map_idx];
200
201 map_fd[i] = bpf_create_map_in_map(maps[i].type,
202 maps[i].key_size,
203 inner_map_fd,
204 maps[i].max_entries,
205 maps[i].map_flags);
206 } else {
207 map_fd[i] = bpf_create_map(maps[i].type,
208 maps[i].key_size,
209 maps[i].value_size,
210 maps[i].max_entries,
211 maps[i].map_flags);
212 }
Alexei Starovoitov618ec9a72016-03-07 21:57:18 -0800213 if (map_fd[i] < 0) {
214 printf("failed to create a map: %d %s\n",
215 errno, strerror(errno));
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800216 return 1;
Alexei Starovoitov618ec9a72016-03-07 21:57:18 -0800217 }
Alexei Starovoitov5bacd782015-05-19 16:59:05 -0700218
219 if (maps[i].type == BPF_MAP_TYPE_PROG_ARRAY)
220 prog_array_fd = map_fd[i];
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800221 }
222 return 0;
223}
224
225static int get_sec(Elf *elf, int i, GElf_Ehdr *ehdr, char **shname,
226 GElf_Shdr *shdr, Elf_Data **data)
227{
228 Elf_Scn *scn;
229
230 scn = elf_getscn(elf, i);
231 if (!scn)
232 return 1;
233
234 if (gelf_getshdr(scn, shdr) != shdr)
235 return 2;
236
237 *shname = elf_strptr(elf, ehdr->e_shstrndx, shdr->sh_name);
238 if (!*shname || !shdr->sh_size)
239 return 3;
240
241 *data = elf_getdata(scn, 0);
242 if (!*data || elf_getdata(scn, *data) != NULL)
243 return 4;
244
245 return 0;
246}
247
248static int parse_relo_and_apply(Elf_Data *data, Elf_Data *symbols,
249 GElf_Shdr *shdr, struct bpf_insn *insn)
250{
251 int i, nrels;
252
253 nrels = shdr->sh_size / shdr->sh_entsize;
254
255 for (i = 0; i < nrels; i++) {
256 GElf_Sym sym;
257 GElf_Rel rel;
258 unsigned int insn_idx;
259
260 gelf_getrel(data, i, &rel);
261
262 insn_idx = rel.r_offset / sizeof(struct bpf_insn);
263
264 gelf_getsym(symbols, GELF_R_SYM(rel.r_info), &sym);
265
266 if (insn[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) {
267 printf("invalid relo for insn[%d].code 0x%x\n",
268 insn_idx, insn[insn_idx].code);
269 return 1;
270 }
271 insn[insn_idx].src_reg = BPF_PSEUDO_MAP_FD;
272 insn[insn_idx].imm = map_fd[sym.st_value / sizeof(struct bpf_map_def)];
273 }
274
275 return 0;
276}
277
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700278static int cmp_symbols(const void *l, const void *r)
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800279{
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700280 const GElf_Sym *lsym = (const GElf_Sym *)l;
281 const GElf_Sym *rsym = (const GElf_Sym *)r;
282
283 if (lsym->st_value < rsym->st_value)
284 return -1;
285 else if (lsym->st_value > rsym->st_value)
286 return 1;
287 else
288 return 0;
289}
290
291static int get_sorted_map_names(Elf *elf, Elf_Data *symbols, int maps_shndx,
292 int strtabidx, char **map_names)
293{
294 GElf_Sym map_symbols[MAX_MAPS];
295 int i, nr_maps = 0;
296
297 for (i = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) {
298 assert(nr_maps < MAX_MAPS);
299 if (!gelf_getsym(symbols, i, &map_symbols[nr_maps]))
300 continue;
301 if (map_symbols[nr_maps].st_shndx != maps_shndx)
302 continue;
303 nr_maps++;
304 }
305
306 qsort(map_symbols, nr_maps, sizeof(GElf_Sym), cmp_symbols);
307
308 for (i = 0; i < nr_maps; i++) {
309 char *map_name;
310
311 map_name = elf_strptr(elf, strtabidx, map_symbols[i].st_name);
312 if (!map_name) {
313 printf("cannot get map symbol\n");
314 return 1;
315 }
316
317 map_names[i] = strdup(map_name);
318 if (!map_names[i]) {
319 printf("strdup(%s): %s(%d)\n", map_name,
320 strerror(errno), errno);
321 return 1;
322 }
323 }
324
325 return 0;
326}
327
328static int do_load_bpf_file(const char *path, fixup_map_cb fixup_map)
329{
330 int fd, i, ret, maps_shndx = -1, strtabidx = -1;
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800331 Elf *elf;
332 GElf_Ehdr ehdr;
333 GElf_Shdr shdr, shdr_prog;
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700334 Elf_Data *data, *data_prog, *data_maps = NULL, *symbols = NULL;
335 char *shname, *shname_prog, *map_names[MAX_MAPS] = { NULL };
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800336
Mickaël Salaüna734fb52017-02-08 21:27:43 +0100337 /* reset global variables */
338 kern_version = 0;
339 memset(license, 0, sizeof(license));
340 memset(processed_sec, 0, sizeof(processed_sec));
341
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800342 if (elf_version(EV_CURRENT) == EV_NONE)
343 return 1;
344
345 fd = open(path, O_RDONLY, 0);
346 if (fd < 0)
347 return 1;
348
349 elf = elf_begin(fd, ELF_C_READ, NULL);
350
351 if (!elf)
352 return 1;
353
354 if (gelf_getehdr(elf, &ehdr) != &ehdr)
355 return 1;
356
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700357 /* clear all kprobes */
358 i = system("echo \"\" > /sys/kernel/debug/tracing/kprobe_events");
359
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800360 /* scan over all elf sections to get license and map info */
361 for (i = 1; i < ehdr.e_shnum; i++) {
362
363 if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
364 continue;
365
366 if (0) /* helpful for llvm debugging */
367 printf("section %d:%s data %p size %zd link %d flags %d\n",
368 i, shname, data->d_buf, data->d_size,
369 shdr.sh_link, (int) shdr.sh_flags);
370
371 if (strcmp(shname, "license") == 0) {
372 processed_sec[i] = true;
373 memcpy(license, data->d_buf, data->d_size);
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700374 } else if (strcmp(shname, "version") == 0) {
375 processed_sec[i] = true;
376 if (data->d_size != sizeof(int)) {
377 printf("invalid size of version section %zd\n",
378 data->d_size);
379 return 1;
380 }
381 memcpy(&kern_version, data->d_buf, sizeof(int));
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800382 } else if (strcmp(shname, "maps") == 0) {
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700383 maps_shndx = i;
384 data_maps = data;
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800385 } else if (shdr.sh_type == SHT_SYMTAB) {
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700386 strtabidx = shdr.sh_link;
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800387 symbols = data;
388 }
389 }
390
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700391 ret = 1;
392
393 if (!symbols) {
394 printf("missing SHT_SYMTAB section\n");
395 goto done;
396 }
397
398 if (data_maps) {
399 if (get_sorted_map_names(elf, symbols, maps_shndx, strtabidx,
400 map_names))
401 goto done;
402
403 if (load_maps(data_maps->d_buf, data_maps->d_size,
404 (const char **)map_names, fixup_map))
405 goto done;
406
407 processed_sec[maps_shndx] = true;
408 }
409
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800410 /* load programs that need map fixup (relocations) */
411 for (i = 1; i < ehdr.e_shnum; i++) {
Mickaël Salaün16ad1322017-02-08 21:27:42 +0100412 if (processed_sec[i])
413 continue;
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800414
415 if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
416 continue;
417 if (shdr.sh_type == SHT_REL) {
418 struct bpf_insn *insns;
419
420 if (get_sec(elf, shdr.sh_info, &ehdr, &shname_prog,
421 &shdr_prog, &data_prog))
422 continue;
423
Alexei Starovoitovdb6a71d2016-11-22 16:52:09 -0800424 if (shdr_prog.sh_type != SHT_PROGBITS ||
425 !(shdr_prog.sh_flags & SHF_EXECINSTR))
426 continue;
427
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800428 insns = (struct bpf_insn *) data_prog->d_buf;
429
430 processed_sec[shdr.sh_info] = true;
431 processed_sec[i] = true;
432
433 if (parse_relo_and_apply(data, symbols, &shdr, insns))
434 continue;
435
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700436 if (memcmp(shname_prog, "kprobe/", 7) == 0 ||
437 memcmp(shname_prog, "kretprobe/", 10) == 0 ||
Alexei Starovoitovc0766042016-04-06 18:43:29 -0700438 memcmp(shname_prog, "tracepoint/", 11) == 0 ||
Brenden Blanco86af8b42016-07-19 12:16:51 -0700439 memcmp(shname_prog, "xdp", 3) == 0 ||
Alexei Starovoitov1c47910e2016-09-01 18:37:25 -0700440 memcmp(shname_prog, "perf_event", 10) == 0 ||
David Ahern4f2e7ae2016-12-01 08:48:07 -0800441 memcmp(shname_prog, "socket", 6) == 0 ||
442 memcmp(shname_prog, "cgroup/", 7) == 0)
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800443 load_and_attach(shname_prog, insns, data_prog->d_size);
444 }
445 }
446
447 /* load programs that don't use maps */
448 for (i = 1; i < ehdr.e_shnum; i++) {
449
450 if (processed_sec[i])
451 continue;
452
453 if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
454 continue;
455
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700456 if (memcmp(shname, "kprobe/", 7) == 0 ||
457 memcmp(shname, "kretprobe/", 10) == 0 ||
Alexei Starovoitovc0766042016-04-06 18:43:29 -0700458 memcmp(shname, "tracepoint/", 11) == 0 ||
Brenden Blanco86af8b42016-07-19 12:16:51 -0700459 memcmp(shname, "xdp", 3) == 0 ||
Alexei Starovoitov1c47910e2016-09-01 18:37:25 -0700460 memcmp(shname, "perf_event", 10) == 0 ||
David Ahern4f2e7ae2016-12-01 08:48:07 -0800461 memcmp(shname, "socket", 6) == 0 ||
462 memcmp(shname, "cgroup/", 7) == 0)
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800463 load_and_attach(shname, data->d_buf, data->d_size);
464 }
465
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700466 ret = 0;
467done:
468 for (i = 0; i < MAX_MAPS; i++)
469 free(map_names[i]);
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800470 close(fd);
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700471 return ret;
472}
473
474int load_bpf_file(char *path)
475{
476 return do_load_bpf_file(path, NULL);
477}
478
479int load_bpf_file_fixup_map(const char *path, fixup_map_cb fixup_map)
480{
481 return do_load_bpf_file(path, fixup_map);
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800482}
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700483
484void read_trace_pipe(void)
485{
486 int trace_fd;
487
488 trace_fd = open(DEBUGFS "trace_pipe", O_RDONLY, 0);
489 if (trace_fd < 0)
490 return;
491
492 while (1) {
493 static char buf[4096];
494 ssize_t sz;
495
496 sz = read(trace_fd, buf, sizeof(buf));
497 if (sz > 0) {
498 buf[sz] = 0;
499 puts(buf);
500 }
501 }
502}
Alexei Starovoitov3622e7e2016-03-07 21:57:19 -0800503
504#define MAX_SYMS 300000
505static struct ksym syms[MAX_SYMS];
506static int sym_cnt;
507
508static int ksym_cmp(const void *p1, const void *p2)
509{
510 return ((struct ksym *)p1)->addr - ((struct ksym *)p2)->addr;
511}
512
513int load_kallsyms(void)
514{
515 FILE *f = fopen("/proc/kallsyms", "r");
516 char func[256], buf[256];
517 char symbol;
518 void *addr;
519 int i = 0;
520
521 if (!f)
522 return -ENOENT;
523
524 while (!feof(f)) {
525 if (!fgets(buf, sizeof(buf), f))
526 break;
527 if (sscanf(buf, "%p %c %s", &addr, &symbol, func) != 3)
528 break;
529 if (!addr)
530 continue;
531 syms[i].addr = (long) addr;
532 syms[i].name = strdup(func);
533 i++;
534 }
535 sym_cnt = i;
536 qsort(syms, sym_cnt, sizeof(struct ksym), ksym_cmp);
537 return 0;
538}
539
540struct ksym *ksym_search(long key)
541{
542 int start = 0, end = sym_cnt;
543 int result;
544
545 while (start < end) {
546 size_t mid = start + (end - start) / 2;
547
548 result = key - syms[mid].addr;
549 if (result < 0)
550 end = mid;
551 else if (result > 0)
552 start = mid + 1;
553 else
554 return &syms[mid];
555 }
556
557 if (start >= 1 && syms[start - 1].addr < key &&
558 key < syms[start].addr)
559 /* valid ksym */
560 return &syms[start - 1];
561
562 /* out of range. return _stext */
563 return &syms[0];
564}
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -0800565
566int set_link_xdp_fd(int ifindex, int fd)
567{
568 struct sockaddr_nl sa;
569 int sock, seq = 0, len, ret = -1;
570 char buf[4096];
571 struct nlattr *nla, *nla_xdp;
572 struct {
573 struct nlmsghdr nh;
574 struct ifinfomsg ifinfo;
575 char attrbuf[64];
576 } req;
577 struct nlmsghdr *nh;
578 struct nlmsgerr *err;
579
580 memset(&sa, 0, sizeof(sa));
581 sa.nl_family = AF_NETLINK;
582
583 sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
584 if (sock < 0) {
585 printf("open netlink socket: %s\n", strerror(errno));
586 return -1;
587 }
588
589 if (bind(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
590 printf("bind to netlink: %s\n", strerror(errno));
591 goto cleanup;
592 }
593
594 memset(&req, 0, sizeof(req));
595 req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
596 req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
597 req.nh.nlmsg_type = RTM_SETLINK;
598 req.nh.nlmsg_pid = 0;
599 req.nh.nlmsg_seq = ++seq;
600 req.ifinfo.ifi_family = AF_UNSPEC;
601 req.ifinfo.ifi_index = ifindex;
602 nla = (struct nlattr *)(((char *)&req)
603 + NLMSG_ALIGN(req.nh.nlmsg_len));
604 nla->nla_type = NLA_F_NESTED | 43/*IFLA_XDP*/;
605
606 nla_xdp = (struct nlattr *)((char *)nla + NLA_HDRLEN);
607 nla_xdp->nla_type = 1/*IFLA_XDP_FD*/;
608 nla_xdp->nla_len = NLA_HDRLEN + sizeof(int);
609 memcpy((char *)nla_xdp + NLA_HDRLEN, &fd, sizeof(fd));
610 nla->nla_len = NLA_HDRLEN + nla_xdp->nla_len;
611
612 req.nh.nlmsg_len += NLA_ALIGN(nla->nla_len);
613
614 if (send(sock, &req, req.nh.nlmsg_len, 0) < 0) {
615 printf("send to netlink: %s\n", strerror(errno));
616 goto cleanup;
617 }
618
619 len = recv(sock, buf, sizeof(buf), 0);
620 if (len < 0) {
621 printf("recv from netlink: %s\n", strerror(errno));
622 goto cleanup;
623 }
624
625 for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len);
626 nh = NLMSG_NEXT(nh, len)) {
627 if (nh->nlmsg_pid != getpid()) {
628 printf("Wrong pid %d, expected %d\n",
629 nh->nlmsg_pid, getpid());
630 goto cleanup;
631 }
632 if (nh->nlmsg_seq != seq) {
633 printf("Wrong seq %d, expected %d\n",
634 nh->nlmsg_seq, seq);
635 goto cleanup;
636 }
637 switch (nh->nlmsg_type) {
638 case NLMSG_ERROR:
639 err = (struct nlmsgerr *)NLMSG_DATA(nh);
640 if (!err->error)
641 continue;
642 printf("nlmsg error %s\n", strerror(-err->error));
643 goto cleanup;
644 case NLMSG_DONE:
645 break;
646 }
647 }
648
649 ret = 0;
650
651cleanup:
652 close(sock);
653 return ret;
654}