1 /*
2  * Copyright 2013 Google Inc.
3  * Author: Willem de Bruijn (willemb@google.com)
4  *
5  * A basic test of packet socket fanout behavior.
6  *
7  * Control:
8  * - create fanout fails as expected with illegal flag combinations
9  * - join   fanout fails as expected with diverging types or flags
10  *
11  * Datapath:
12  *   Open a pair of packet sockets and a pair of INET sockets, send a known
13  *   number of packets across the two INET sockets and count the number of
14  *   packets enqueued onto the two packet sockets.
15  *
16  *   The test currently runs for
17  *   - PACKET_FANOUT_HASH
18  *   - PACKET_FANOUT_HASH with PACKET_FANOUT_FLAG_ROLLOVER
19  *   - PACKET_FANOUT_LB
20  *   - PACKET_FANOUT_CPU
21  *   - PACKET_FANOUT_ROLLOVER
22  *   - PACKET_FANOUT_CBPF
23  *   - PACKET_FANOUT_EBPF
24  *
25  * Todo:
26  * - functionality: PACKET_FANOUT_FLAG_DEFRAG
27  *
28  * License (GPLv2):
29  *
30  * This program is free software; you can redistribute it and/or modify it
31  * under the terms and conditions of the GNU General Public License,
32  * version 2, as published by the Free Software Foundation.
33  *
34  * This program is distributed in the hope it will be useful, but WITHOUT
35  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
36  * FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for
37  * more details.
38  *
39  * You should have received a copy of the GNU General Public License along with
40  * this program; if not, write to the Free Software Foundation, Inc.,
41  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
42  */
43 
44 #define _GNU_SOURCE		/* for sched_setaffinity */
45 
46 #include <arpa/inet.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <linux/unistd.h>	/* for __NR_bpf */
50 #include <linux/filter.h>
51 #include <linux/bpf.h>
52 #include <linux/if_packet.h>
53 #include <net/ethernet.h>
54 #include <netinet/ip.h>
55 #include <netinet/udp.h>
56 #include <poll.h>
57 #include <sched.h>
58 #include <stdint.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <sys/mman.h>
63 #include <sys/socket.h>
64 #include <sys/stat.h>
65 #include <sys/types.h>
66 #include <unistd.h>
67 
68 #include "psock_lib.h"
69 
70 #define RING_NUM_FRAMES			20
71 
72 /* Open a socket in a given fanout mode.
73  * @return -1 if mode is bad, a valid socket otherwise */
74 static int sock_fanout_open(uint16_t typeflags, uint16_t group_id)
75 {
76 	int fd, val;
77 
78 	fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IP));
79 	if (fd < 0) {
80 		perror("socket packet");
81 		exit(1);
82 	}
83 
84 	val = (((int) typeflags) << 16) | group_id;
85 	if (setsockopt(fd, SOL_PACKET, PACKET_FANOUT, &val, sizeof(val))) {
86 		if (close(fd)) {
87 			perror("close packet");
88 			exit(1);
89 		}
90 		return -1;
91 	}
92 
93 	pair_udp_setfilter(fd);
94 	return fd;
95 }
96 
97 static void sock_fanout_set_cbpf(int fd)
98 {
99 	struct sock_filter bpf_filter[] = {
100 		BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 80),	      /* ldb [80] */
101 		BPF_STMT(BPF_RET+BPF_A, 0),		      /* ret A */
102 	};
103 	struct sock_fprog bpf_prog;
104 
105 	bpf_prog.filter = bpf_filter;
106 	bpf_prog.len = sizeof(bpf_filter) / sizeof(struct sock_filter);
107 
108 	if (setsockopt(fd, SOL_PACKET, PACKET_FANOUT_DATA, &bpf_prog,
109 		       sizeof(bpf_prog))) {
110 		perror("fanout data cbpf");
111 		exit(1);
112 	}
113 }
114 
115 static void sock_fanout_getopts(int fd, uint16_t *typeflags, uint16_t *group_id)
116 {
117 	int sockopt;
118 	socklen_t sockopt_len = sizeof(sockopt);
119 
120 	if (getsockopt(fd, SOL_PACKET, PACKET_FANOUT,
121 		       &sockopt, &sockopt_len)) {
122 		perror("failed to getsockopt");
123 		exit(1);
124 	}
125 	*typeflags = sockopt >> 16;
126 	*group_id = sockopt & 0xfffff;
127 }
128 
129 static void sock_fanout_set_ebpf(int fd)
130 {
131 	const int len_off = __builtin_offsetof(struct __sk_buff, len);
132 	struct bpf_insn prog[] = {
133 		{ BPF_ALU64 | BPF_MOV | BPF_X,   6, 1, 0, 0 },
134 		{ BPF_LDX   | BPF_W   | BPF_MEM, 0, 6, len_off, 0 },
135 		{ BPF_JMP   | BPF_JGE | BPF_K,   0, 0, 1, DATA_LEN },
136 		{ BPF_JMP   | BPF_JA  | BPF_K,   0, 0, 4, 0 },
137 		{ BPF_LD    | BPF_B   | BPF_ABS, 0, 0, 0, 0x50 },
138 		{ BPF_JMP   | BPF_JEQ | BPF_K,   0, 0, 2, DATA_CHAR },
139 		{ BPF_JMP   | BPF_JEQ | BPF_K,   0, 0, 1, DATA_CHAR_1 },
140 		{ BPF_ALU   | BPF_MOV | BPF_K,   0, 0, 0, 0 },
141 		{ BPF_JMP   | BPF_EXIT,          0, 0, 0, 0 }
142 	};
143 	char log_buf[512];
144 	union bpf_attr attr;
145 	int pfd;
146 
147 	memset(&attr, 0, sizeof(attr));
148 	attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
149 	attr.insns = (unsigned long) prog;
150 	attr.insn_cnt = sizeof(prog) / sizeof(prog[0]);
151 	attr.license = (unsigned long) "GPL";
152 	attr.log_buf = (unsigned long) log_buf,
153 	attr.log_size = sizeof(log_buf),
154 	attr.log_level = 1,
155 
156 	pfd = syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr));
157 	if (pfd < 0) {
158 		perror("bpf");
159 		fprintf(stderr, "bpf verifier:\n%s\n", log_buf);
160 		exit(1);
161 	}
162 
163 	if (setsockopt(fd, SOL_PACKET, PACKET_FANOUT_DATA, &pfd, sizeof(pfd))) {
164 		perror("fanout data ebpf");
165 		exit(1);
166 	}
167 
168 	if (close(pfd)) {
169 		perror("close ebpf");
170 		exit(1);
171 	}
172 }
173 
174 static char *sock_fanout_open_ring(int fd)
175 {
176 	struct tpacket_req req = {
177 		.tp_block_size = getpagesize(),
178 		.tp_frame_size = getpagesize(),
179 		.tp_block_nr   = RING_NUM_FRAMES,
180 		.tp_frame_nr   = RING_NUM_FRAMES,
181 	};
182 	char *ring;
183 	int val = TPACKET_V2;
184 
185 	if (setsockopt(fd, SOL_PACKET, PACKET_VERSION, (void *) &val,
186 		       sizeof(val))) {
187 		perror("packetsock ring setsockopt version");
188 		exit(1);
189 	}
190 	if (setsockopt(fd, SOL_PACKET, PACKET_RX_RING, (void *) &req,
191 		       sizeof(req))) {
192 		perror("packetsock ring setsockopt");
193 		exit(1);
194 	}
195 
196 	ring = mmap(0, req.tp_block_size * req.tp_block_nr,
197 		    PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
198 	if (ring == MAP_FAILED) {
199 		perror("packetsock ring mmap");
200 		exit(1);
201 	}
202 
203 	return ring;
204 }
205 
206 static int sock_fanout_read_ring(int fd, void *ring)
207 {
208 	struct tpacket2_hdr *header = ring;
209 	int count = 0;
210 
211 	while (count < RING_NUM_FRAMES && header->tp_status & TP_STATUS_USER) {
212 		count++;
213 		header = ring + (count * getpagesize());
214 	}
215 
216 	return count;
217 }
218 
219 static int sock_fanout_read(int fds[], char *rings[], const int expect[])
220 {
221 	int ret[2];
222 
223 	ret[0] = sock_fanout_read_ring(fds[0], rings[0]);
224 	ret[1] = sock_fanout_read_ring(fds[1], rings[1]);
225 
226 	fprintf(stderr, "info: count=%d,%d, expect=%d,%d\n",
227 			ret[0], ret[1], expect[0], expect[1]);
228 
229 	if ((!(ret[0] == expect[0] && ret[1] == expect[1])) &&
230 	    (!(ret[0] == expect[1] && ret[1] == expect[0]))) {
231 		fprintf(stderr, "ERROR: incorrect queue lengths\n");
232 		return 1;
233 	}
234 
235 	return 0;
236 }
237 
238 /* Test illegal mode + flag combination */
239 static void test_control_single(void)
240 {
241 	fprintf(stderr, "test: control single socket\n");
242 
243 	if (sock_fanout_open(PACKET_FANOUT_ROLLOVER |
244 			       PACKET_FANOUT_FLAG_ROLLOVER, 0) != -1) {
245 		fprintf(stderr, "ERROR: opened socket with dual rollover\n");
246 		exit(1);
247 	}
248 }
249 
250 /* Test illegal group with different modes or flags */
251 static void test_control_group(void)
252 {
253 	int fds[2];
254 
255 	fprintf(stderr, "test: control multiple sockets\n");
256 
257 	fds[0] = sock_fanout_open(PACKET_FANOUT_HASH, 0);
258 	if (fds[0] == -1) {
259 		fprintf(stderr, "ERROR: failed to open HASH socket\n");
260 		exit(1);
261 	}
262 	if (sock_fanout_open(PACKET_FANOUT_HASH |
263 			       PACKET_FANOUT_FLAG_DEFRAG, 0) != -1) {
264 		fprintf(stderr, "ERROR: joined group with wrong flag defrag\n");
265 		exit(1);
266 	}
267 	if (sock_fanout_open(PACKET_FANOUT_HASH |
268 			       PACKET_FANOUT_FLAG_ROLLOVER, 0) != -1) {
269 		fprintf(stderr, "ERROR: joined group with wrong flag ro\n");
270 		exit(1);
271 	}
272 	if (sock_fanout_open(PACKET_FANOUT_CPU, 0) != -1) {
273 		fprintf(stderr, "ERROR: joined group with wrong mode\n");
274 		exit(1);
275 	}
276 	fds[1] = sock_fanout_open(PACKET_FANOUT_HASH, 0);
277 	if (fds[1] == -1) {
278 		fprintf(stderr, "ERROR: failed to join group\n");
279 		exit(1);
280 	}
281 	if (close(fds[1]) || close(fds[0])) {
282 		fprintf(stderr, "ERROR: closing sockets\n");
283 		exit(1);
284 	}
285 }
286 
287 /* Test creating a unique fanout group ids */
288 static void test_unique_fanout_group_ids(void)
289 {
290 	int fds[3];
291 	uint16_t typeflags, first_group_id, second_group_id;
292 
293 	fprintf(stderr, "test: unique ids\n");
294 
295 	fds[0] = sock_fanout_open(PACKET_FANOUT_HASH |
296 				  PACKET_FANOUT_FLAG_UNIQUEID, 0);
297 	if (fds[0] == -1) {
298 		fprintf(stderr, "ERROR: failed to create a unique id group.\n");
299 		exit(1);
300 	}
301 
302 	sock_fanout_getopts(fds[0], &typeflags, &first_group_id);
303 	if (typeflags != PACKET_FANOUT_HASH) {
304 		fprintf(stderr, "ERROR: unexpected typeflags %x\n", typeflags);
305 		exit(1);
306 	}
307 
308 	if (sock_fanout_open(PACKET_FANOUT_CPU, first_group_id) != -1) {
309 		fprintf(stderr, "ERROR: joined group with wrong type.\n");
310 		exit(1);
311 	}
312 
313 	fds[1] = sock_fanout_open(PACKET_FANOUT_HASH, first_group_id);
314 	if (fds[1] == -1) {
315 		fprintf(stderr,
316 			"ERROR: failed to join previously created group.\n");
317 		exit(1);
318 	}
319 
320 	fds[2] = sock_fanout_open(PACKET_FANOUT_HASH |
321 				  PACKET_FANOUT_FLAG_UNIQUEID, 0);
322 	if (fds[2] == -1) {
323 		fprintf(stderr,
324 			"ERROR: failed to create a second unique id group.\n");
325 		exit(1);
326 	}
327 
328 	sock_fanout_getopts(fds[2], &typeflags, &second_group_id);
329 	if (sock_fanout_open(PACKET_FANOUT_HASH | PACKET_FANOUT_FLAG_UNIQUEID,
330 			     second_group_id) != -1) {
331 		fprintf(stderr,
332 			"ERROR: specified a group id when requesting unique id\n");
333 		exit(1);
334 	}
335 
336 	if (close(fds[0]) || close(fds[1]) || close(fds[2])) {
337 		fprintf(stderr, "ERROR: closing sockets\n");
338 		exit(1);
339 	}
340 }
341 
342 static int test_datapath(uint16_t typeflags, int port_off,
343 			 const int expect1[], const int expect2[])
344 {
345 	const int expect0[] = { 0, 0 };
346 	char *rings[2];
347 	uint8_t type = typeflags & 0xFF;
348 	int fds[2], fds_udp[2][2], ret;
349 
350 	fprintf(stderr, "test: datapath 0x%hx\n", typeflags);
351 
352 	fds[0] = sock_fanout_open(typeflags, 0);
353 	fds[1] = sock_fanout_open(typeflags, 0);
354 	if (fds[0] == -1 || fds[1] == -1) {
355 		fprintf(stderr, "ERROR: failed open\n");
356 		exit(1);
357 	}
358 	if (type == PACKET_FANOUT_CBPF)
359 		sock_fanout_set_cbpf(fds[0]);
360 	else if (type == PACKET_FANOUT_EBPF)
361 		sock_fanout_set_ebpf(fds[0]);
362 
363 	rings[0] = sock_fanout_open_ring(fds[0]);
364 	rings[1] = sock_fanout_open_ring(fds[1]);
365 	pair_udp_open(fds_udp[0], PORT_BASE);
366 	pair_udp_open(fds_udp[1], PORT_BASE + port_off);
367 	sock_fanout_read(fds, rings, expect0);
368 
369 	/* Send data, but not enough to overflow a queue */
370 	pair_udp_send(fds_udp[0], 15);
371 	pair_udp_send_char(fds_udp[1], 5, DATA_CHAR_1);
372 	ret = sock_fanout_read(fds, rings, expect1);
373 
374 	/* Send more data, overflow the queue */
375 	pair_udp_send_char(fds_udp[0], 15, DATA_CHAR_1);
376 	/* TODO: ensure consistent order between expect1 and expect2 */
377 	ret |= sock_fanout_read(fds, rings, expect2);
378 
379 	if (munmap(rings[1], RING_NUM_FRAMES * getpagesize()) ||
380 	    munmap(rings[0], RING_NUM_FRAMES * getpagesize())) {
381 		fprintf(stderr, "close rings\n");
382 		exit(1);
383 	}
384 	if (close(fds_udp[1][1]) || close(fds_udp[1][0]) ||
385 	    close(fds_udp[0][1]) || close(fds_udp[0][0]) ||
386 	    close(fds[1]) || close(fds[0])) {
387 		fprintf(stderr, "close datapath\n");
388 		exit(1);
389 	}
390 
391 	return ret;
392 }
393 
394 static int set_cpuaffinity(int cpuid)
395 {
396 	cpu_set_t mask;
397 
398 	CPU_ZERO(&mask);
399 	CPU_SET(cpuid, &mask);
400 	if (sched_setaffinity(0, sizeof(mask), &mask)) {
401 		if (errno != EINVAL) {
402 			fprintf(stderr, "setaffinity %d\n", cpuid);
403 			exit(1);
404 		}
405 		return 1;
406 	}
407 
408 	return 0;
409 }
410 
411 int main(int argc, char **argv)
412 {
413 	const int expect_hash[2][2]	= { { 15, 5 },  { 20, 5 } };
414 	const int expect_hash_rb[2][2]	= { { 15, 5 },  { 20, 15 } };
415 	const int expect_lb[2][2]	= { { 10, 10 }, { 18, 17 } };
416 	const int expect_rb[2][2]	= { { 15, 5 },  { 20, 15 } };
417 	const int expect_cpu0[2][2]	= { { 20, 0 },  { 20, 0 } };
418 	const int expect_cpu1[2][2]	= { { 0, 20 },  { 0, 20 } };
419 	const int expect_bpf[2][2]	= { { 15, 5 },  { 15, 20 } };
420 	const int expect_uniqueid[2][2] = { { 20, 20},  { 20, 20 } };
421 	int port_off = 2, tries = 5, ret;
422 
423 	test_control_single();
424 	test_control_group();
425 	test_unique_fanout_group_ids();
426 
427 	/* find a set of ports that do not collide onto the same socket */
428 	ret = test_datapath(PACKET_FANOUT_HASH, port_off,
429 			    expect_hash[0], expect_hash[1]);
430 	while (ret && tries--) {
431 		fprintf(stderr, "info: trying alternate ports (%d)\n", tries);
432 		ret = test_datapath(PACKET_FANOUT_HASH, ++port_off,
433 				    expect_hash[0], expect_hash[1]);
434 	}
435 
436 	ret |= test_datapath(PACKET_FANOUT_HASH | PACKET_FANOUT_FLAG_ROLLOVER,
437 			     port_off, expect_hash_rb[0], expect_hash_rb[1]);
438 	ret |= test_datapath(PACKET_FANOUT_LB,
439 			     port_off, expect_lb[0], expect_lb[1]);
440 	ret |= test_datapath(PACKET_FANOUT_ROLLOVER,
441 			     port_off, expect_rb[0], expect_rb[1]);
442 
443 	ret |= test_datapath(PACKET_FANOUT_CBPF,
444 			     port_off, expect_bpf[0], expect_bpf[1]);
445 	ret |= test_datapath(PACKET_FANOUT_EBPF,
446 			     port_off, expect_bpf[0], expect_bpf[1]);
447 
448 	set_cpuaffinity(0);
449 	ret |= test_datapath(PACKET_FANOUT_CPU, port_off,
450 			     expect_cpu0[0], expect_cpu0[1]);
451 	if (!set_cpuaffinity(1))
452 		/* TODO: test that choice alternates with previous */
453 		ret |= test_datapath(PACKET_FANOUT_CPU, port_off,
454 				     expect_cpu1[0], expect_cpu1[1]);
455 
456 	ret |= test_datapath(PACKET_FANOUT_FLAG_UNIQUEID, port_off,
457 			     expect_uniqueid[0], expect_uniqueid[1]);
458 
459 	if (ret)
460 		return 1;
461 
462 	printf("OK. All tests passed\n");
463 	return 0;
464 }
465