blob: a5cddc99cccdea8165ff567adea7c2fdc31dcc12 [file] [log] [blame]
Daniel Borkmann42984d72015-10-29 14:58:10 +01001#include <linux/unistd.h>
2#include <linux/bpf.h>
3
4#include <stdio.h>
5#include <stdlib.h>
6#include <stdint.h>
7#include <unistd.h>
8#include <string.h>
9#include <assert.h>
10#include <errno.h>
11
12#include <sys/types.h>
13#include <sys/socket.h>
14
15#include "bpf_load.h"
16#include "libbpf.h"
17
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)
21
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)
25
26#define BPF_M_UNSPEC 0
27#define BPF_M_MAP 1
28#define BPF_M_PROG 2
29
30static void usage(void)
31{
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");
42}
43
44static int bpf_map_create(void)
45{
46 return bpf_create_map(BPF_MAP_TYPE_ARRAY, sizeof(uint32_t),
Alexei Starovoitov89b97602016-03-07 21:57:20 -080047 sizeof(uint32_t), 1024, 0);
Daniel Borkmann42984d72015-10-29 14:58:10 +010048}
49
50static int bpf_prog_create(const char *object)
51{
Arnaldo Carvalho de Melo811b4f02016-12-20 11:03:13 -030052 static struct bpf_insn insns[] = {
Daniel Borkmann42984d72015-10-29 14:58:10 +010053 BPF_MOV64_IMM(BPF_REG_0, 1),
54 BPF_EXIT_INSN(),
55 };
Joe Stringer43371c82016-12-14 14:43:39 -080056 size_t insns_cnt = sizeof(insns) / sizeof(struct bpf_insn);
Daniel Borkmann42984d72015-10-29 14:58:10 +010057
58 if (object) {
59 assert(!load_bpf_file((char *)object));
60 return prog_fd[0];
61 } else {
Joe Stringerd40fc182016-12-14 14:43:38 -080062 return bpf_load_program(BPF_PROG_TYPE_SOCKET_FILTER,
Joe Stringer43371c82016-12-14 14:43:39 -080063 insns, insns_cnt, "GPL", 0,
Joe Stringerd40fc182016-12-14 14:43:38 -080064 bpf_log_buf, BPF_LOG_BUF_SIZE);
Daniel Borkmann42984d72015-10-29 14:58:10 +010065 }
66}
67
68static int bpf_do_map(const char *file, uint32_t flags, uint32_t key,
69 uint32_t value)
70{
71 int fd, ret;
72
73 if (flags & BPF_F_PIN) {
74 fd = bpf_map_create();
75 printf("bpf: map fd:%d (%s)\n", fd, strerror(errno));
76 assert(fd > 0);
77
78 ret = bpf_obj_pin(fd, file);
79 printf("bpf: pin ret:(%d,%s)\n", ret, strerror(errno));
80 assert(ret == 0);
81 } else {
82 fd = bpf_obj_get(file);
83 printf("bpf: get fd:%d (%s)\n", fd, strerror(errno));
84 assert(fd > 0);
85 }
86
87 if ((flags & BPF_F_KEY_VAL) == BPF_F_KEY_VAL) {
Joe Stringerd40fc182016-12-14 14:43:38 -080088 ret = bpf_map_update_elem(fd, &key, &value, 0);
Daniel Borkmann42984d72015-10-29 14:58:10 +010089 printf("bpf: fd:%d u->(%u:%u) ret:(%d,%s)\n", fd, key, value,
90 ret, strerror(errno));
91 assert(ret == 0);
92 } else if (flags & BPF_F_KEY) {
Joe Stringerd40fc182016-12-14 14:43:38 -080093 ret = bpf_map_lookup_elem(fd, &key, &value);
Daniel Borkmann42984d72015-10-29 14:58:10 +010094 printf("bpf: fd:%d l->(%u):%u ret:(%d,%s)\n", fd, key, value,
95 ret, strerror(errno));
96 assert(ret == 0);
97 }
98
99 return 0;
100}
101
102static int bpf_do_prog(const char *file, uint32_t flags, const char *object)
103{
104 int fd, sock, ret;
105
106 if (flags & BPF_F_PIN) {
107 fd = bpf_prog_create(object);
108 printf("bpf: prog fd:%d (%s)\n", fd, strerror(errno));
109 assert(fd > 0);
110
111 ret = bpf_obj_pin(fd, file);
112 printf("bpf: pin ret:(%d,%s)\n", ret, strerror(errno));
113 assert(ret == 0);
114 } else {
115 fd = bpf_obj_get(file);
116 printf("bpf: get fd:%d (%s)\n", fd, strerror(errno));
117 assert(fd > 0);
118 }
119
120 sock = open_raw_sock("lo");
121 assert(sock > 0);
122
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));
126 assert(ret == 0);
127
128 return 0;
129}
130
131int main(int argc, char **argv)
132{
133 const char *file = NULL, *object = NULL;
134 uint32_t key = 0, value = 0, flags = 0;
135 int opt, mode = BPF_M_UNSPEC;
136
137 while ((opt = getopt(argc, argv, "F:PGmk:v:po:")) != -1) {
138 switch (opt) {
139 /* General args */
140 case 'F':
141 file = optarg;
142 break;
143 case 'P':
144 flags |= BPF_F_PIN;
145 break;
146 case 'G':
147 flags |= BPF_F_GET;
148 break;
149 /* Map-related args */
150 case 'm':
151 mode = BPF_M_MAP;
152 break;
153 case 'k':
154 key = strtoul(optarg, NULL, 0);
155 flags |= BPF_F_KEY;
156 break;
157 case 'v':
158 value = strtoul(optarg, NULL, 0);
159 flags |= BPF_F_VAL;
160 break;
161 /* Prog-related args */
162 case 'p':
163 mode = BPF_M_PROG;
164 break;
165 case 'o':
166 object = optarg;
167 break;
168 default:
169 goto out;
170 }
171 }
172
173 if (!(flags & BPF_F_PIN_GET) || !file)
174 goto out;
175
176 switch (mode) {
177 case BPF_M_MAP:
178 return bpf_do_map(file, flags, key, value);
179 case BPF_M_PROG:
180 return bpf_do_prog(file, flags, object);
181 }
182out:
183 usage();
184 return -1;
185}