xref: /linux/tools/testing/selftests/net/psock_fanout.c (revision e5c5d22e8dcf7c2d430336cbf8e180bd38e8daf1)
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  *
23  * Todo:
24  * - functionality: PACKET_FANOUT_FLAG_DEFRAG
25  *
26  * License (GPLv2):
27  *
28  * This program is free software; you can redistribute it and/or modify it
29  * under the terms and conditions of the GNU General Public License,
30  * version 2, as published by the Free Software Foundation.
31  *
32  * This program is distributed in the hope it will be useful, but WITHOUT
33  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
34  * FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for
35  * more details.
36  *
37  * You should have received a copy of the GNU General Public License along with
38  * this program; if not, write to the Free Software Foundation, Inc.,
39  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
40  */
41 
42 #define _GNU_SOURCE		/* for sched_setaffinity */
43 
44 #include <arpa/inet.h>
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <linux/filter.h>
48 #include <linux/if_packet.h>
49 #include <net/ethernet.h>
50 #include <netinet/ip.h>
51 #include <netinet/udp.h>
52 #include <poll.h>
53 #include <sched.h>
54 #include <stdint.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <sys/mman.h>
59 #include <sys/socket.h>
60 #include <sys/stat.h>
61 #include <sys/types.h>
62 #include <unistd.h>
63 
64 #define DATA_LEN			100
65 #define DATA_CHAR			'a'
66 #define RING_NUM_FRAMES			20
67 #define PORT_BASE			8000
68 
69 static void pair_udp_open(int fds[], uint16_t port)
70 {
71 	struct sockaddr_in saddr, daddr;
72 
73 	fds[0] = socket(PF_INET, SOCK_DGRAM, 0);
74 	fds[1] = socket(PF_INET, SOCK_DGRAM, 0);
75 	if (fds[0] == -1 || fds[1] == -1) {
76 		fprintf(stderr, "ERROR: socket dgram\n");
77 		exit(1);
78 	}
79 
80 	memset(&saddr, 0, sizeof(saddr));
81 	saddr.sin_family = AF_INET;
82 	saddr.sin_port = htons(port);
83 	saddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
84 
85 	memset(&daddr, 0, sizeof(daddr));
86 	daddr.sin_family = AF_INET;
87 	daddr.sin_port = htons(port + 1);
88 	daddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
89 
90 	/* must bind both to get consistent hash result */
91 	if (bind(fds[1], (void *) &daddr, sizeof(daddr))) {
92 		perror("bind");
93 		exit(1);
94 	}
95 	if (bind(fds[0], (void *) &saddr, sizeof(saddr))) {
96 		perror("bind");
97 		exit(1);
98 	}
99 	if (connect(fds[0], (void *) &daddr, sizeof(daddr))) {
100 		perror("connect");
101 		exit(1);
102 	}
103 }
104 
105 static void pair_udp_send(int fds[], int num)
106 {
107 	char buf[DATA_LEN], rbuf[DATA_LEN];
108 
109 	memset(buf, DATA_CHAR, sizeof(buf));
110 	while (num--) {
111 		/* Should really handle EINTR and EAGAIN */
112 		if (write(fds[0], buf, sizeof(buf)) != sizeof(buf)) {
113 			fprintf(stderr, "ERROR: send failed left=%d\n", num);
114 			exit(1);
115 		}
116 		if (read(fds[1], rbuf, sizeof(rbuf)) != sizeof(rbuf)) {
117 			fprintf(stderr, "ERROR: recv failed left=%d\n", num);
118 			exit(1);
119 		}
120 		if (memcmp(buf, rbuf, sizeof(buf))) {
121 			fprintf(stderr, "ERROR: data failed left=%d\n", num);
122 			exit(1);
123 		}
124 	}
125 }
126 
127 static void sock_fanout_setfilter(int fd)
128 {
129 	struct sock_filter bpf_filter[] = {
130 		{ 0x80, 0, 0, 0x00000000 },  /* LD  pktlen		      */
131 		{ 0x35, 0, 5, DATA_LEN   },  /* JGE DATA_LEN  [f goto nomatch]*/
132 		{ 0x30, 0, 0, 0x00000050 },  /* LD  ip[80]		      */
133 		{ 0x15, 0, 3, DATA_CHAR  },  /* JEQ DATA_CHAR [f goto nomatch]*/
134 		{ 0x30, 0, 0, 0x00000051 },  /* LD  ip[81]		      */
135 		{ 0x15, 0, 1, DATA_CHAR  },  /* JEQ DATA_CHAR [f goto nomatch]*/
136 		{ 0x6, 0, 0, 0x00000060  },  /* RET match	              */
137 /* nomatch */	{ 0x6, 0, 0, 0x00000000  },  /* RET no match		      */
138 	};
139 	struct sock_fprog bpf_prog;
140 
141 	bpf_prog.filter = bpf_filter;
142 	bpf_prog.len = sizeof(bpf_filter) / sizeof(struct sock_filter);
143 	if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &bpf_prog,
144 		       sizeof(bpf_prog))) {
145 		perror("setsockopt SO_ATTACH_FILTER");
146 		exit(1);
147 	}
148 }
149 
150 /* Open a socket in a given fanout mode.
151  * @return -1 if mode is bad, a valid socket otherwise */
152 static int sock_fanout_open(uint16_t typeflags, int num_packets)
153 {
154 	int fd, val;
155 
156 	fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
157 	if (fd < 0) {
158 		perror("socket packet");
159 		exit(1);
160 	}
161 
162 	/* fanout group ID is always 0: tests whether old groups are deleted */
163 	val = ((int) typeflags) << 16;
164 	if (setsockopt(fd, SOL_PACKET, PACKET_FANOUT, &val, sizeof(val))) {
165 		if (close(fd)) {
166 			perror("close packet");
167 			exit(1);
168 		}
169 		return -1;
170 	}
171 
172 	sock_fanout_setfilter(fd);
173 	return fd;
174 }
175 
176 static char *sock_fanout_open_ring(int fd)
177 {
178 	struct tpacket_req req = {
179 		.tp_block_size = getpagesize(),
180 		.tp_frame_size = getpagesize(),
181 		.tp_block_nr   = RING_NUM_FRAMES,
182 		.tp_frame_nr   = RING_NUM_FRAMES,
183 	};
184 	char *ring;
185 	int val = TPACKET_V2;
186 
187 	if (setsockopt(fd, SOL_PACKET, PACKET_VERSION, (void *) &val,
188 		       sizeof(val))) {
189 		perror("packetsock ring setsockopt version");
190 		exit(1);
191 	}
192 	if (setsockopt(fd, SOL_PACKET, PACKET_RX_RING, (void *) &req,
193 		       sizeof(req))) {
194 		perror("packetsock ring setsockopt");
195 		exit(1);
196 	}
197 
198 	ring = mmap(0, req.tp_block_size * req.tp_block_nr,
199 		    PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
200 	if (!ring) {
201 		fprintf(stderr, "packetsock ring mmap\n");
202 		exit(1);
203 	}
204 
205 	return ring;
206 }
207 
208 static int sock_fanout_read_ring(int fd, void *ring)
209 {
210 	struct tpacket2_hdr *header = ring;
211 	int count = 0;
212 
213 	while (header->tp_status & TP_STATUS_USER && count < RING_NUM_FRAMES) {
214 		count++;
215 		header = ring + (count * getpagesize());
216 	}
217 
218 	return count;
219 }
220 
221 static int sock_fanout_read(int fds[], char *rings[], const int expect[])
222 {
223 	int ret[2];
224 
225 	ret[0] = sock_fanout_read_ring(fds[0], rings[0]);
226 	ret[1] = sock_fanout_read_ring(fds[1], rings[1]);
227 
228 	fprintf(stderr, "info: count=%d,%d, expect=%d,%d\n",
229 			ret[0], ret[1], expect[0], expect[1]);
230 
231 	if ((!(ret[0] == expect[0] && ret[1] == expect[1])) &&
232 	    (!(ret[0] == expect[1] && ret[1] == expect[0]))) {
233 		fprintf(stderr, "ERROR: incorrect queue lengths\n");
234 		return 1;
235 	}
236 
237 	return 0;
238 }
239 
240 /* Test illegal mode + flag combination */
241 static void test_control_single(void)
242 {
243 	fprintf(stderr, "test: control single socket\n");
244 
245 	if (sock_fanout_open(PACKET_FANOUT_ROLLOVER |
246 			       PACKET_FANOUT_FLAG_ROLLOVER, 0) != -1) {
247 		fprintf(stderr, "ERROR: opened socket with dual rollover\n");
248 		exit(1);
249 	}
250 }
251 
252 /* Test illegal group with different modes or flags */
253 static void test_control_group(void)
254 {
255 	int fds[2];
256 
257 	fprintf(stderr, "test: control multiple sockets\n");
258 
259 	fds[0] = sock_fanout_open(PACKET_FANOUT_HASH, 20);
260 	if (fds[0] == -1) {
261 		fprintf(stderr, "ERROR: failed to open HASH socket\n");
262 		exit(1);
263 	}
264 	if (sock_fanout_open(PACKET_FANOUT_HASH |
265 			       PACKET_FANOUT_FLAG_DEFRAG, 10) != -1) {
266 		fprintf(stderr, "ERROR: joined group with wrong flag defrag\n");
267 		exit(1);
268 	}
269 	if (sock_fanout_open(PACKET_FANOUT_HASH |
270 			       PACKET_FANOUT_FLAG_ROLLOVER, 10) != -1) {
271 		fprintf(stderr, "ERROR: joined group with wrong flag ro\n");
272 		exit(1);
273 	}
274 	if (sock_fanout_open(PACKET_FANOUT_CPU, 10) != -1) {
275 		fprintf(stderr, "ERROR: joined group with wrong mode\n");
276 		exit(1);
277 	}
278 	fds[1] = sock_fanout_open(PACKET_FANOUT_HASH, 20);
279 	if (fds[1] == -1) {
280 		fprintf(stderr, "ERROR: failed to join group\n");
281 		exit(1);
282 	}
283 	if (close(fds[1]) || close(fds[0])) {
284 		fprintf(stderr, "ERROR: closing sockets\n");
285 		exit(1);
286 	}
287 }
288 
289 static int test_datapath(uint16_t typeflags, int port_off,
290 			 const int expect1[], const int expect2[])
291 {
292 	const int expect0[] = { 0, 0 };
293 	char *rings[2];
294 	int fds[2], fds_udp[2][2], ret;
295 
296 	fprintf(stderr, "test: datapath 0x%hx\n", typeflags);
297 
298 	fds[0] = sock_fanout_open(typeflags, 20);
299 	fds[1] = sock_fanout_open(typeflags, 20);
300 	if (fds[0] == -1 || fds[1] == -1) {
301 		fprintf(stderr, "ERROR: failed open\n");
302 		exit(1);
303 	}
304 	rings[0] = sock_fanout_open_ring(fds[0]);
305 	rings[1] = sock_fanout_open_ring(fds[1]);
306 	pair_udp_open(fds_udp[0], PORT_BASE);
307 	pair_udp_open(fds_udp[1], PORT_BASE + port_off);
308 	sock_fanout_read(fds, rings, expect0);
309 
310 	/* Send data, but not enough to overflow a queue */
311 	pair_udp_send(fds_udp[0], 15);
312 	pair_udp_send(fds_udp[1], 5);
313 	ret = sock_fanout_read(fds, rings, expect1);
314 
315 	/* Send more data, overflow the queue */
316 	pair_udp_send(fds_udp[0], 15);
317 	/* TODO: ensure consistent order between expect1 and expect2 */
318 	ret |= sock_fanout_read(fds, rings, expect2);
319 
320 	if (munmap(rings[1], RING_NUM_FRAMES * getpagesize()) ||
321 	    munmap(rings[0], RING_NUM_FRAMES * getpagesize())) {
322 		fprintf(stderr, "close rings\n");
323 		exit(1);
324 	}
325 	if (close(fds_udp[1][1]) || close(fds_udp[1][0]) ||
326 	    close(fds_udp[0][1]) || close(fds_udp[0][0]) ||
327 	    close(fds[1]) || close(fds[0])) {
328 		fprintf(stderr, "close datapath\n");
329 		exit(1);
330 	}
331 
332 	return ret;
333 }
334 
335 static int set_cpuaffinity(int cpuid)
336 {
337 	cpu_set_t mask;
338 
339 	CPU_ZERO(&mask);
340 	CPU_SET(cpuid, &mask);
341 	if (sched_setaffinity(0, sizeof(mask), &mask)) {
342 		if (errno != EINVAL) {
343 			fprintf(stderr, "setaffinity %d\n", cpuid);
344 			exit(1);
345 		}
346 		return 1;
347 	}
348 
349 	return 0;
350 }
351 
352 int main(int argc, char **argv)
353 {
354 	const int expect_hash[2][2]	= { { 15, 5 },  { 20, 5 } };
355 	const int expect_hash_rb[2][2]	= { { 15, 5 },  { 20, 15 } };
356 	const int expect_lb[2][2]	= { { 10, 10 }, { 18, 17 } };
357 	const int expect_rb[2][2]	= { { 20, 0 },  { 20, 15 } };
358 	const int expect_cpu0[2][2]	= { { 20, 0 },  { 20, 0 } };
359 	const int expect_cpu1[2][2]	= { { 0, 20 },  { 0, 20 } };
360 	int port_off = 2, tries = 5, ret;
361 
362 	test_control_single();
363 	test_control_group();
364 
365 	/* find a set of ports that do not collide onto the same socket */
366 	ret = test_datapath(PACKET_FANOUT_HASH, port_off,
367 			    expect_hash[0], expect_hash[1]);
368 	while (ret && tries--) {
369 		fprintf(stderr, "info: trying alternate ports (%d)\n", tries);
370 		ret = test_datapath(PACKET_FANOUT_HASH, ++port_off,
371 				    expect_hash[0], expect_hash[1]);
372 	}
373 
374 	ret |= test_datapath(PACKET_FANOUT_HASH | PACKET_FANOUT_FLAG_ROLLOVER,
375 			     port_off, expect_hash_rb[0], expect_hash_rb[1]);
376 	ret |= test_datapath(PACKET_FANOUT_LB,
377 			     port_off, expect_lb[0], expect_lb[1]);
378 	ret |= test_datapath(PACKET_FANOUT_ROLLOVER,
379 			     port_off, expect_rb[0], expect_rb[1]);
380 
381 	set_cpuaffinity(0);
382 	ret |= test_datapath(PACKET_FANOUT_CPU, port_off,
383 			     expect_cpu0[0], expect_cpu0[1]);
384 	if (!set_cpuaffinity(1))
385 		/* TODO: test that choice alternates with previous */
386 		ret |= test_datapath(PACKET_FANOUT_CPU, port_off,
387 				     expect_cpu1[0], expect_cpu1[1]);
388 
389 	if (ret)
390 		return 1;
391 
392 	printf("OK. All tests passed\n");
393 	return 0;
394 }
395