-
-
Notifications
You must be signed in to change notification settings - Fork 82
/
xdp-counter.c
executable file
·73 lines (67 loc) · 1.6 KB
/
xdp-counter.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <stdlib.h>
#include <stdio.h>
#include <linux/if_link.h>
#include <bpf/bpf.h>
#include <bpf/libbpf.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <unistd.h>
static int libbpf_print_fn(enum libbpf_print_level level, const char *format,
va_list args)
{
return vfprintf(stderr, format, args);
}
int main(int argc, char **argv)
{
if (argc < 3) {
printf("ERROR - Usage is: ./loader"
" <BPF_FILE> <INTERFACE> [btf_file]\n"
"\n");
return 1;
}
libbpf_set_print(libbpf_print_fn);
LIBBPF_OPTS(bpf_object_open_opts , opts,
);
if (argc == 4)
opts.btf_custom_path = argv[3];
// Open and load the BPF program
struct bpf_object* obj = bpf_object__open_file(argv[1], &opts);
if (bpf_object__load(obj)) {
printf( "Failed to load program\n");
return 1;
}
struct bpf_program* prog = bpf_object__find_program_by_name(obj, "xdp_pass");
if (!prog) {
printf("Failed to find program\n");
return 1;
}
bpf_program__set_type(prog, BPF_PROG_TYPE_XDP);
int prog_fd = bpf_program__fd(prog);
if (prog_fd < 0) {
printf("Failed to get prog FD\n");
return 1;
}
const char* progName = bpf_program__name(prog);
if (!progName) {
printf("Failed to get progName\n");
return 1;
}
printf("load prog %s\n", progName);
// Attach the XDP program to the interface
int ifindex = if_nametoindex(argv[2]);
if (!ifindex) {
printf("failed to if_nametoindex\n");
return 1;
}
struct bpf_link *link = bpf_program__attach_xdp(prog, ifindex);
if (!link) {
printf("attach error\n");
return 1;
}
printf("Attach XDP success\n");
while (1) {
sleep(1);
printf("wait...\n");
}
return 0;
}