1 #include <linux/unistd.h>
12 #include <sys/types.h>
13 #include <sys/socket.h>
17 #include "sock_example.h"
19 #define BPF_F_PIN (1 << 0)
20 #define BPF_F_GET (1 << 1)
21 #define BPF_F_PIN_GET (BPF_F_PIN | BPF_F_GET)
23 #define BPF_F_KEY (1 << 2)
24 #define BPF_F_VAL (1 << 3)
25 #define BPF_F_KEY_VAL (BPF_F_KEY | BPF_F_VAL)
27 #define BPF_M_UNSPEC 0
31 static void usage(void)
33 printf("Usage: fds_example [...]\n");
34 printf(" -F <file> File to pin/get object\n");
35 printf(" -P |- pin object\n");
36 printf(" -G `- get object\n");
37 printf(" -m eBPF map mode\n");
38 printf(" -k <key> |- map key\n");
39 printf(" -v <value> `- map value\n");
40 printf(" -p eBPF prog mode\n");
41 printf(" -o <object> `- object file\n");
42 printf(" -h Display this help.\n");
45 static int bpf_map_create(void)
47 return bpf_create_map(BPF_MAP_TYPE_ARRAY
, sizeof(uint32_t),
48 sizeof(uint32_t), 1024, 0);
51 static int bpf_prog_create(const char *object
)
53 static struct bpf_insn insns
[] = {
54 BPF_MOV64_IMM(BPF_REG_0
, 1),
57 size_t insns_cnt
= sizeof(insns
) / sizeof(struct bpf_insn
);
60 assert(!load_bpf_file((char *)object
));
63 return bpf_load_program(BPF_PROG_TYPE_SOCKET_FILTER
,
64 insns
, insns_cnt
, "GPL", 0,
65 bpf_log_buf
, BPF_LOG_BUF_SIZE
);
69 static int bpf_do_map(const char *file
, uint32_t flags
, uint32_t key
,
74 if (flags
& BPF_F_PIN
) {
75 fd
= bpf_map_create();
76 printf("bpf: map fd:%d (%s)\n", fd
, strerror(errno
));
79 ret
= bpf_obj_pin(fd
, file
);
80 printf("bpf: pin ret:(%d,%s)\n", ret
, strerror(errno
));
83 fd
= bpf_obj_get(file
);
84 printf("bpf: get fd:%d (%s)\n", fd
, strerror(errno
));
88 if ((flags
& BPF_F_KEY_VAL
) == BPF_F_KEY_VAL
) {
89 ret
= bpf_map_update_elem(fd
, &key
, &value
, 0);
90 printf("bpf: fd:%d u->(%u:%u) ret:(%d,%s)\n", fd
, key
, value
,
91 ret
, strerror(errno
));
93 } else if (flags
& BPF_F_KEY
) {
94 ret
= bpf_map_lookup_elem(fd
, &key
, &value
);
95 printf("bpf: fd:%d l->(%u):%u ret:(%d,%s)\n", fd
, key
, value
,
96 ret
, strerror(errno
));
103 static int bpf_do_prog(const char *file
, uint32_t flags
, const char *object
)
107 if (flags
& BPF_F_PIN
) {
108 fd
= bpf_prog_create(object
);
109 printf("bpf: prog fd:%d (%s)\n", fd
, strerror(errno
));
112 ret
= bpf_obj_pin(fd
, file
);
113 printf("bpf: pin ret:(%d,%s)\n", ret
, strerror(errno
));
116 fd
= bpf_obj_get(file
);
117 printf("bpf: get fd:%d (%s)\n", fd
, strerror(errno
));
121 sock
= open_raw_sock("lo");
124 ret
= setsockopt(sock
, SOL_SOCKET
, SO_ATTACH_BPF
, &fd
, sizeof(fd
));
125 printf("bpf: sock:%d <- fd:%d attached ret:(%d,%s)\n", sock
, fd
,
126 ret
, strerror(errno
));
132 int main(int argc
, char **argv
)
134 const char *file
= NULL
, *object
= NULL
;
135 uint32_t key
= 0, value
= 0, flags
= 0;
136 int opt
, mode
= BPF_M_UNSPEC
;
138 while ((opt
= getopt(argc
, argv
, "F:PGmk:v:po:")) != -1) {
150 /* Map-related args */
155 key
= strtoul(optarg
, NULL
, 0);
159 value
= strtoul(optarg
, NULL
, 0);
162 /* Prog-related args */
174 if (!(flags
& BPF_F_PIN_GET
) || !file
)
179 return bpf_do_map(file
, flags
, key
, value
);
181 return bpf_do_prog(file
, flags
, object
);