1 #include <linux/unistd.h>
12 #include <sys/types.h>
13 #include <sys/socket.h>
17 #include <bpf/libbpf.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 char bpf_log_buf[BPF_LOG_BUF_SIZE];
35 static void usage(void)
37 printf("Usage: fds_example [...]\n");
38 printf(" -F <file> File to pin/get object\n");
39 printf(" -P |- pin object\n");
40 printf(" -G `- get object\n");
41 printf(" -m eBPF map mode\n");
42 printf(" -k <key> |- map key\n");
43 printf(" -v <value> `- map value\n");
44 printf(" -p eBPF prog mode\n");
45 printf(" -o <object> `- object file\n");
46 printf(" -h Display this help.\n");
49 static int bpf_prog_create(const char *object)
51 static struct bpf_insn insns[] = {
52 BPF_MOV64_IMM(BPF_REG_0, 1),
55 size_t insns_cnt = sizeof(insns) / sizeof(struct bpf_insn);
56 struct bpf_object *obj;
60 obj = bpf_object__open_file(object, NULL);
61 assert(!libbpf_get_error(obj));
62 err = bpf_object__load(obj);
64 return bpf_program__fd(bpf_object__next_program(obj, NULL));
66 LIBBPF_OPTS(bpf_prog_load_opts, opts,
67 .log_buf = bpf_log_buf,
68 .log_size = BPF_LOG_BUF_SIZE,
71 return bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL",
72 insns, insns_cnt, &opts);
76 static int bpf_do_map(const char *file, uint32_t flags, uint32_t key,
81 if (flags & BPF_F_PIN) {
82 fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, sizeof(uint32_t),
83 sizeof(uint32_t), 1024, NULL);
84 printf("bpf: map fd:%d (%s)\n", fd, strerror(errno));
87 ret = bpf_obj_pin(fd, file);
88 printf("bpf: pin ret:(%d,%s)\n", ret, strerror(errno));
91 fd = bpf_obj_get(file);
92 printf("bpf: get fd:%d (%s)\n", fd, strerror(errno));
96 if ((flags & BPF_F_KEY_VAL) == BPF_F_KEY_VAL) {
97 ret = bpf_map_update_elem(fd, &key, &value, 0);
98 printf("bpf: fd:%d u->(%u:%u) ret:(%d,%s)\n", fd, key, value,
99 ret, strerror(errno));
101 } else if (flags & BPF_F_KEY) {
102 ret = bpf_map_lookup_elem(fd, &key, &value);
103 printf("bpf: fd:%d l->(%u):%u ret:(%d,%s)\n", fd, key, value,
104 ret, strerror(errno));
111 static int bpf_do_prog(const char *file, uint32_t flags, const char *object)
115 if (flags & BPF_F_PIN) {
116 fd = bpf_prog_create(object);
117 printf("bpf: prog fd:%d (%s)\n", fd, strerror(errno));
120 ret = bpf_obj_pin(fd, file);
121 printf("bpf: pin ret:(%d,%s)\n", ret, strerror(errno));
124 fd = bpf_obj_get(file);
125 printf("bpf: get fd:%d (%s)\n", fd, strerror(errno));
129 sock = open_raw_sock("lo");
132 ret = setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &fd, sizeof(fd));
133 printf("bpf: sock:%d <- fd:%d attached ret:(%d,%s)\n", sock, fd,
134 ret, strerror(errno));
140 int main(int argc, char **argv)
142 const char *file = NULL, *object = NULL;
143 uint32_t key = 0, value = 0, flags = 0;
144 int opt, mode = BPF_M_UNSPEC;
146 while ((opt = getopt(argc, argv, "F:PGmk:v:po:")) != -1) {
158 /* Map-related args */
163 key = strtoul(optarg, NULL, 0);
167 value = strtoul(optarg, NULL, 0);
170 /* Prog-related args */
182 if (!(flags & BPF_F_PIN_GET) || !file)
187 return bpf_do_map(file, flags, key, value);
189 return bpf_do_prog(file, flags, object);