1 #include <linux/unistd.h>
12 #include <sys/types.h>
13 #include <sys/socket.h>
19 #include "sock_example.h"
21 #define BPF_F_PIN (1 << 0)
22 #define BPF_F_GET (1 << 1)
23 #define BPF_F_PIN_GET (BPF_F_PIN | BPF_F_GET)
25 #define BPF_F_KEY (1 << 2)
26 #define BPF_F_VAL (1 << 3)
27 #define BPF_F_KEY_VAL (BPF_F_KEY | BPF_F_VAL)
29 #define BPF_M_UNSPEC 0
33 static void usage(void)
35 printf("Usage: fds_example [...]\n");
36 printf(" -F <file> File to pin/get object\n");
37 printf(" -P |- pin object\n");
38 printf(" -G `- get object\n");
39 printf(" -m eBPF map mode\n");
40 printf(" -k <key> |- map key\n");
41 printf(" -v <value> `- map value\n");
42 printf(" -p eBPF prog mode\n");
43 printf(" -o <object> `- object file\n");
44 printf(" -h Display this help.\n");
47 static int bpf_map_create(void)
49 return bpf_create_map(BPF_MAP_TYPE_ARRAY
, sizeof(uint32_t),
50 sizeof(uint32_t), 1024, 0);
53 static int bpf_prog_create(const char *object
)
55 static struct bpf_insn insns
[] = {
56 BPF_MOV64_IMM(BPF_REG_0
, 1),
59 size_t insns_cnt
= sizeof(insns
) / sizeof(struct bpf_insn
);
62 assert(!load_bpf_file((char *)object
));
65 return bpf_load_program(BPF_PROG_TYPE_SOCKET_FILTER
,
66 insns
, insns_cnt
, "GPL", 0,
67 bpf_log_buf
, BPF_LOG_BUF_SIZE
);
71 static int bpf_do_map(const char *file
, uint32_t flags
, uint32_t key
,
76 if (flags
& BPF_F_PIN
) {
77 fd
= bpf_map_create();
78 printf("bpf: map fd:%d (%s)\n", fd
, strerror(errno
));
81 ret
= bpf_obj_pin(fd
, file
);
82 printf("bpf: pin ret:(%d,%s)\n", ret
, strerror(errno
));
85 fd
= bpf_obj_get(file
);
86 printf("bpf: get fd:%d (%s)\n", fd
, strerror(errno
));
90 if ((flags
& BPF_F_KEY_VAL
) == BPF_F_KEY_VAL
) {
91 ret
= bpf_map_update_elem(fd
, &key
, &value
, 0);
92 printf("bpf: fd:%d u->(%u:%u) ret:(%d,%s)\n", fd
, key
, value
,
93 ret
, strerror(errno
));
95 } else if (flags
& BPF_F_KEY
) {
96 ret
= bpf_map_lookup_elem(fd
, &key
, &value
);
97 printf("bpf: fd:%d l->(%u):%u ret:(%d,%s)\n", fd
, key
, value
,
98 ret
, strerror(errno
));
105 static int bpf_do_prog(const char *file
, uint32_t flags
, const char *object
)
109 if (flags
& BPF_F_PIN
) {
110 fd
= bpf_prog_create(object
);
111 printf("bpf: prog fd:%d (%s)\n", fd
, strerror(errno
));
114 ret
= bpf_obj_pin(fd
, file
);
115 printf("bpf: pin ret:(%d,%s)\n", ret
, strerror(errno
));
118 fd
= bpf_obj_get(file
);
119 printf("bpf: get fd:%d (%s)\n", fd
, strerror(errno
));
123 sock
= open_raw_sock("lo");
126 ret
= setsockopt(sock
, SOL_SOCKET
, SO_ATTACH_BPF
, &fd
, sizeof(fd
));
127 printf("bpf: sock:%d <- fd:%d attached ret:(%d,%s)\n", sock
, fd
,
128 ret
, strerror(errno
));
134 int main(int argc
, char **argv
)
136 const char *file
= NULL
, *object
= NULL
;
137 uint32_t key
= 0, value
= 0, flags
= 0;
138 int opt
, mode
= BPF_M_UNSPEC
;
140 while ((opt
= getopt(argc
, argv
, "F:PGmk:v:po:")) != -1) {
152 /* Map-related args */
157 key
= strtoul(optarg
, NULL
, 0);
161 value
= strtoul(optarg
, NULL
, 0);
164 /* Prog-related args */
176 if (!(flags
& BPF_F_PIN_GET
) || !file
)
181 return bpf_do_map(file
, flags
, key
, value
);
183 return bpf_do_prog(file
, flags
, object
);