]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - samples/bpf/bpf_load.c
Merge branch 'xdp-netlink-ext-ack'
[mirror_ubuntu-artful-kernel.git] / samples / bpf / bpf_load.c
CommitLineData
249b812d
AS
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>
b896c4f9 11#include <stdlib.h>
249b812d
AS
12#include <linux/bpf.h>
13#include <linux/filter.h>
b896c4f9 14#include <linux/perf_event.h>
12d8bb64
MKL
15#include <linux/netlink.h>
16#include <linux/rtnetlink.h>
17#include <sys/types.h>
18#include <sys/socket.h>
b896c4f9
AS
19#include <sys/syscall.h>
20#include <sys/ioctl.h>
21#include <sys/mman.h>
22#include <poll.h>
5bacd780 23#include <ctype.h>
9fd63d05 24#include <assert.h>
249b812d 25#include "libbpf.h"
249b812d 26#include "bpf_load.h"
205c8ada 27#include "perf-sys.h"
249b812d 28
b896c4f9
AS
29#define DEBUGFS "/sys/kernel/debug/tracing/"
30
249b812d 31static char license[128];
b896c4f9 32static int kern_version;
249b812d 33static bool processed_sec[128];
d40fc181 34char bpf_log_buf[BPF_LOG_BUF_SIZE];
249b812d
AS
35int map_fd[MAX_MAPS];
36int prog_fd[MAX_PROGS];
b896c4f9 37int event_fd[MAX_PROGS];
249b812d 38int prog_cnt;
5bacd780
AS
39int prog_array_fd = -1;
40
41static int populate_prog_array(const char *event, int prog_fd)
42{
43 int ind = atoi(event), err;
44
d40fc181 45 err = bpf_map_update_elem(prog_array_fd, &ind, &prog_fd, BPF_ANY);
5bacd780
AS
46 if (err < 0) {
47 printf("failed to store prog_fd in prog_array\n");
48 return -1;
49 }
50 return 0;
51}
249b812d
AS
52
53static int load_and_attach(const char *event, struct bpf_insn *prog, int size)
54{
249b812d 55 bool is_socket = strncmp(event, "socket", 6) == 0;
b896c4f9
AS
56 bool is_kprobe = strncmp(event, "kprobe/", 7) == 0;
57 bool is_kretprobe = strncmp(event, "kretprobe/", 10) == 0;
c0766040 58 bool is_tracepoint = strncmp(event, "tracepoint/", 11) == 0;
86af8b41 59 bool is_xdp = strncmp(event, "xdp", 3) == 0;
1c47910e 60 bool is_perf_event = strncmp(event, "perf_event", 10) == 0;
4f2e7ae5
DA
61 bool is_cgroup_skb = strncmp(event, "cgroup/skb", 10) == 0;
62 bool is_cgroup_sk = strncmp(event, "cgroup/sock", 11) == 0;
43371c83 63 size_t insns_cnt = size / sizeof(struct bpf_insn);
b896c4f9
AS
64 enum bpf_prog_type prog_type;
65 char buf[256];
66 int fd, efd, err, id;
67 struct perf_event_attr attr = {};
68
69 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;
c0766040
AS
78 } else if (is_tracepoint) {
79 prog_type = BPF_PROG_TYPE_TRACEPOINT;
86af8b41
BB
80 } else if (is_xdp) {
81 prog_type = BPF_PROG_TYPE_XDP;
1c47910e
AS
82 } else if (is_perf_event) {
83 prog_type = BPF_PROG_TYPE_PERF_EVENT;
4f2e7ae5
DA
84 } 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;
b896c4f9
AS
88 } else {
89 printf("Unknown event '%s'\n", event);
249b812d 90 return -1;
b896c4f9
AS
91 }
92
43371c83 93 fd = bpf_load_program(prog_type, prog, insns_cnt, license, kern_version,
d40fc181 94 bpf_log_buf, BPF_LOG_BUF_SIZE);
5bacd780 95 if (fd < 0) {
d40fc181 96 printf("bpf_load_program() err=%d\n%s", errno, bpf_log_buf);
5bacd780
AS
97 return -1;
98 }
99
100 prog_fd[prog_cnt++] = fd;
101
4f2e7ae5 102 if (is_xdp || is_perf_event || is_cgroup_skb || is_cgroup_sk)
86af8b41
BB
103 return 0;
104
5bacd780
AS
105 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
b896c4f9
AS
117 if (is_kprobe || is_kretprobe) {
118 if (is_kprobe)
119 event += 7;
120 else
121 event += 10;
122
5bacd780
AS
123 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
b896c4f9
AS
131 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 }
249b812d 140
c0766040
AS
141 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 }
b896c4f9
AS
157
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
205c8ada 176 efd = sys_perf_event_open(&attr, -1/*pid*/, 0/*cpu*/, -1/*group_fd*/, 0);
b896c4f9
AS
177 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
249b812d
AS
185 return 0;
186}
187
5010e948 188static int load_maps(struct bpf_map_def *maps, int nr_maps,
9fd63d05 189 const char **map_names, fixup_map_cb fixup_map)
249b812d
AS
190{
191 int i;
5010e948
JDB
192 /*
193 * Warning: Using "maps" pointing to ELF data_maps->d_buf as
194 * an array of struct bpf_map_def is a wrong assumption about
195 * the ELF maps section format.
196 */
197 for (i = 0; i < nr_maps; i++) {
9fd63d05
MKL
198 if (fixup_map)
199 fixup_map(&maps[i], map_names[i], i);
249b812d 200
fb30d4b7
MKL
201 if (maps[i].type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
202 maps[i].type == BPF_MAP_TYPE_HASH_OF_MAPS) {
203 int inner_map_fd = map_fd[maps[i].inner_map_idx];
204
205 map_fd[i] = bpf_create_map_in_map(maps[i].type,
206 maps[i].key_size,
207 inner_map_fd,
208 maps[i].max_entries,
209 maps[i].map_flags);
210 } else {
211 map_fd[i] = bpf_create_map(maps[i].type,
212 maps[i].key_size,
213 maps[i].value_size,
214 maps[i].max_entries,
215 maps[i].map_flags);
216 }
618ec9a7
AS
217 if (map_fd[i] < 0) {
218 printf("failed to create a map: %d %s\n",
219 errno, strerror(errno));
249b812d 220 return 1;
618ec9a7 221 }
5bacd780
AS
222
223 if (maps[i].type == BPF_MAP_TYPE_PROG_ARRAY)
224 prog_array_fd = map_fd[i];
249b812d
AS
225 }
226 return 0;
227}
228
229static int get_sec(Elf *elf, int i, GElf_Ehdr *ehdr, char **shname,
230 GElf_Shdr *shdr, Elf_Data **data)
231{
232 Elf_Scn *scn;
233
234 scn = elf_getscn(elf, i);
235 if (!scn)
236 return 1;
237
238 if (gelf_getshdr(scn, shdr) != shdr)
239 return 2;
240
241 *shname = elf_strptr(elf, ehdr->e_shstrndx, shdr->sh_name);
242 if (!*shname || !shdr->sh_size)
243 return 3;
244
245 *data = elf_getdata(scn, 0);
246 if (!*data || elf_getdata(scn, *data) != NULL)
247 return 4;
248
249 return 0;
250}
251
252static int parse_relo_and_apply(Elf_Data *data, Elf_Data *symbols,
253 GElf_Shdr *shdr, struct bpf_insn *insn)
254{
255 int i, nrels;
256
257 nrels = shdr->sh_size / shdr->sh_entsize;
258
259 for (i = 0; i < nrels; i++) {
260 GElf_Sym sym;
261 GElf_Rel rel;
262 unsigned int insn_idx;
263
264 gelf_getrel(data, i, &rel);
265
266 insn_idx = rel.r_offset / sizeof(struct bpf_insn);
267
268 gelf_getsym(symbols, GELF_R_SYM(rel.r_info), &sym);
269
270 if (insn[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) {
271 printf("invalid relo for insn[%d].code 0x%x\n",
272 insn_idx, insn[insn_idx].code);
273 return 1;
274 }
275 insn[insn_idx].src_reg = BPF_PSEUDO_MAP_FD;
5010e948
JDB
276 /*
277 * Warning: Using sizeof(struct bpf_map_def) here is a
278 * wrong assumption about ELF maps section format
279 */
249b812d
AS
280 insn[insn_idx].imm = map_fd[sym.st_value / sizeof(struct bpf_map_def)];
281 }
282
283 return 0;
284}
285
9fd63d05
MKL
286static int cmp_symbols(const void *l, const void *r)
287{
288 const GElf_Sym *lsym = (const GElf_Sym *)l;
289 const GElf_Sym *rsym = (const GElf_Sym *)r;
290
291 if (lsym->st_value < rsym->st_value)
292 return -1;
293 else if (lsym->st_value > rsym->st_value)
294 return 1;
295 else
296 return 0;
297}
298
299static int get_sorted_map_names(Elf *elf, Elf_Data *symbols, int maps_shndx,
300 int strtabidx, char **map_names)
301{
302 GElf_Sym map_symbols[MAX_MAPS];
303 int i, nr_maps = 0;
304
305 for (i = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) {
306 assert(nr_maps < MAX_MAPS);
307 if (!gelf_getsym(symbols, i, &map_symbols[nr_maps]))
308 continue;
309 if (map_symbols[nr_maps].st_shndx != maps_shndx)
310 continue;
311 nr_maps++;
312 }
313
314 qsort(map_symbols, nr_maps, sizeof(GElf_Sym), cmp_symbols);
315
316 for (i = 0; i < nr_maps; i++) {
317 char *map_name;
318
319 map_name = elf_strptr(elf, strtabidx, map_symbols[i].st_name);
320 if (!map_name) {
321 printf("cannot get map symbol\n");
5010e948 322 return -1;
9fd63d05
MKL
323 }
324
325 map_names[i] = strdup(map_name);
326 if (!map_names[i]) {
327 printf("strdup(%s): %s(%d)\n", map_name,
328 strerror(errno), errno);
5010e948 329 return -1;
9fd63d05
MKL
330 }
331 }
332
5010e948 333 return nr_maps;
9fd63d05
MKL
334}
335
336static int do_load_bpf_file(const char *path, fixup_map_cb fixup_map)
249b812d 337{
9fd63d05 338 int fd, i, ret, maps_shndx = -1, strtabidx = -1;
249b812d
AS
339 Elf *elf;
340 GElf_Ehdr ehdr;
341 GElf_Shdr shdr, shdr_prog;
9fd63d05
MKL
342 Elf_Data *data, *data_prog, *data_maps = NULL, *symbols = NULL;
343 char *shname, *shname_prog, *map_names[MAX_MAPS] = { NULL };
249b812d 344
a734fb5d
MS
345 /* reset global variables */
346 kern_version = 0;
347 memset(license, 0, sizeof(license));
348 memset(processed_sec, 0, sizeof(processed_sec));
349
249b812d
AS
350 if (elf_version(EV_CURRENT) == EV_NONE)
351 return 1;
352
353 fd = open(path, O_RDONLY, 0);
354 if (fd < 0)
355 return 1;
356
357 elf = elf_begin(fd, ELF_C_READ, NULL);
358
359 if (!elf)
360 return 1;
361
362 if (gelf_getehdr(elf, &ehdr) != &ehdr)
363 return 1;
364
b896c4f9
AS
365 /* clear all kprobes */
366 i = system("echo \"\" > /sys/kernel/debug/tracing/kprobe_events");
367
249b812d
AS
368 /* scan over all elf sections to get license and map info */
369 for (i = 1; i < ehdr.e_shnum; i++) {
370
371 if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
372 continue;
373
374 if (0) /* helpful for llvm debugging */
375 printf("section %d:%s data %p size %zd link %d flags %d\n",
376 i, shname, data->d_buf, data->d_size,
377 shdr.sh_link, (int) shdr.sh_flags);
378
379 if (strcmp(shname, "license") == 0) {
380 processed_sec[i] = true;
381 memcpy(license, data->d_buf, data->d_size);
b896c4f9
AS
382 } else if (strcmp(shname, "version") == 0) {
383 processed_sec[i] = true;
384 if (data->d_size != sizeof(int)) {
385 printf("invalid size of version section %zd\n",
386 data->d_size);
387 return 1;
388 }
389 memcpy(&kern_version, data->d_buf, sizeof(int));
249b812d 390 } else if (strcmp(shname, "maps") == 0) {
9fd63d05
MKL
391 maps_shndx = i;
392 data_maps = data;
249b812d 393 } else if (shdr.sh_type == SHT_SYMTAB) {
9fd63d05 394 strtabidx = shdr.sh_link;
249b812d
AS
395 symbols = data;
396 }
397 }
398
9fd63d05
MKL
399 ret = 1;
400
401 if (!symbols) {
402 printf("missing SHT_SYMTAB section\n");
403 goto done;
404 }
405
406 if (data_maps) {
5010e948
JDB
407 int nr_maps;
408 int prog_elf_map_sz;
409
410 nr_maps = get_sorted_map_names(elf, symbols, maps_shndx,
411 strtabidx, map_names);
412 if (nr_maps < 0)
9fd63d05
MKL
413 goto done;
414
5010e948
JDB
415 /* Deduce map struct size stored in ELF maps section */
416 prog_elf_map_sz = data_maps->d_size / nr_maps;
417 if (prog_elf_map_sz != sizeof(struct bpf_map_def)) {
418 printf("Error: ELF maps sec wrong size (%d/%lu),"
419 " old kern.o file?\n",
420 prog_elf_map_sz, sizeof(struct bpf_map_def));
421 ret = 1;
422 goto done;
423 }
424
425 if (load_maps(data_maps->d_buf, nr_maps,
9fd63d05
MKL
426 (const char **)map_names, fixup_map))
427 goto done;
428
429 processed_sec[maps_shndx] = true;
430 }
431
249b812d
AS
432 /* load programs that need map fixup (relocations) */
433 for (i = 1; i < ehdr.e_shnum; i++) {
16ad1329
MS
434 if (processed_sec[i])
435 continue;
249b812d
AS
436
437 if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
438 continue;
439 if (shdr.sh_type == SHT_REL) {
440 struct bpf_insn *insns;
441
442 if (get_sec(elf, shdr.sh_info, &ehdr, &shname_prog,
443 &shdr_prog, &data_prog))
444 continue;
445
db6a71dd
AS
446 if (shdr_prog.sh_type != SHT_PROGBITS ||
447 !(shdr_prog.sh_flags & SHF_EXECINSTR))
448 continue;
449
249b812d
AS
450 insns = (struct bpf_insn *) data_prog->d_buf;
451
452 processed_sec[shdr.sh_info] = true;
453 processed_sec[i] = true;
454
455 if (parse_relo_and_apply(data, symbols, &shdr, insns))
456 continue;
457
b896c4f9
AS
458 if (memcmp(shname_prog, "kprobe/", 7) == 0 ||
459 memcmp(shname_prog, "kretprobe/", 10) == 0 ||
c0766040 460 memcmp(shname_prog, "tracepoint/", 11) == 0 ||
86af8b41 461 memcmp(shname_prog, "xdp", 3) == 0 ||
1c47910e 462 memcmp(shname_prog, "perf_event", 10) == 0 ||
4f2e7ae5
DA
463 memcmp(shname_prog, "socket", 6) == 0 ||
464 memcmp(shname_prog, "cgroup/", 7) == 0)
249b812d
AS
465 load_and_attach(shname_prog, insns, data_prog->d_size);
466 }
467 }
468
469 /* load programs that don't use maps */
470 for (i = 1; i < ehdr.e_shnum; i++) {
471
472 if (processed_sec[i])
473 continue;
474
475 if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
476 continue;
477
b896c4f9
AS
478 if (memcmp(shname, "kprobe/", 7) == 0 ||
479 memcmp(shname, "kretprobe/", 10) == 0 ||
c0766040 480 memcmp(shname, "tracepoint/", 11) == 0 ||
86af8b41 481 memcmp(shname, "xdp", 3) == 0 ||
1c47910e 482 memcmp(shname, "perf_event", 10) == 0 ||
4f2e7ae5
DA
483 memcmp(shname, "socket", 6) == 0 ||
484 memcmp(shname, "cgroup/", 7) == 0)
249b812d
AS
485 load_and_attach(shname, data->d_buf, data->d_size);
486 }
487
9fd63d05
MKL
488 ret = 0;
489done:
490 for (i = 0; i < MAX_MAPS; i++)
491 free(map_names[i]);
249b812d 492 close(fd);
9fd63d05
MKL
493 return ret;
494}
495
496int load_bpf_file(char *path)
497{
498 return do_load_bpf_file(path, NULL);
499}
500
501int load_bpf_file_fixup_map(const char *path, fixup_map_cb fixup_map)
502{
503 return do_load_bpf_file(path, fixup_map);
249b812d 504}
b896c4f9
AS
505
506void read_trace_pipe(void)
507{
508 int trace_fd;
509
510 trace_fd = open(DEBUGFS "trace_pipe", O_RDONLY, 0);
511 if (trace_fd < 0)
512 return;
513
514 while (1) {
515 static char buf[4096];
516 ssize_t sz;
517
518 sz = read(trace_fd, buf, sizeof(buf));
519 if (sz > 0) {
520 buf[sz] = 0;
521 puts(buf);
522 }
523 }
524}
3622e7e4
AS
525
526#define MAX_SYMS 300000
527static struct ksym syms[MAX_SYMS];
528static int sym_cnt;
529
530static int ksym_cmp(const void *p1, const void *p2)
531{
532 return ((struct ksym *)p1)->addr - ((struct ksym *)p2)->addr;
533}
534
535int load_kallsyms(void)
536{
537 FILE *f = fopen("/proc/kallsyms", "r");
538 char func[256], buf[256];
539 char symbol;
540 void *addr;
541 int i = 0;
542
543 if (!f)
544 return -ENOENT;
545
546 while (!feof(f)) {
547 if (!fgets(buf, sizeof(buf), f))
548 break;
549 if (sscanf(buf, "%p %c %s", &addr, &symbol, func) != 3)
550 break;
551 if (!addr)
552 continue;
553 syms[i].addr = (long) addr;
554 syms[i].name = strdup(func);
555 i++;
556 }
557 sym_cnt = i;
558 qsort(syms, sym_cnt, sizeof(struct ksym), ksym_cmp);
559 return 0;
560}
561
562struct ksym *ksym_search(long key)
563{
564 int start = 0, end = sym_cnt;
565 int result;
566
567 while (start < end) {
568 size_t mid = start + (end - start) / 2;
569
570 result = key - syms[mid].addr;
571 if (result < 0)
572 end = mid;
573 else if (result > 0)
574 start = mid + 1;
575 else
576 return &syms[mid];
577 }
578
579 if (start >= 1 && syms[start - 1].addr < key &&
580 key < syms[start].addr)
581 /* valid ksym */
582 return &syms[start - 1];
583
584 /* out of range. return _stext */
585 return &syms[0];
586}
12d8bb64 587
3993f2cb 588int set_link_xdp_fd(int ifindex, int fd, int flags)
12d8bb64
MKL
589{
590 struct sockaddr_nl sa;
591 int sock, seq = 0, len, ret = -1;
592 char buf[4096];
593 struct nlattr *nla, *nla_xdp;
594 struct {
595 struct nlmsghdr nh;
596 struct ifinfomsg ifinfo;
597 char attrbuf[64];
598 } req;
599 struct nlmsghdr *nh;
600 struct nlmsgerr *err;
601
602 memset(&sa, 0, sizeof(sa));
603 sa.nl_family = AF_NETLINK;
604
605 sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
606 if (sock < 0) {
607 printf("open netlink socket: %s\n", strerror(errno));
608 return -1;
609 }
610
611 if (bind(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
612 printf("bind to netlink: %s\n", strerror(errno));
613 goto cleanup;
614 }
615
616 memset(&req, 0, sizeof(req));
617 req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
618 req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
619 req.nh.nlmsg_type = RTM_SETLINK;
620 req.nh.nlmsg_pid = 0;
621 req.nh.nlmsg_seq = ++seq;
622 req.ifinfo.ifi_family = AF_UNSPEC;
623 req.ifinfo.ifi_index = ifindex;
3993f2cb
DA
624
625 /* started nested attribute for XDP */
12d8bb64
MKL
626 nla = (struct nlattr *)(((char *)&req)
627 + NLMSG_ALIGN(req.nh.nlmsg_len));
628 nla->nla_type = NLA_F_NESTED | 43/*IFLA_XDP*/;
3993f2cb 629 nla->nla_len = NLA_HDRLEN;
12d8bb64 630
3993f2cb
DA
631 /* add XDP fd */
632 nla_xdp = (struct nlattr *)((char *)nla + nla->nla_len);
12d8bb64
MKL
633 nla_xdp->nla_type = 1/*IFLA_XDP_FD*/;
634 nla_xdp->nla_len = NLA_HDRLEN + sizeof(int);
635 memcpy((char *)nla_xdp + NLA_HDRLEN, &fd, sizeof(fd));
3993f2cb
DA
636 nla->nla_len += nla_xdp->nla_len;
637
638 /* if user passed in any flags, add those too */
639 if (flags) {
640 nla_xdp = (struct nlattr *)((char *)nla + nla->nla_len);
641 nla_xdp->nla_type = 3/*IFLA_XDP_FLAGS*/;
642 nla_xdp->nla_len = NLA_HDRLEN + sizeof(flags);
643 memcpy((char *)nla_xdp + NLA_HDRLEN, &flags, sizeof(flags));
644 nla->nla_len += nla_xdp->nla_len;
645 }
12d8bb64
MKL
646
647 req.nh.nlmsg_len += NLA_ALIGN(nla->nla_len);
648
649 if (send(sock, &req, req.nh.nlmsg_len, 0) < 0) {
650 printf("send to netlink: %s\n", strerror(errno));
651 goto cleanup;
652 }
653
654 len = recv(sock, buf, sizeof(buf), 0);
655 if (len < 0) {
656 printf("recv from netlink: %s\n", strerror(errno));
657 goto cleanup;
658 }
659
660 for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len);
661 nh = NLMSG_NEXT(nh, len)) {
662 if (nh->nlmsg_pid != getpid()) {
663 printf("Wrong pid %d, expected %d\n",
664 nh->nlmsg_pid, getpid());
665 goto cleanup;
666 }
667 if (nh->nlmsg_seq != seq) {
668 printf("Wrong seq %d, expected %d\n",
669 nh->nlmsg_seq, seq);
670 goto cleanup;
671 }
672 switch (nh->nlmsg_type) {
673 case NLMSG_ERROR:
674 err = (struct nlmsgerr *)NLMSG_DATA(nh);
675 if (!err->error)
676 continue;
677 printf("nlmsg error %s\n", strerror(-err->error));
678 goto cleanup;
679 case NLMSG_DONE:
680 break;
681 }
682 }
683
684 ret = 0;
685
686cleanup:
687 close(sock);
688 return ret;
689}