blob: a8552b8a2ab6e770c25ef4351f48cc1768dd1e81 [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>
Jesper Dangaard Brouer6387d012017-05-01 11:26:15 +020017#include <linux/types.h>
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -080018#include <sys/types.h>
19#include <sys/socket.h>
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070020#include <sys/syscall.h>
21#include <sys/ioctl.h>
22#include <sys/mman.h>
23#include <poll.h>
Alexei Starovoitov5bacd782015-05-19 16:59:05 -070024#include <ctype.h>
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -070025#include <assert.h>
Alexei Starovoitov249b8122014-12-01 15:06:37 -080026#include "libbpf.h"
Alexei Starovoitov249b8122014-12-01 15:06:37 -080027#include "bpf_load.h"
Joe Stringer205c8ad2016-12-08 18:46:19 -080028#include "perf-sys.h"
Alexei Starovoitov249b8122014-12-01 15:06:37 -080029
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070030#define DEBUGFS "/sys/kernel/debug/tracing/"
31
Alexei Starovoitov249b8122014-12-01 15:06:37 -080032static char license[128];
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070033static int kern_version;
Alexei Starovoitov249b8122014-12-01 15:06:37 -080034static bool processed_sec[128];
Joe Stringerd40fc182016-12-14 14:43:38 -080035char bpf_log_buf[BPF_LOG_BUF_SIZE];
Alexei Starovoitov249b8122014-12-01 15:06:37 -080036int map_fd[MAX_MAPS];
37int prog_fd[MAX_PROGS];
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070038int event_fd[MAX_PROGS];
Alexei Starovoitov249b8122014-12-01 15:06:37 -080039int prog_cnt;
Alexei Starovoitov5bacd782015-05-19 16:59:05 -070040int prog_array_fd = -1;
41
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +020042struct bpf_map_data map_data[MAX_MAPS];
43int map_data_count = 0;
44
Alexei Starovoitov5bacd782015-05-19 16:59:05 -070045static int populate_prog_array(const char *event, int prog_fd)
46{
47 int ind = atoi(event), err;
48
Joe Stringerd40fc182016-12-14 14:43:38 -080049 err = bpf_map_update_elem(prog_array_fd, &ind, &prog_fd, BPF_ANY);
Alexei Starovoitov5bacd782015-05-19 16:59:05 -070050 if (err < 0) {
51 printf("failed to store prog_fd in prog_array\n");
52 return -1;
53 }
54 return 0;
55}
Alexei Starovoitov249b8122014-12-01 15:06:37 -080056
57static int load_and_attach(const char *event, struct bpf_insn *prog, int size)
58{
Alexei Starovoitov249b8122014-12-01 15:06:37 -080059 bool is_socket = strncmp(event, "socket", 6) == 0;
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070060 bool is_kprobe = strncmp(event, "kprobe/", 7) == 0;
61 bool is_kretprobe = strncmp(event, "kretprobe/", 10) == 0;
Alexei Starovoitovc0766042016-04-06 18:43:29 -070062 bool is_tracepoint = strncmp(event, "tracepoint/", 11) == 0;
Brenden Blanco86af8b42016-07-19 12:16:51 -070063 bool is_xdp = strncmp(event, "xdp", 3) == 0;
Alexei Starovoitov1c47910e2016-09-01 18:37:25 -070064 bool is_perf_event = strncmp(event, "perf_event", 10) == 0;
David Ahern4f2e7ae2016-12-01 08:48:07 -080065 bool is_cgroup_skb = strncmp(event, "cgroup/skb", 10) == 0;
66 bool is_cgroup_sk = strncmp(event, "cgroup/sock", 11) == 0;
Lawrence Brakmo40304b22017-06-30 20:02:40 -070067 bool is_sockops = strncmp(event, "sockops", 7) == 0;
John Fastabend69e8cc12017-08-15 22:33:32 -070068 bool is_sk_skb = strncmp(event, "sk_skb", 6) == 0;
Joe Stringer43371c82016-12-14 14:43:39 -080069 size_t insns_cnt = size / sizeof(struct bpf_insn);
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070070 enum bpf_prog_type prog_type;
71 char buf[256];
72 int fd, efd, err, id;
73 struct perf_event_attr attr = {};
Alexei Starovoitov249b8122014-12-01 15:06:37 -080074
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070075 attr.type = PERF_TYPE_TRACEPOINT;
76 attr.sample_type = PERF_SAMPLE_RAW;
77 attr.sample_period = 1;
78 attr.wakeup_events = 1;
79
80 if (is_socket) {
81 prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
82 } else if (is_kprobe || is_kretprobe) {
83 prog_type = BPF_PROG_TYPE_KPROBE;
Alexei Starovoitovc0766042016-04-06 18:43:29 -070084 } else if (is_tracepoint) {
85 prog_type = BPF_PROG_TYPE_TRACEPOINT;
Brenden Blanco86af8b42016-07-19 12:16:51 -070086 } else if (is_xdp) {
87 prog_type = BPF_PROG_TYPE_XDP;
Alexei Starovoitov1c47910e2016-09-01 18:37:25 -070088 } else if (is_perf_event) {
89 prog_type = BPF_PROG_TYPE_PERF_EVENT;
David Ahern4f2e7ae2016-12-01 08:48:07 -080090 } else if (is_cgroup_skb) {
91 prog_type = BPF_PROG_TYPE_CGROUP_SKB;
92 } else if (is_cgroup_sk) {
93 prog_type = BPF_PROG_TYPE_CGROUP_SOCK;
Lawrence Brakmo40304b22017-06-30 20:02:40 -070094 } else if (is_sockops) {
95 prog_type = BPF_PROG_TYPE_SOCK_OPS;
John Fastabend69e8cc12017-08-15 22:33:32 -070096 } else if (is_sk_skb) {
97 prog_type = BPF_PROG_TYPE_SK_SKB;
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070098 } else {
99 printf("Unknown event '%s'\n", event);
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800100 return -1;
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700101 }
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800102
Joe Stringer43371c82016-12-14 14:43:39 -0800103 fd = bpf_load_program(prog_type, prog, insns_cnt, license, kern_version,
Joe Stringerd40fc182016-12-14 14:43:38 -0800104 bpf_log_buf, BPF_LOG_BUF_SIZE);
Alexei Starovoitov5bacd782015-05-19 16:59:05 -0700105 if (fd < 0) {
Joe Stringerd40fc182016-12-14 14:43:38 -0800106 printf("bpf_load_program() err=%d\n%s", errno, bpf_log_buf);
Alexei Starovoitov5bacd782015-05-19 16:59:05 -0700107 return -1;
108 }
109
110 prog_fd[prog_cnt++] = fd;
111
David Ahern4f2e7ae2016-12-01 08:48:07 -0800112 if (is_xdp || is_perf_event || is_cgroup_skb || is_cgroup_sk)
Brenden Blanco86af8b42016-07-19 12:16:51 -0700113 return 0;
114
John Fastabend69e8cc12017-08-15 22:33:32 -0700115 if (is_socket || is_sockops || is_sk_skb) {
Lawrence Brakmo40304b22017-06-30 20:02:40 -0700116 if (is_socket)
117 event += 6;
118 else
119 event += 7;
Alexei Starovoitov5bacd782015-05-19 16:59:05 -0700120 if (*event != '/')
121 return 0;
122 event++;
123 if (!isdigit(*event)) {
124 printf("invalid prog number\n");
125 return -1;
126 }
127 return populate_prog_array(event, fd);
128 }
129
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700130 if (is_kprobe || is_kretprobe) {
131 if (is_kprobe)
132 event += 7;
133 else
134 event += 10;
135
Alexei Starovoitov5bacd782015-05-19 16:59:05 -0700136 if (*event == 0) {
137 printf("event name cannot be empty\n");
138 return -1;
139 }
140
141 if (isdigit(*event))
142 return populate_prog_array(event, fd);
143
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700144 snprintf(buf, sizeof(buf),
145 "echo '%c:%s %s' >> /sys/kernel/debug/tracing/kprobe_events",
146 is_kprobe ? 'p' : 'r', event, event);
147 err = system(buf);
148 if (err < 0) {
149 printf("failed to create kprobe '%s' error '%s'\n",
150 event, strerror(errno));
151 return -1;
152 }
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700153
Alexei Starovoitovc0766042016-04-06 18:43:29 -0700154 strcpy(buf, DEBUGFS);
155 strcat(buf, "events/kprobes/");
156 strcat(buf, event);
157 strcat(buf, "/id");
158 } else if (is_tracepoint) {
159 event += 11;
160
161 if (*event == 0) {
162 printf("event name cannot be empty\n");
163 return -1;
164 }
165 strcpy(buf, DEBUGFS);
166 strcat(buf, "events/");
167 strcat(buf, event);
168 strcat(buf, "/id");
169 }
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700170
171 efd = open(buf, O_RDONLY, 0);
172 if (efd < 0) {
173 printf("failed to open event %s\n", event);
174 return -1;
175 }
176
177 err = read(efd, buf, sizeof(buf));
178 if (err < 0 || err >= sizeof(buf)) {
179 printf("read from '%s' failed '%s'\n", event, strerror(errno));
180 return -1;
181 }
182
183 close(efd);
184
185 buf[err] = 0;
186 id = atoi(buf);
187 attr.config = id;
188
Joe Stringer205c8ad2016-12-08 18:46:19 -0800189 efd = sys_perf_event_open(&attr, -1/*pid*/, 0/*cpu*/, -1/*group_fd*/, 0);
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700190 if (efd < 0) {
191 printf("event %d fd %d err %s\n", id, efd, strerror(errno));
192 return -1;
193 }
194 event_fd[prog_cnt - 1] = efd;
195 ioctl(efd, PERF_EVENT_IOC_ENABLE, 0);
196 ioctl(efd, PERF_EVENT_IOC_SET_BPF, fd);
197
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800198 return 0;
199}
200
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200201static int load_maps(struct bpf_map_data *maps, int nr_maps,
202 fixup_map_cb fixup_map)
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800203{
204 int i;
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200205
Jesper Dangaard Brouer5010e942017-04-28 16:25:04 +0200206 for (i = 0; i < nr_maps; i++) {
Jesper Dangaard Brouer6979bcc2017-05-02 14:32:01 +0200207 if (fixup_map) {
208 fixup_map(&maps[i], i);
209 /* Allow userspace to assign map FD prior to creation */
210 if (maps[i].fd != -1) {
211 map_fd[i] = maps[i].fd;
212 continue;
213 }
214 }
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800215
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200216 if (maps[i].def.type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
217 maps[i].def.type == BPF_MAP_TYPE_HASH_OF_MAPS) {
218 int inner_map_fd = map_fd[maps[i].def.inner_map_idx];
Martin KaFai Laufb30d4b2017-03-22 10:00:35 -0700219
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200220 map_fd[i] = bpf_create_map_in_map(maps[i].def.type,
221 maps[i].def.key_size,
222 inner_map_fd,
223 maps[i].def.max_entries,
224 maps[i].def.map_flags);
Martin KaFai Laufb30d4b2017-03-22 10:00:35 -0700225 } else {
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200226 map_fd[i] = bpf_create_map(maps[i].def.type,
227 maps[i].def.key_size,
228 maps[i].def.value_size,
229 maps[i].def.max_entries,
230 maps[i].def.map_flags);
Martin KaFai Laufb30d4b2017-03-22 10:00:35 -0700231 }
Alexei Starovoitov618ec9a72016-03-07 21:57:18 -0800232 if (map_fd[i] < 0) {
233 printf("failed to create a map: %d %s\n",
234 errno, strerror(errno));
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800235 return 1;
Alexei Starovoitov618ec9a72016-03-07 21:57:18 -0800236 }
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200237 maps[i].fd = map_fd[i];
Alexei Starovoitov5bacd782015-05-19 16:59:05 -0700238
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200239 if (maps[i].def.type == BPF_MAP_TYPE_PROG_ARRAY)
Alexei Starovoitov5bacd782015-05-19 16:59:05 -0700240 prog_array_fd = map_fd[i];
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800241 }
242 return 0;
243}
244
245static int get_sec(Elf *elf, int i, GElf_Ehdr *ehdr, char **shname,
246 GElf_Shdr *shdr, Elf_Data **data)
247{
248 Elf_Scn *scn;
249
250 scn = elf_getscn(elf, i);
251 if (!scn)
252 return 1;
253
254 if (gelf_getshdr(scn, shdr) != shdr)
255 return 2;
256
257 *shname = elf_strptr(elf, ehdr->e_shstrndx, shdr->sh_name);
258 if (!*shname || !shdr->sh_size)
259 return 3;
260
261 *data = elf_getdata(scn, 0);
262 if (!*data || elf_getdata(scn, *data) != NULL)
263 return 4;
264
265 return 0;
266}
267
268static int parse_relo_and_apply(Elf_Data *data, Elf_Data *symbols,
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200269 GElf_Shdr *shdr, struct bpf_insn *insn,
270 struct bpf_map_data *maps, int nr_maps)
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800271{
272 int i, nrels;
273
274 nrels = shdr->sh_size / shdr->sh_entsize;
275
276 for (i = 0; i < nrels; i++) {
277 GElf_Sym sym;
278 GElf_Rel rel;
279 unsigned int insn_idx;
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200280 bool match = false;
281 int j, map_idx;
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800282
283 gelf_getrel(data, i, &rel);
284
285 insn_idx = rel.r_offset / sizeof(struct bpf_insn);
286
287 gelf_getsym(symbols, GELF_R_SYM(rel.r_info), &sym);
288
289 if (insn[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) {
290 printf("invalid relo for insn[%d].code 0x%x\n",
291 insn_idx, insn[insn_idx].code);
292 return 1;
293 }
294 insn[insn_idx].src_reg = BPF_PSEUDO_MAP_FD;
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200295
296 /* Match FD relocation against recorded map_data[] offset */
297 for (map_idx = 0; map_idx < nr_maps; map_idx++) {
298 if (maps[map_idx].elf_offset == sym.st_value) {
299 match = true;
300 break;
301 }
302 }
303 if (match) {
304 insn[insn_idx].imm = maps[map_idx].fd;
305 } else {
306 printf("invalid relo for insn[%d] no map_data match\n",
307 insn_idx);
308 return 1;
309 }
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800310 }
311
312 return 0;
313}
314
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700315static int cmp_symbols(const void *l, const void *r)
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800316{
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700317 const GElf_Sym *lsym = (const GElf_Sym *)l;
318 const GElf_Sym *rsym = (const GElf_Sym *)r;
319
320 if (lsym->st_value < rsym->st_value)
321 return -1;
322 else if (lsym->st_value > rsym->st_value)
323 return 1;
324 else
325 return 0;
326}
327
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200328static int load_elf_maps_section(struct bpf_map_data *maps, int maps_shndx,
329 Elf *elf, Elf_Data *symbols, int strtabidx)
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700330{
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200331 int map_sz_elf, map_sz_copy;
332 bool validate_zero = false;
333 Elf_Data *data_maps;
334 int i, nr_maps;
335 GElf_Sym *sym;
336 Elf_Scn *scn;
337 int copy_sz;
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700338
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200339 if (maps_shndx < 0)
340 return -EINVAL;
341 if (!symbols)
342 return -EINVAL;
343
344 /* Get data for maps section via elf index */
345 scn = elf_getscn(elf, maps_shndx);
346 if (scn)
347 data_maps = elf_getdata(scn, NULL);
348 if (!scn || !data_maps) {
349 printf("Failed to get Elf_Data from maps section %d\n",
350 maps_shndx);
351 return -EINVAL;
352 }
353
354 /* For each map get corrosponding symbol table entry */
355 sym = calloc(MAX_MAPS+1, sizeof(GElf_Sym));
356 for (i = 0, nr_maps = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) {
357 assert(nr_maps < MAX_MAPS+1);
358 if (!gelf_getsym(symbols, i, &sym[nr_maps]))
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700359 continue;
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200360 if (sym[nr_maps].st_shndx != maps_shndx)
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700361 continue;
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200362 /* Only increment iif maps section */
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700363 nr_maps++;
364 }
365
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200366 /* Align to map_fd[] order, via sort on offset in sym.st_value */
367 qsort(sym, nr_maps, sizeof(GElf_Sym), cmp_symbols);
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700368
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200369 /* Keeping compatible with ELF maps section changes
370 * ------------------------------------------------
371 * The program size of struct bpf_map_def is known by loader
372 * code, but struct stored in ELF file can be different.
373 *
374 * Unfortunately sym[i].st_size is zero. To calculate the
375 * struct size stored in the ELF file, assume all struct have
376 * the same size, and simply divide with number of map
377 * symbols.
378 */
379 map_sz_elf = data_maps->d_size / nr_maps;
380 map_sz_copy = sizeof(struct bpf_map_def);
381 if (map_sz_elf < map_sz_copy) {
382 /*
383 * Backward compat, loading older ELF file with
384 * smaller struct, keeping remaining bytes zero.
385 */
386 map_sz_copy = map_sz_elf;
387 } else if (map_sz_elf > map_sz_copy) {
388 /*
389 * Forward compat, loading newer ELF file with larger
390 * struct with unknown features. Assume zero means
391 * feature not used. Thus, validate rest of struct
392 * data is zero.
393 */
394 validate_zero = true;
395 }
396
397 /* Memcpy relevant part of ELF maps data to loader maps */
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700398 for (i = 0; i < nr_maps; i++) {
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200399 unsigned char *addr, *end;
400 struct bpf_map_def *def;
401 const char *map_name;
402 size_t offset;
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700403
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200404 map_name = elf_strptr(elf, strtabidx, sym[i].st_name);
405 maps[i].name = strdup(map_name);
406 if (!maps[i].name) {
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700407 printf("strdup(%s): %s(%d)\n", map_name,
408 strerror(errno), errno);
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200409 free(sym);
410 return -errno;
411 }
412
413 /* Symbol value is offset into ELF maps section data area */
414 offset = sym[i].st_value;
415 def = (struct bpf_map_def *)(data_maps->d_buf + offset);
416 maps[i].elf_offset = offset;
417 memset(&maps[i].def, 0, sizeof(struct bpf_map_def));
418 memcpy(&maps[i].def, def, map_sz_copy);
419
420 /* Verify no newer features were requested */
421 if (validate_zero) {
422 addr = (unsigned char*) def + map_sz_copy;
423 end = (unsigned char*) def + map_sz_elf;
424 for (; addr < end; addr++) {
425 if (*addr != 0) {
426 free(sym);
427 return -EFBIG;
428 }
429 }
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700430 }
431 }
432
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200433 free(sym);
Jesper Dangaard Brouer5010e942017-04-28 16:25:04 +0200434 return nr_maps;
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700435}
436
437static int do_load_bpf_file(const char *path, fixup_map_cb fixup_map)
438{
439 int fd, i, ret, maps_shndx = -1, strtabidx = -1;
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800440 Elf *elf;
441 GElf_Ehdr ehdr;
442 GElf_Shdr shdr, shdr_prog;
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700443 Elf_Data *data, *data_prog, *data_maps = NULL, *symbols = NULL;
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200444 char *shname, *shname_prog;
445 int nr_maps = 0;
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800446
Mickaël Salaüna734fb52017-02-08 21:27:43 +0100447 /* reset global variables */
448 kern_version = 0;
449 memset(license, 0, sizeof(license));
450 memset(processed_sec, 0, sizeof(processed_sec));
451
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800452 if (elf_version(EV_CURRENT) == EV_NONE)
453 return 1;
454
455 fd = open(path, O_RDONLY, 0);
456 if (fd < 0)
457 return 1;
458
459 elf = elf_begin(fd, ELF_C_READ, NULL);
460
461 if (!elf)
462 return 1;
463
464 if (gelf_getehdr(elf, &ehdr) != &ehdr)
465 return 1;
466
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700467 /* clear all kprobes */
468 i = system("echo \"\" > /sys/kernel/debug/tracing/kprobe_events");
469
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800470 /* scan over all elf sections to get license and map info */
471 for (i = 1; i < ehdr.e_shnum; i++) {
472
473 if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
474 continue;
475
476 if (0) /* helpful for llvm debugging */
477 printf("section %d:%s data %p size %zd link %d flags %d\n",
478 i, shname, data->d_buf, data->d_size,
479 shdr.sh_link, (int) shdr.sh_flags);
480
481 if (strcmp(shname, "license") == 0) {
482 processed_sec[i] = true;
483 memcpy(license, data->d_buf, data->d_size);
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700484 } else if (strcmp(shname, "version") == 0) {
485 processed_sec[i] = true;
486 if (data->d_size != sizeof(int)) {
487 printf("invalid size of version section %zd\n",
488 data->d_size);
489 return 1;
490 }
491 memcpy(&kern_version, data->d_buf, sizeof(int));
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800492 } else if (strcmp(shname, "maps") == 0) {
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200493 int j;
494
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700495 maps_shndx = i;
496 data_maps = data;
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200497 for (j = 0; j < MAX_MAPS; j++)
498 map_data[j].fd = -1;
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800499 } else if (shdr.sh_type == SHT_SYMTAB) {
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700500 strtabidx = shdr.sh_link;
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800501 symbols = data;
502 }
503 }
504
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700505 ret = 1;
506
507 if (!symbols) {
508 printf("missing SHT_SYMTAB section\n");
509 goto done;
510 }
511
512 if (data_maps) {
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200513 nr_maps = load_elf_maps_section(map_data, maps_shndx,
514 elf, symbols, strtabidx);
515 if (nr_maps < 0) {
516 printf("Error: Failed loading ELF maps (errno:%d):%s\n",
517 nr_maps, strerror(-nr_maps));
Jesper Dangaard Brouer5010e942017-04-28 16:25:04 +0200518 ret = 1;
519 goto done;
520 }
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200521 if (load_maps(map_data, nr_maps, fixup_map))
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700522 goto done;
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200523 map_data_count = nr_maps;
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700524
525 processed_sec[maps_shndx] = true;
526 }
527
Jesper Dangaard Brouer7bc57952017-05-30 14:37:51 +0200528 /* process all relo sections, and rewrite bpf insns for maps */
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800529 for (i = 1; i < ehdr.e_shnum; i++) {
Mickaël Salaün16ad1322017-02-08 21:27:42 +0100530 if (processed_sec[i])
531 continue;
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800532
533 if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
534 continue;
Jesper Dangaard Brouer7bc57952017-05-30 14:37:51 +0200535
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800536 if (shdr.sh_type == SHT_REL) {
537 struct bpf_insn *insns;
538
Jesper Dangaard Brouer7bc57952017-05-30 14:37:51 +0200539 /* locate prog sec that need map fixup (relocations) */
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800540 if (get_sec(elf, shdr.sh_info, &ehdr, &shname_prog,
541 &shdr_prog, &data_prog))
542 continue;
543
Alexei Starovoitovdb6a71d2016-11-22 16:52:09 -0800544 if (shdr_prog.sh_type != SHT_PROGBITS ||
545 !(shdr_prog.sh_flags & SHF_EXECINSTR))
546 continue;
547
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800548 insns = (struct bpf_insn *) data_prog->d_buf;
Jesper Dangaard Brouer7bc57952017-05-30 14:37:51 +0200549 processed_sec[i] = true; /* relo section */
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800550
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200551 if (parse_relo_and_apply(data, symbols, &shdr, insns,
552 map_data, nr_maps))
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800553 continue;
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800554 }
555 }
556
Jesper Dangaard Brouer7bc57952017-05-30 14:37:51 +0200557 /* load programs */
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800558 for (i = 1; i < ehdr.e_shnum; i++) {
559
560 if (processed_sec[i])
561 continue;
562
563 if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
564 continue;
565
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700566 if (memcmp(shname, "kprobe/", 7) == 0 ||
567 memcmp(shname, "kretprobe/", 10) == 0 ||
Alexei Starovoitovc0766042016-04-06 18:43:29 -0700568 memcmp(shname, "tracepoint/", 11) == 0 ||
Brenden Blanco86af8b42016-07-19 12:16:51 -0700569 memcmp(shname, "xdp", 3) == 0 ||
Alexei Starovoitov1c47910e2016-09-01 18:37:25 -0700570 memcmp(shname, "perf_event", 10) == 0 ||
David Ahern4f2e7ae2016-12-01 08:48:07 -0800571 memcmp(shname, "socket", 6) == 0 ||
Lawrence Brakmo40304b22017-06-30 20:02:40 -0700572 memcmp(shname, "cgroup/", 7) == 0 ||
John Fastabend69e8cc12017-08-15 22:33:32 -0700573 memcmp(shname, "sockops", 7) == 0 ||
574 memcmp(shname, "sk_skb", 6) == 0) {
Lawrence Brakmof856e462017-07-04 15:57:50 -0700575 ret = load_and_attach(shname, data->d_buf,
576 data->d_size);
577 if (ret != 0)
578 goto done;
579 }
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800580 }
581
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700582 ret = 0;
583done:
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800584 close(fd);
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700585 return ret;
586}
587
588int load_bpf_file(char *path)
589{
590 return do_load_bpf_file(path, NULL);
591}
592
593int load_bpf_file_fixup_map(const char *path, fixup_map_cb fixup_map)
594{
595 return do_load_bpf_file(path, fixup_map);
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800596}
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700597
598void read_trace_pipe(void)
599{
600 int trace_fd;
601
602 trace_fd = open(DEBUGFS "trace_pipe", O_RDONLY, 0);
603 if (trace_fd < 0)
604 return;
605
606 while (1) {
607 static char buf[4096];
608 ssize_t sz;
609
610 sz = read(trace_fd, buf, sizeof(buf));
611 if (sz > 0) {
612 buf[sz] = 0;
613 puts(buf);
614 }
615 }
616}
Alexei Starovoitov3622e7e2016-03-07 21:57:19 -0800617
618#define MAX_SYMS 300000
619static struct ksym syms[MAX_SYMS];
620static int sym_cnt;
621
622static int ksym_cmp(const void *p1, const void *p2)
623{
624 return ((struct ksym *)p1)->addr - ((struct ksym *)p2)->addr;
625}
626
627int load_kallsyms(void)
628{
629 FILE *f = fopen("/proc/kallsyms", "r");
630 char func[256], buf[256];
631 char symbol;
632 void *addr;
633 int i = 0;
634
635 if (!f)
636 return -ENOENT;
637
638 while (!feof(f)) {
639 if (!fgets(buf, sizeof(buf), f))
640 break;
641 if (sscanf(buf, "%p %c %s", &addr, &symbol, func) != 3)
642 break;
643 if (!addr)
644 continue;
645 syms[i].addr = (long) addr;
646 syms[i].name = strdup(func);
647 i++;
648 }
649 sym_cnt = i;
650 qsort(syms, sym_cnt, sizeof(struct ksym), ksym_cmp);
651 return 0;
652}
653
654struct ksym *ksym_search(long key)
655{
656 int start = 0, end = sym_cnt;
657 int result;
658
659 while (start < end) {
660 size_t mid = start + (end - start) / 2;
661
662 result = key - syms[mid].addr;
663 if (result < 0)
664 end = mid;
665 else if (result > 0)
666 start = mid + 1;
667 else
668 return &syms[mid];
669 }
670
671 if (start >= 1 && syms[start - 1].addr < key &&
672 key < syms[start].addr)
673 /* valid ksym */
674 return &syms[start - 1];
675
676 /* out of range. return _stext */
677 return &syms[0];
678}
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -0800679
Jesper Dangaard Brouer6387d012017-05-01 11:26:15 +0200680int set_link_xdp_fd(int ifindex, int fd, __u32 flags)
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -0800681{
682 struct sockaddr_nl sa;
683 int sock, seq = 0, len, ret = -1;
684 char buf[4096];
685 struct nlattr *nla, *nla_xdp;
686 struct {
687 struct nlmsghdr nh;
688 struct ifinfomsg ifinfo;
689 char attrbuf[64];
690 } req;
691 struct nlmsghdr *nh;
692 struct nlmsgerr *err;
693
694 memset(&sa, 0, sizeof(sa));
695 sa.nl_family = AF_NETLINK;
696
697 sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
698 if (sock < 0) {
699 printf("open netlink socket: %s\n", strerror(errno));
700 return -1;
701 }
702
703 if (bind(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
704 printf("bind to netlink: %s\n", strerror(errno));
705 goto cleanup;
706 }
707
708 memset(&req, 0, sizeof(req));
709 req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
710 req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
711 req.nh.nlmsg_type = RTM_SETLINK;
712 req.nh.nlmsg_pid = 0;
713 req.nh.nlmsg_seq = ++seq;
714 req.ifinfo.ifi_family = AF_UNSPEC;
715 req.ifinfo.ifi_index = ifindex;
David Ahern3993f2c2017-04-27 09:11:13 -0700716
717 /* started nested attribute for XDP */
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -0800718 nla = (struct nlattr *)(((char *)&req)
719 + NLMSG_ALIGN(req.nh.nlmsg_len));
720 nla->nla_type = NLA_F_NESTED | 43/*IFLA_XDP*/;
David Ahern3993f2c2017-04-27 09:11:13 -0700721 nla->nla_len = NLA_HDRLEN;
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -0800722
David Ahern3993f2c2017-04-27 09:11:13 -0700723 /* add XDP fd */
724 nla_xdp = (struct nlattr *)((char *)nla + nla->nla_len);
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -0800725 nla_xdp->nla_type = 1/*IFLA_XDP_FD*/;
726 nla_xdp->nla_len = NLA_HDRLEN + sizeof(int);
727 memcpy((char *)nla_xdp + NLA_HDRLEN, &fd, sizeof(fd));
David Ahern3993f2c2017-04-27 09:11:13 -0700728 nla->nla_len += nla_xdp->nla_len;
729
730 /* if user passed in any flags, add those too */
731 if (flags) {
732 nla_xdp = (struct nlattr *)((char *)nla + nla->nla_len);
733 nla_xdp->nla_type = 3/*IFLA_XDP_FLAGS*/;
734 nla_xdp->nla_len = NLA_HDRLEN + sizeof(flags);
735 memcpy((char *)nla_xdp + NLA_HDRLEN, &flags, sizeof(flags));
736 nla->nla_len += nla_xdp->nla_len;
737 }
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -0800738
739 req.nh.nlmsg_len += NLA_ALIGN(nla->nla_len);
740
741 if (send(sock, &req, req.nh.nlmsg_len, 0) < 0) {
742 printf("send to netlink: %s\n", strerror(errno));
743 goto cleanup;
744 }
745
746 len = recv(sock, buf, sizeof(buf), 0);
747 if (len < 0) {
748 printf("recv from netlink: %s\n", strerror(errno));
749 goto cleanup;
750 }
751
752 for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len);
753 nh = NLMSG_NEXT(nh, len)) {
754 if (nh->nlmsg_pid != getpid()) {
755 printf("Wrong pid %d, expected %d\n",
756 nh->nlmsg_pid, getpid());
757 goto cleanup;
758 }
759 if (nh->nlmsg_seq != seq) {
760 printf("Wrong seq %d, expected %d\n",
761 nh->nlmsg_seq, seq);
762 goto cleanup;
763 }
764 switch (nh->nlmsg_type) {
765 case NLMSG_ERROR:
766 err = (struct nlmsgerr *)NLMSG_DATA(nh);
767 if (!err->error)
768 continue;
769 printf("nlmsg error %s\n", strerror(-err->error));
770 goto cleanup;
771 case NLMSG_DONE:
772 break;
773 }
774 }
775
776 ret = 0;
777
778cleanup:
779 close(sock);
780 return ret;
781}