1 #include <linux/unistd.h>
12 #include <sys/types.h>
13 #include <sys/socket.h>
18 #define BPF_F_PIN (1 << 0)
19 #define BPF_F_GET (1 << 1)
20 #define BPF_F_PIN_GET (BPF_F_PIN | BPF_F_GET)
22 #define BPF_F_KEY (1 << 2)
23 #define BPF_F_VAL (1 << 3)
24 #define BPF_F_KEY_VAL (BPF_F_KEY | BPF_F_VAL)
26 #define BPF_M_UNSPEC 0
30 static void usage(void)
32 printf("Usage: fds_example [...]\n");
33 printf(" -F <file> File to pin/get object\n");
34 printf(" -P |- pin object\n");
35 printf(" -G `- get object\n");
36 printf(" -m eBPF map mode\n");
37 printf(" -k <key> |- map key\n");
38 printf(" -v <value> `- map value\n");
39 printf(" -p eBPF prog mode\n");
40 printf(" -o <object> `- object file\n");
41 printf(" -h Display this help.\n");
44 static int bpf_map_create(void)
46 return bpf_create_map(BPF_MAP_TYPE_ARRAY, sizeof(uint32_t),
47 sizeof(uint32_t), 1024, 0);
50 static int bpf_prog_create(const char *object)
52 static struct bpf_insn insns[] = {
53 BPF_MOV64_IMM(BPF_REG_0, 1),
56 size_t insns_cnt = sizeof(insns) / sizeof(struct bpf_insn);
59 assert(!load_bpf_file((char *)object));
62 return bpf_load_program(BPF_PROG_TYPE_SOCKET_FILTER,
63 insns, insns_cnt, "GPL", 0,
64 bpf_log_buf, BPF_LOG_BUF_SIZE);
68 static int bpf_do_map(const char *file, uint32_t flags, uint32_t key,
73 if (flags & BPF_F_PIN) {
74 fd = bpf_map_create();
75 printf("bpf: map fd:%d (%s)\n", fd, strerror(errno));
78 ret = bpf_obj_pin(fd, file);
79 printf("bpf: pin ret:(%d,%s)\n", ret, strerror(errno));
82 fd = bpf_obj_get(file);
83 printf("bpf: get fd:%d (%s)\n", fd, strerror(errno));
87 if ((flags & BPF_F_KEY_VAL) == BPF_F_KEY_VAL) {
88 ret = bpf_map_update_elem(fd, &key, &value, 0);
89 printf("bpf: fd:%d u->(%u:%u) ret:(%d,%s)\n", fd, key, value,
90 ret, strerror(errno));
92 } else if (flags & BPF_F_KEY) {
93 ret = bpf_map_lookup_elem(fd, &key, &value);
94 printf("bpf: fd:%d l->(%u):%u ret:(%d,%s)\n", fd, key, value,
95 ret, strerror(errno));
102 static int bpf_do_prog(const char *file, uint32_t flags, const char *object)
106 if (flags & BPF_F_PIN) {
107 fd = bpf_prog_create(object);
108 printf("bpf: prog fd:%d (%s)\n", fd, strerror(errno));
111 ret = bpf_obj_pin(fd, file);
112 printf("bpf: pin ret:(%d,%s)\n", ret, strerror(errno));
115 fd = bpf_obj_get(file);
116 printf("bpf: get fd:%d (%s)\n", fd, strerror(errno));
120 sock = open_raw_sock("lo");
123 ret = setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &fd, sizeof(fd));
124 printf("bpf: sock:%d <- fd:%d attached ret:(%d,%s)\n", sock, fd,
125 ret, strerror(errno));
131 int main(int argc, char **argv)
133 const char *file = NULL, *object = NULL;
134 uint32_t key = 0, value = 0, flags = 0;
135 int opt, mode = BPF_M_UNSPEC;
137 while ((opt = getopt(argc, argv, "F:PGmk:v:po:")) != -1) {
149 /* Map-related args */
154 key = strtoul(optarg, NULL, 0);
158 value = strtoul(optarg, NULL, 0);
161 /* Prog-related args */
173 if (!(flags & BPF_F_PIN_GET) || !file)
178 return bpf_do_map(file, flags, key, value);
180 return bpf_do_prog(file, flags, object);