blob: dcdce1270d38617af84087f532ba16a402d46124 [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>
Alexei Starovoitov249b8122014-12-01 15:06:37 -080024#include "libbpf.h"
Alexei Starovoitov249b8122014-12-01 15:06:37 -080025#include "bpf_load.h"
Joe Stringer205c8ad2016-12-08 18:46:19 -080026#include "perf-sys.h"
Alexei Starovoitov249b8122014-12-01 15:06:37 -080027
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070028#define DEBUGFS "/sys/kernel/debug/tracing/"
29
Alexei Starovoitov249b8122014-12-01 15:06:37 -080030static char license[128];
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070031static int kern_version;
Alexei Starovoitov249b8122014-12-01 15:06:37 -080032static bool processed_sec[128];
Joe Stringerd40fc182016-12-14 14:43:38 -080033char bpf_log_buf[BPF_LOG_BUF_SIZE];
Alexei Starovoitov249b8122014-12-01 15:06:37 -080034int map_fd[MAX_MAPS];
35int prog_fd[MAX_PROGS];
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070036int event_fd[MAX_PROGS];
Alexei Starovoitov249b8122014-12-01 15:06:37 -080037int prog_cnt;
Alexei Starovoitov5bacd782015-05-19 16:59:05 -070038int prog_array_fd = -1;
39
Joe Stringerd40fc182016-12-14 14:43:38 -080040struct bpf_map_def {
41 unsigned int type;
42 unsigned int key_size;
43 unsigned int value_size;
44 unsigned int max_entries;
45 unsigned int map_flags;
Martin KaFai Laufb30d4b2017-03-22 10:00:35 -070046 unsigned int inner_map_idx;
Joe Stringerd40fc182016-12-14 14:43:38 -080047};
48
Alexei Starovoitov5bacd782015-05-19 16:59:05 -070049static int populate_prog_array(const char *event, int prog_fd)
50{
51 int ind = atoi(event), err;
52
Joe Stringerd40fc182016-12-14 14:43:38 -080053 err = bpf_map_update_elem(prog_array_fd, &ind, &prog_fd, BPF_ANY);
Alexei Starovoitov5bacd782015-05-19 16:59:05 -070054 if (err < 0) {
55 printf("failed to store prog_fd in prog_array\n");
56 return -1;
57 }
58 return 0;
59}
Alexei Starovoitov249b8122014-12-01 15:06:37 -080060
61static int load_and_attach(const char *event, struct bpf_insn *prog, int size)
62{
Alexei Starovoitov249b8122014-12-01 15:06:37 -080063 bool is_socket = strncmp(event, "socket", 6) == 0;
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070064 bool is_kprobe = strncmp(event, "kprobe/", 7) == 0;
65 bool is_kretprobe = strncmp(event, "kretprobe/", 10) == 0;
Alexei Starovoitovc0766042016-04-06 18:43:29 -070066 bool is_tracepoint = strncmp(event, "tracepoint/", 11) == 0;
Brenden Blanco86af8b42016-07-19 12:16:51 -070067 bool is_xdp = strncmp(event, "xdp", 3) == 0;
Alexei Starovoitov1c47910e2016-09-01 18:37:25 -070068 bool is_perf_event = strncmp(event, "perf_event", 10) == 0;
David Ahern4f2e7ae2016-12-01 08:48:07 -080069 bool is_cgroup_skb = strncmp(event, "cgroup/skb", 10) == 0;
70 bool is_cgroup_sk = strncmp(event, "cgroup/sock", 11) == 0;
Joe Stringer43371c82016-12-14 14:43:39 -080071 size_t insns_cnt = size / sizeof(struct bpf_insn);
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070072 enum bpf_prog_type prog_type;
73 char buf[256];
74 int fd, efd, err, id;
75 struct perf_event_attr attr = {};
Alexei Starovoitov249b8122014-12-01 15:06:37 -080076
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070077 attr.type = PERF_TYPE_TRACEPOINT;
78 attr.sample_type = PERF_SAMPLE_RAW;
79 attr.sample_period = 1;
80 attr.wakeup_events = 1;
81
82 if (is_socket) {
83 prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
84 } else if (is_kprobe || is_kretprobe) {
85 prog_type = BPF_PROG_TYPE_KPROBE;
Alexei Starovoitovc0766042016-04-06 18:43:29 -070086 } else if (is_tracepoint) {
87 prog_type = BPF_PROG_TYPE_TRACEPOINT;
Brenden Blanco86af8b42016-07-19 12:16:51 -070088 } else if (is_xdp) {
89 prog_type = BPF_PROG_TYPE_XDP;
Alexei Starovoitov1c47910e2016-09-01 18:37:25 -070090 } else if (is_perf_event) {
91 prog_type = BPF_PROG_TYPE_PERF_EVENT;
David Ahern4f2e7ae2016-12-01 08:48:07 -080092 } else if (is_cgroup_skb) {
93 prog_type = BPF_PROG_TYPE_CGROUP_SKB;
94 } else if (is_cgroup_sk) {
95 prog_type = BPF_PROG_TYPE_CGROUP_SOCK;
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070096 } else {
97 printf("Unknown event '%s'\n", event);
Alexei Starovoitov249b8122014-12-01 15:06:37 -080098 return -1;
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070099 }
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800100
Joe Stringer43371c82016-12-14 14:43:39 -0800101 fd = bpf_load_program(prog_type, prog, insns_cnt, license, kern_version,
Joe Stringerd40fc182016-12-14 14:43:38 -0800102 bpf_log_buf, BPF_LOG_BUF_SIZE);
Alexei Starovoitov5bacd782015-05-19 16:59:05 -0700103 if (fd < 0) {
Joe Stringerd40fc182016-12-14 14:43:38 -0800104 printf("bpf_load_program() err=%d\n%s", errno, bpf_log_buf);
Alexei Starovoitov5bacd782015-05-19 16:59:05 -0700105 return -1;
106 }
107
108 prog_fd[prog_cnt++] = fd;
109
David Ahern4f2e7ae2016-12-01 08:48:07 -0800110 if (is_xdp || is_perf_event || is_cgroup_skb || is_cgroup_sk)
Brenden Blanco86af8b42016-07-19 12:16:51 -0700111 return 0;
112
Alexei Starovoitov5bacd782015-05-19 16:59:05 -0700113 if (is_socket) {
114 event += 6;
115 if (*event != '/')
116 return 0;
117 event++;
118 if (!isdigit(*event)) {
119 printf("invalid prog number\n");
120 return -1;
121 }
122 return populate_prog_array(event, fd);
123 }
124
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700125 if (is_kprobe || is_kretprobe) {
126 if (is_kprobe)
127 event += 7;
128 else
129 event += 10;
130
Alexei Starovoitov5bacd782015-05-19 16:59:05 -0700131 if (*event == 0) {
132 printf("event name cannot be empty\n");
133 return -1;
134 }
135
136 if (isdigit(*event))
137 return populate_prog_array(event, fd);
138
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700139 snprintf(buf, sizeof(buf),
140 "echo '%c:%s %s' >> /sys/kernel/debug/tracing/kprobe_events",
141 is_kprobe ? 'p' : 'r', event, event);
142 err = system(buf);
143 if (err < 0) {
144 printf("failed to create kprobe '%s' error '%s'\n",
145 event, strerror(errno));
146 return -1;
147 }
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700148
Alexei Starovoitovc0766042016-04-06 18:43:29 -0700149 strcpy(buf, DEBUGFS);
150 strcat(buf, "events/kprobes/");
151 strcat(buf, event);
152 strcat(buf, "/id");
153 } else if (is_tracepoint) {
154 event += 11;
155
156 if (*event == 0) {
157 printf("event name cannot be empty\n");
158 return -1;
159 }
160 strcpy(buf, DEBUGFS);
161 strcat(buf, "events/");
162 strcat(buf, event);
163 strcat(buf, "/id");
164 }
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700165
166 efd = open(buf, O_RDONLY, 0);
167 if (efd < 0) {
168 printf("failed to open event %s\n", event);
169 return -1;
170 }
171
172 err = read(efd, buf, sizeof(buf));
173 if (err < 0 || err >= sizeof(buf)) {
174 printf("read from '%s' failed '%s'\n", event, strerror(errno));
175 return -1;
176 }
177
178 close(efd);
179
180 buf[err] = 0;
181 id = atoi(buf);
182 attr.config = id;
183
Joe Stringer205c8ad2016-12-08 18:46:19 -0800184 efd = sys_perf_event_open(&attr, -1/*pid*/, 0/*cpu*/, -1/*group_fd*/, 0);
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700185 if (efd < 0) {
186 printf("event %d fd %d err %s\n", id, efd, strerror(errno));
187 return -1;
188 }
189 event_fd[prog_cnt - 1] = efd;
190 ioctl(efd, PERF_EVENT_IOC_ENABLE, 0);
191 ioctl(efd, PERF_EVENT_IOC_SET_BPF, fd);
192
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800193 return 0;
194}
195
196static int load_maps(struct bpf_map_def *maps, int len)
197{
198 int i;
199
200 for (i = 0; i < len / sizeof(struct bpf_map_def); i++) {
201
Martin KaFai Laufb30d4b2017-03-22 10:00:35 -0700202 if (maps[i].type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
203 maps[i].type == BPF_MAP_TYPE_HASH_OF_MAPS) {
204 int inner_map_fd = map_fd[maps[i].inner_map_idx];
205
206 map_fd[i] = bpf_create_map_in_map(maps[i].type,
207 maps[i].key_size,
208 inner_map_fd,
209 maps[i].max_entries,
210 maps[i].map_flags);
211 } else {
212 map_fd[i] = bpf_create_map(maps[i].type,
213 maps[i].key_size,
214 maps[i].value_size,
215 maps[i].max_entries,
216 maps[i].map_flags);
217 }
Alexei Starovoitov618ec9a72016-03-07 21:57:18 -0800218 if (map_fd[i] < 0) {
219 printf("failed to create a map: %d %s\n",
220 errno, strerror(errno));
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800221 return 1;
Alexei Starovoitov618ec9a72016-03-07 21:57:18 -0800222 }
Alexei Starovoitov5bacd782015-05-19 16:59:05 -0700223
224 if (maps[i].type == BPF_MAP_TYPE_PROG_ARRAY)
225 prog_array_fd = map_fd[i];
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800226 }
227 return 0;
228}
229
230static int get_sec(Elf *elf, int i, GElf_Ehdr *ehdr, char **shname,
231 GElf_Shdr *shdr, Elf_Data **data)
232{
233 Elf_Scn *scn;
234
235 scn = elf_getscn(elf, i);
236 if (!scn)
237 return 1;
238
239 if (gelf_getshdr(scn, shdr) != shdr)
240 return 2;
241
242 *shname = elf_strptr(elf, ehdr->e_shstrndx, shdr->sh_name);
243 if (!*shname || !shdr->sh_size)
244 return 3;
245
246 *data = elf_getdata(scn, 0);
247 if (!*data || elf_getdata(scn, *data) != NULL)
248 return 4;
249
250 return 0;
251}
252
253static int parse_relo_and_apply(Elf_Data *data, Elf_Data *symbols,
254 GElf_Shdr *shdr, struct bpf_insn *insn)
255{
256 int i, nrels;
257
258 nrels = shdr->sh_size / shdr->sh_entsize;
259
260 for (i = 0; i < nrels; i++) {
261 GElf_Sym sym;
262 GElf_Rel rel;
263 unsigned int insn_idx;
264
265 gelf_getrel(data, i, &rel);
266
267 insn_idx = rel.r_offset / sizeof(struct bpf_insn);
268
269 gelf_getsym(symbols, GELF_R_SYM(rel.r_info), &sym);
270
271 if (insn[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) {
272 printf("invalid relo for insn[%d].code 0x%x\n",
273 insn_idx, insn[insn_idx].code);
274 return 1;
275 }
276 insn[insn_idx].src_reg = BPF_PSEUDO_MAP_FD;
277 insn[insn_idx].imm = map_fd[sym.st_value / sizeof(struct bpf_map_def)];
278 }
279
280 return 0;
281}
282
283int load_bpf_file(char *path)
284{
285 int fd, i;
286 Elf *elf;
287 GElf_Ehdr ehdr;
288 GElf_Shdr shdr, shdr_prog;
289 Elf_Data *data, *data_prog, *symbols = NULL;
290 char *shname, *shname_prog;
291
Mickaël Salaüna734fb52017-02-08 21:27:43 +0100292 /* reset global variables */
293 kern_version = 0;
294 memset(license, 0, sizeof(license));
295 memset(processed_sec, 0, sizeof(processed_sec));
296
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800297 if (elf_version(EV_CURRENT) == EV_NONE)
298 return 1;
299
300 fd = open(path, O_RDONLY, 0);
301 if (fd < 0)
302 return 1;
303
304 elf = elf_begin(fd, ELF_C_READ, NULL);
305
306 if (!elf)
307 return 1;
308
309 if (gelf_getehdr(elf, &ehdr) != &ehdr)
310 return 1;
311
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700312 /* clear all kprobes */
313 i = system("echo \"\" > /sys/kernel/debug/tracing/kprobe_events");
314
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800315 /* scan over all elf sections to get license and map info */
316 for (i = 1; i < ehdr.e_shnum; i++) {
317
318 if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
319 continue;
320
321 if (0) /* helpful for llvm debugging */
322 printf("section %d:%s data %p size %zd link %d flags %d\n",
323 i, shname, data->d_buf, data->d_size,
324 shdr.sh_link, (int) shdr.sh_flags);
325
326 if (strcmp(shname, "license") == 0) {
327 processed_sec[i] = true;
328 memcpy(license, data->d_buf, data->d_size);
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700329 } else if (strcmp(shname, "version") == 0) {
330 processed_sec[i] = true;
331 if (data->d_size != sizeof(int)) {
332 printf("invalid size of version section %zd\n",
333 data->d_size);
334 return 1;
335 }
336 memcpy(&kern_version, data->d_buf, sizeof(int));
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800337 } else if (strcmp(shname, "maps") == 0) {
338 processed_sec[i] = true;
339 if (load_maps(data->d_buf, data->d_size))
340 return 1;
341 } else if (shdr.sh_type == SHT_SYMTAB) {
342 symbols = data;
343 }
344 }
345
346 /* load programs that need map fixup (relocations) */
347 for (i = 1; i < ehdr.e_shnum; i++) {
Mickaël Salaün16ad1322017-02-08 21:27:42 +0100348 if (processed_sec[i])
349 continue;
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800350
351 if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
352 continue;
353 if (shdr.sh_type == SHT_REL) {
354 struct bpf_insn *insns;
355
356 if (get_sec(elf, shdr.sh_info, &ehdr, &shname_prog,
357 &shdr_prog, &data_prog))
358 continue;
359
Alexei Starovoitovdb6a71d2016-11-22 16:52:09 -0800360 if (shdr_prog.sh_type != SHT_PROGBITS ||
361 !(shdr_prog.sh_flags & SHF_EXECINSTR))
362 continue;
363
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800364 insns = (struct bpf_insn *) data_prog->d_buf;
365
366 processed_sec[shdr.sh_info] = true;
367 processed_sec[i] = true;
368
369 if (parse_relo_and_apply(data, symbols, &shdr, insns))
370 continue;
371
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700372 if (memcmp(shname_prog, "kprobe/", 7) == 0 ||
373 memcmp(shname_prog, "kretprobe/", 10) == 0 ||
Alexei Starovoitovc0766042016-04-06 18:43:29 -0700374 memcmp(shname_prog, "tracepoint/", 11) == 0 ||
Brenden Blanco86af8b42016-07-19 12:16:51 -0700375 memcmp(shname_prog, "xdp", 3) == 0 ||
Alexei Starovoitov1c47910e2016-09-01 18:37:25 -0700376 memcmp(shname_prog, "perf_event", 10) == 0 ||
David Ahern4f2e7ae2016-12-01 08:48:07 -0800377 memcmp(shname_prog, "socket", 6) == 0 ||
378 memcmp(shname_prog, "cgroup/", 7) == 0)
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800379 load_and_attach(shname_prog, insns, data_prog->d_size);
380 }
381 }
382
383 /* load programs that don't use maps */
384 for (i = 1; i < ehdr.e_shnum; i++) {
385
386 if (processed_sec[i])
387 continue;
388
389 if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
390 continue;
391
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700392 if (memcmp(shname, "kprobe/", 7) == 0 ||
393 memcmp(shname, "kretprobe/", 10) == 0 ||
Alexei Starovoitovc0766042016-04-06 18:43:29 -0700394 memcmp(shname, "tracepoint/", 11) == 0 ||
Brenden Blanco86af8b42016-07-19 12:16:51 -0700395 memcmp(shname, "xdp", 3) == 0 ||
Alexei Starovoitov1c47910e2016-09-01 18:37:25 -0700396 memcmp(shname, "perf_event", 10) == 0 ||
David Ahern4f2e7ae2016-12-01 08:48:07 -0800397 memcmp(shname, "socket", 6) == 0 ||
398 memcmp(shname, "cgroup/", 7) == 0)
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800399 load_and_attach(shname, data->d_buf, data->d_size);
400 }
401
402 close(fd);
403 return 0;
404}
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700405
406void read_trace_pipe(void)
407{
408 int trace_fd;
409
410 trace_fd = open(DEBUGFS "trace_pipe", O_RDONLY, 0);
411 if (trace_fd < 0)
412 return;
413
414 while (1) {
415 static char buf[4096];
416 ssize_t sz;
417
418 sz = read(trace_fd, buf, sizeof(buf));
419 if (sz > 0) {
420 buf[sz] = 0;
421 puts(buf);
422 }
423 }
424}
Alexei Starovoitov3622e7e2016-03-07 21:57:19 -0800425
426#define MAX_SYMS 300000
427static struct ksym syms[MAX_SYMS];
428static int sym_cnt;
429
430static int ksym_cmp(const void *p1, const void *p2)
431{
432 return ((struct ksym *)p1)->addr - ((struct ksym *)p2)->addr;
433}
434
435int load_kallsyms(void)
436{
437 FILE *f = fopen("/proc/kallsyms", "r");
438 char func[256], buf[256];
439 char symbol;
440 void *addr;
441 int i = 0;
442
443 if (!f)
444 return -ENOENT;
445
446 while (!feof(f)) {
447 if (!fgets(buf, sizeof(buf), f))
448 break;
449 if (sscanf(buf, "%p %c %s", &addr, &symbol, func) != 3)
450 break;
451 if (!addr)
452 continue;
453 syms[i].addr = (long) addr;
454 syms[i].name = strdup(func);
455 i++;
456 }
457 sym_cnt = i;
458 qsort(syms, sym_cnt, sizeof(struct ksym), ksym_cmp);
459 return 0;
460}
461
462struct ksym *ksym_search(long key)
463{
464 int start = 0, end = sym_cnt;
465 int result;
466
467 while (start < end) {
468 size_t mid = start + (end - start) / 2;
469
470 result = key - syms[mid].addr;
471 if (result < 0)
472 end = mid;
473 else if (result > 0)
474 start = mid + 1;
475 else
476 return &syms[mid];
477 }
478
479 if (start >= 1 && syms[start - 1].addr < key &&
480 key < syms[start].addr)
481 /* valid ksym */
482 return &syms[start - 1];
483
484 /* out of range. return _stext */
485 return &syms[0];
486}
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -0800487
488int set_link_xdp_fd(int ifindex, int fd)
489{
490 struct sockaddr_nl sa;
491 int sock, seq = 0, len, ret = -1;
492 char buf[4096];
493 struct nlattr *nla, *nla_xdp;
494 struct {
495 struct nlmsghdr nh;
496 struct ifinfomsg ifinfo;
497 char attrbuf[64];
498 } req;
499 struct nlmsghdr *nh;
500 struct nlmsgerr *err;
501
502 memset(&sa, 0, sizeof(sa));
503 sa.nl_family = AF_NETLINK;
504
505 sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
506 if (sock < 0) {
507 printf("open netlink socket: %s\n", strerror(errno));
508 return -1;
509 }
510
511 if (bind(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
512 printf("bind to netlink: %s\n", strerror(errno));
513 goto cleanup;
514 }
515
516 memset(&req, 0, sizeof(req));
517 req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
518 req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
519 req.nh.nlmsg_type = RTM_SETLINK;
520 req.nh.nlmsg_pid = 0;
521 req.nh.nlmsg_seq = ++seq;
522 req.ifinfo.ifi_family = AF_UNSPEC;
523 req.ifinfo.ifi_index = ifindex;
524 nla = (struct nlattr *)(((char *)&req)
525 + NLMSG_ALIGN(req.nh.nlmsg_len));
526 nla->nla_type = NLA_F_NESTED | 43/*IFLA_XDP*/;
527
528 nla_xdp = (struct nlattr *)((char *)nla + NLA_HDRLEN);
529 nla_xdp->nla_type = 1/*IFLA_XDP_FD*/;
530 nla_xdp->nla_len = NLA_HDRLEN + sizeof(int);
531 memcpy((char *)nla_xdp + NLA_HDRLEN, &fd, sizeof(fd));
532 nla->nla_len = NLA_HDRLEN + nla_xdp->nla_len;
533
534 req.nh.nlmsg_len += NLA_ALIGN(nla->nla_len);
535
536 if (send(sock, &req, req.nh.nlmsg_len, 0) < 0) {
537 printf("send to netlink: %s\n", strerror(errno));
538 goto cleanup;
539 }
540
541 len = recv(sock, buf, sizeof(buf), 0);
542 if (len < 0) {
543 printf("recv from netlink: %s\n", strerror(errno));
544 goto cleanup;
545 }
546
547 for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len);
548 nh = NLMSG_NEXT(nh, len)) {
549 if (nh->nlmsg_pid != getpid()) {
550 printf("Wrong pid %d, expected %d\n",
551 nh->nlmsg_pid, getpid());
552 goto cleanup;
553 }
554 if (nh->nlmsg_seq != seq) {
555 printf("Wrong seq %d, expected %d\n",
556 nh->nlmsg_seq, seq);
557 goto cleanup;
558 }
559 switch (nh->nlmsg_type) {
560 case NLMSG_ERROR:
561 err = (struct nlmsgerr *)NLMSG_DATA(nh);
562 if (!err->error)
563 continue;
564 printf("nlmsg error %s\n", strerror(-err->error));
565 goto cleanup;
566 case NLMSG_DONE:
567 break;
568 }
569 }
570
571 ret = 0;
572
573cleanup:
574 close(sock);
575 return ret;
576}