blob: 16dbf49e0f194654e82c7faf65168a046ca5852e [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
Jakub Kicinski8d930452018-05-14 22:35:03 -070015#include <bpf/bpf.h>
16
Toke Høiland-Jørgensen7cf245a2020-01-20 14:06:49 +010017#include <bpf/libbpf.h>
Jakub Kicinski8d930452018-05-14 22:35:03 -070018#include "bpf_insn.h"
Joe Stringer98996942016-12-08 18:46:20 -080019#include "sock_example.h"
Daniel Borkmann42984d72015-10-29 14:58:10 +010020
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)
24
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)
28
29#define BPF_M_UNSPEC 0
30#define BPF_M_MAP 1
31#define BPF_M_PROG 2
32
Wenbo Zhangeef8a42d2020-07-10 05:20:35 -040033char bpf_log_buf[BPF_LOG_BUF_SIZE];
34
Daniel Borkmann42984d72015-10-29 14:58:10 +010035static void usage(void)
36{
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");
47}
48
Daniel Borkmann42984d72015-10-29 14:58:10 +010049static int bpf_prog_create(const char *object)
50{
Arnaldo Carvalho de Melo811b4f02016-12-20 11:03:13 -030051 static struct bpf_insn insns[] = {
Daniel Borkmann42984d72015-10-29 14:58:10 +010052 BPF_MOV64_IMM(BPF_REG_0, 1),
53 BPF_EXIT_INSN(),
54 };
Joe Stringer43371c82016-12-14 14:43:39 -080055 size_t insns_cnt = sizeof(insns) / sizeof(struct bpf_insn);
Jakub Kicinski1a9b2682019-02-27 19:04:13 -080056 struct bpf_object *obj;
Andrii Nakryikoc58f9812021-12-01 15:28:23 -080057 int err;
Daniel Borkmann42984d72015-10-29 14:58:10 +010058
59 if (object) {
Andrii Nakryikoc58f9812021-12-01 15:28:23 -080060 obj = bpf_object__open_file(object, NULL);
61 assert(!libbpf_get_error(obj));
62 err = bpf_object__load(obj);
63 assert(!err);
64 return bpf_program__fd(bpf_object__next_program(obj, NULL));
Daniel Borkmann42984d72015-10-29 14:58:10 +010065 } else {
Andrii Nakryikoc58f9812021-12-01 15:28:23 -080066 LIBBPF_OPTS(bpf_prog_load_opts, opts,
67 .log_buf = bpf_log_buf,
68 .log_size = BPF_LOG_BUF_SIZE,
69 );
70
71 return bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL",
72 insns, insns_cnt, &opts);
Daniel Borkmann42984d72015-10-29 14:58:10 +010073 }
74}
75
76static int bpf_do_map(const char *file, uint32_t flags, uint32_t key,
77 uint32_t value)
78{
79 int fd, ret;
80
81 if (flags & BPF_F_PIN) {
Andrii Nakryikoc58f9812021-12-01 15:28:23 -080082 fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, sizeof(uint32_t),
83 sizeof(uint32_t), 1024, NULL);
Daniel Borkmann42984d72015-10-29 14:58:10 +010084 printf("bpf: map fd:%d (%s)\n", fd, strerror(errno));
85 assert(fd > 0);
86
87 ret = bpf_obj_pin(fd, file);
88 printf("bpf: pin ret:(%d,%s)\n", ret, strerror(errno));
89 assert(ret == 0);
90 } else {
91 fd = bpf_obj_get(file);
92 printf("bpf: get fd:%d (%s)\n", fd, strerror(errno));
93 assert(fd > 0);
94 }
95
96 if ((flags & BPF_F_KEY_VAL) == BPF_F_KEY_VAL) {
Joe Stringerd40fc182016-12-14 14:43:38 -080097 ret = bpf_map_update_elem(fd, &key, &value, 0);
Daniel Borkmann42984d72015-10-29 14:58:10 +010098 printf("bpf: fd:%d u->(%u:%u) ret:(%d,%s)\n", fd, key, value,
99 ret, strerror(errno));
100 assert(ret == 0);
101 } else if (flags & BPF_F_KEY) {
Joe Stringerd40fc182016-12-14 14:43:38 -0800102 ret = bpf_map_lookup_elem(fd, &key, &value);
Daniel Borkmann42984d72015-10-29 14:58:10 +0100103 printf("bpf: fd:%d l->(%u):%u ret:(%d,%s)\n", fd, key, value,
104 ret, strerror(errno));
105 assert(ret == 0);
106 }
107
108 return 0;
109}
110
111static int bpf_do_prog(const char *file, uint32_t flags, const char *object)
112{
113 int fd, sock, ret;
114
115 if (flags & BPF_F_PIN) {
116 fd = bpf_prog_create(object);
117 printf("bpf: prog fd:%d (%s)\n", fd, strerror(errno));
118 assert(fd > 0);
119
120 ret = bpf_obj_pin(fd, file);
121 printf("bpf: pin ret:(%d,%s)\n", ret, strerror(errno));
122 assert(ret == 0);
123 } else {
124 fd = bpf_obj_get(file);
125 printf("bpf: get fd:%d (%s)\n", fd, strerror(errno));
126 assert(fd > 0);
127 }
128
129 sock = open_raw_sock("lo");
130 assert(sock > 0);
131
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));
135 assert(ret == 0);
136
137 return 0;
138}
139
140int main(int argc, char **argv)
141{
142 const char *file = NULL, *object = NULL;
143 uint32_t key = 0, value = 0, flags = 0;
144 int opt, mode = BPF_M_UNSPEC;
145
146 while ((opt = getopt(argc, argv, "F:PGmk:v:po:")) != -1) {
147 switch (opt) {
148 /* General args */
149 case 'F':
150 file = optarg;
151 break;
152 case 'P':
153 flags |= BPF_F_PIN;
154 break;
155 case 'G':
156 flags |= BPF_F_GET;
157 break;
158 /* Map-related args */
159 case 'm':
160 mode = BPF_M_MAP;
161 break;
162 case 'k':
163 key = strtoul(optarg, NULL, 0);
164 flags |= BPF_F_KEY;
165 break;
166 case 'v':
167 value = strtoul(optarg, NULL, 0);
168 flags |= BPF_F_VAL;
169 break;
170 /* Prog-related args */
171 case 'p':
172 mode = BPF_M_PROG;
173 break;
174 case 'o':
175 object = optarg;
176 break;
177 default:
178 goto out;
179 }
180 }
181
182 if (!(flags & BPF_F_PIN_GET) || !file)
183 goto out;
184
185 switch (mode) {
186 case BPF_M_MAP:
187 return bpf_do_map(file, flags, key, value);
188 case BPF_M_PROG:
189 return bpf_do_prog(file, flags, object);
190 }
191out:
192 usage();
193 return -1;
194}