Blame view

samples/bpf/fds_example.c 4.24 KB
42984d7c1   Daniel Borkmann   bpf: add sample u...
1
2
3
4
5
6
7
8
9
10
11
12
13
  #include <linux/unistd.h>
  #include <linux/bpf.h>
  
  #include <stdio.h>
  #include <stdlib.h>
  #include <stdint.h>
  #include <unistd.h>
  #include <string.h>
  #include <assert.h>
  #include <errno.h>
  
  #include <sys/types.h>
  #include <sys/socket.h>
8d9304507   Jakub Kicinski   samples: bpf: ren...
14
  #include <bpf/bpf.h>
4d18f6de6   Daniel T. Lee   samples: bpf: ref...
15
  #include "libbpf.h"
8d9304507   Jakub Kicinski   samples: bpf: ren...
16
  #include "bpf_insn.h"
9899694a7   Joe Stringer   samples/bpf: Move...
17
  #include "sock_example.h"
42984d7c1   Daniel Borkmann   bpf: add sample u...
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  
  #define BPF_F_PIN	(1 << 0)
  #define BPF_F_GET	(1 << 1)
  #define BPF_F_PIN_GET	(BPF_F_PIN | BPF_F_GET)
  
  #define BPF_F_KEY	(1 << 2)
  #define BPF_F_VAL	(1 << 3)
  #define BPF_F_KEY_VAL	(BPF_F_KEY | BPF_F_VAL)
  
  #define BPF_M_UNSPEC	0
  #define BPF_M_MAP	1
  #define BPF_M_PROG	2
  
  static void usage(void)
  {
  	printf("Usage: fds_example [...]
  ");
  	printf("       -F <file>   File to pin/get object
  ");
  	printf("       -P          |- pin object
  ");
  	printf("       -G          `- get object
  ");
  	printf("       -m          eBPF map mode
  ");
  	printf("       -k <key>    |- map key
  ");
  	printf("       -v <value>  `- map value
  ");
  	printf("       -p          eBPF prog mode
  ");
  	printf("       -o <object> `- object file
  ");
  	printf("       -h          Display this help.
  ");
  }
  
  static int bpf_map_create(void)
  {
  	return bpf_create_map(BPF_MAP_TYPE_ARRAY, sizeof(uint32_t),
89b976070   Alexei Starovoitov   samples/bpf: add ...
58
  			      sizeof(uint32_t), 1024, 0);
42984d7c1   Daniel Borkmann   bpf: add sample u...
59
60
61
62
  }
  
  static int bpf_prog_create(const char *object)
  {
811b4f0d7   Arnaldo Carvalho de Melo   samples/bpf: Be c...
63
  	static struct bpf_insn insns[] = {
42984d7c1   Daniel Borkmann   bpf: add sample u...
64
65
66
  		BPF_MOV64_IMM(BPF_REG_0, 1),
  		BPF_EXIT_INSN(),
  	};
43371c83f   Joe Stringer   samples/bpf: Swit...
67
  	size_t insns_cnt = sizeof(insns) / sizeof(struct bpf_insn);
1a9b268c9   Jakub Kicinski   samples: bpf: use...
68
69
70
  	char bpf_log_buf[BPF_LOG_BUF_SIZE];
  	struct bpf_object *obj;
  	int prog_fd;
42984d7c1   Daniel Borkmann   bpf: add sample u...
71
72
  
  	if (object) {
1a9b268c9   Jakub Kicinski   samples: bpf: use...
73
74
75
  		assert(!bpf_prog_load(object, BPF_PROG_TYPE_UNSPEC,
  				      &obj, &prog_fd));
  		return prog_fd;
42984d7c1   Daniel Borkmann   bpf: add sample u...
76
  	} else {
d40fc181e   Joe Stringer   samples/bpf: Make...
77
  		return bpf_load_program(BPF_PROG_TYPE_SOCKET_FILTER,
43371c83f   Joe Stringer   samples/bpf: Swit...
78
  					insns, insns_cnt, "GPL", 0,
d40fc181e   Joe Stringer   samples/bpf: Make...
79
  					bpf_log_buf, BPF_LOG_BUF_SIZE);
42984d7c1   Daniel Borkmann   bpf: add sample u...
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
  	}
  }
  
  static int bpf_do_map(const char *file, uint32_t flags, uint32_t key,
  		      uint32_t value)
  {
  	int fd, ret;
  
  	if (flags & BPF_F_PIN) {
  		fd = bpf_map_create();
  		printf("bpf: map fd:%d (%s)
  ", fd, strerror(errno));
  		assert(fd > 0);
  
  		ret = bpf_obj_pin(fd, file);
  		printf("bpf: pin ret:(%d,%s)
  ", ret, strerror(errno));
  		assert(ret == 0);
  	} else {
  		fd = bpf_obj_get(file);
  		printf("bpf: get fd:%d (%s)
  ", fd, strerror(errno));
  		assert(fd > 0);
  	}
  
  	if ((flags & BPF_F_KEY_VAL) == BPF_F_KEY_VAL) {
d40fc181e   Joe Stringer   samples/bpf: Make...
106
  		ret = bpf_map_update_elem(fd, &key, &value, 0);
42984d7c1   Daniel Borkmann   bpf: add sample u...
107
108
109
110
111
  		printf("bpf: fd:%d u->(%u:%u) ret:(%d,%s)
  ", fd, key, value,
  		       ret, strerror(errno));
  		assert(ret == 0);
  	} else if (flags & BPF_F_KEY) {
d40fc181e   Joe Stringer   samples/bpf: Make...
112
  		ret = bpf_map_lookup_elem(fd, &key, &value);
42984d7c1   Daniel Borkmann   bpf: add sample u...
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
  		printf("bpf: fd:%d l->(%u):%u ret:(%d,%s)
  ", fd, key, value,
  		       ret, strerror(errno));
  		assert(ret == 0);
  	}
  
  	return 0;
  }
  
  static int bpf_do_prog(const char *file, uint32_t flags, const char *object)
  {
  	int fd, sock, ret;
  
  	if (flags & BPF_F_PIN) {
  		fd = bpf_prog_create(object);
  		printf("bpf: prog fd:%d (%s)
  ", fd, strerror(errno));
  		assert(fd > 0);
  
  		ret = bpf_obj_pin(fd, file);
  		printf("bpf: pin ret:(%d,%s)
  ", ret, strerror(errno));
  		assert(ret == 0);
  	} else {
  		fd = bpf_obj_get(file);
  		printf("bpf: get fd:%d (%s)
  ", fd, strerror(errno));
  		assert(fd > 0);
  	}
  
  	sock = open_raw_sock("lo");
  	assert(sock > 0);
  
  	ret = setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &fd, sizeof(fd));
  	printf("bpf: sock:%d <- fd:%d attached ret:(%d,%s)
  ", sock, fd,
  	       ret, strerror(errno));
  	assert(ret == 0);
  
  	return 0;
  }
  
  int main(int argc, char **argv)
  {
  	const char *file = NULL, *object = NULL;
  	uint32_t key = 0, value = 0, flags = 0;
  	int opt, mode = BPF_M_UNSPEC;
  
  	while ((opt = getopt(argc, argv, "F:PGmk:v:po:")) != -1) {
  		switch (opt) {
  		/* General args */
  		case 'F':
  			file = optarg;
  			break;
  		case 'P':
  			flags |= BPF_F_PIN;
  			break;
  		case 'G':
  			flags |= BPF_F_GET;
  			break;
  		/* Map-related args */
  		case 'm':
  			mode = BPF_M_MAP;
  			break;
  		case 'k':
  			key = strtoul(optarg, NULL, 0);
  			flags |= BPF_F_KEY;
  			break;
  		case 'v':
  			value = strtoul(optarg, NULL, 0);
  			flags |= BPF_F_VAL;
  			break;
  		/* Prog-related args */
  		case 'p':
  			mode = BPF_M_PROG;
  			break;
  		case 'o':
  			object = optarg;
  			break;
  		default:
  			goto out;
  		}
  	}
  
  	if (!(flags & BPF_F_PIN_GET) || !file)
  		goto out;
  
  	switch (mode) {
  	case BPF_M_MAP:
  		return bpf_do_map(file, flags, key, value);
  	case BPF_M_PROG:
  		return bpf_do_prog(file, flags, object);
  	}
  out:
  	usage();
  	return -1;
  }