xref: /linux/tools/testing/selftests/net/tcp_mmap.c (revision fafb66e5903c2bcfc7b7e259042a8282f18a6faa)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright 2018 Google Inc.
4  * Author: Eric Dumazet (edumazet@google.com)
5  *
6  * Reference program demonstrating tcp mmap() usage,
7  * and SO_RCVLOWAT hints for receiver.
8  *
9  * Note : NIC with header split is needed to use mmap() on TCP :
10  * Each incoming frame must be a multiple of PAGE_SIZE bytes of TCP payload.
11  *
12  * How to use on loopback interface :
13  *
14  *  ifconfig lo mtu 61512  # 15*4096 + 40 (ipv6 header) + 32 (TCP with TS option header)
15  *  tcp_mmap -s -z &
16  *  tcp_mmap -H ::1 -z
17  *
18  *  Or leave default lo mtu, but use -M option to set TCP_MAXSEG option to (4096 + 12)
19  *      (4096 : page size on x86, 12: TCP TS option length)
20  *  tcp_mmap -s -z -M $((4096+12)) &
21  *  tcp_mmap -H ::1 -z -M $((4096+12))
22  *
23  * Note: -z option on sender uses MSG_ZEROCOPY, which forces a copy when packets go through loopback interface.
24  *       We might use sendfile() instead, but really this test program is about mmap(), for receivers ;)
25  *
26  * $ ./tcp_mmap -s &                                 # Without mmap()
27  * $ for i in {1..4}; do ./tcp_mmap -H ::1 -z ; done
28  * received 32768 MB (0 % mmap'ed) in 14.1157 s, 19.4732 Gbit
29  *   cpu usage user:0.057 sys:7.815, 240.234 usec per MB, 65531 c-switches
30  * received 32768 MB (0 % mmap'ed) in 14.6833 s, 18.7204 Gbit
31  *  cpu usage user:0.043 sys:8.103, 248.596 usec per MB, 65524 c-switches
32  * received 32768 MB (0 % mmap'ed) in 11.143 s, 24.6682 Gbit
33  *   cpu usage user:0.044 sys:6.576, 202.026 usec per MB, 65519 c-switches
34  * received 32768 MB (0 % mmap'ed) in 14.9056 s, 18.4413 Gbit
35  *   cpu usage user:0.036 sys:8.193, 251.129 usec per MB, 65530 c-switches
36  * $ kill %1   # kill tcp_mmap server
37  *
38  * $ ./tcp_mmap -s -z &                              # With mmap()
39  * $ for i in {1..4}; do ./tcp_mmap -H ::1 -z ; done
40  * received 32768 MB (99.9939 % mmap'ed) in 6.73792 s, 40.7956 Gbit
41  *   cpu usage user:0.045 sys:2.827, 87.6465 usec per MB, 65532 c-switches
42  * received 32768 MB (99.9939 % mmap'ed) in 7.26732 s, 37.8238 Gbit
43  *   cpu usage user:0.037 sys:3.087, 95.3369 usec per MB, 65532 c-switches
44  * received 32768 MB (99.9939 % mmap'ed) in 7.61661 s, 36.0893 Gbit
45  *   cpu usage user:0.046 sys:3.559, 110.016 usec per MB, 65529 c-switches
46  * received 32768 MB (99.9939 % mmap'ed) in 7.43764 s, 36.9577 Gbit
47  *   cpu usage user:0.035 sys:3.467, 106.873 usec per MB, 65530 c-switches
48  */
49 #define _GNU_SOURCE
50 #include <pthread.h>
51 #include <sys/types.h>
52 #include <fcntl.h>
53 #include <error.h>
54 #include <sys/socket.h>
55 #include <sys/mman.h>
56 #include <sys/resource.h>
57 #include <unistd.h>
58 #include <string.h>
59 #include <stdlib.h>
60 #include <stdio.h>
61 #include <errno.h>
62 #include <time.h>
63 #include <sys/time.h>
64 #include <netinet/in.h>
65 #include <arpa/inet.h>
66 #include <poll.h>
67 #include <linux/tcp.h>
68 #include <assert.h>
69 #include <openssl/pem.h>
70 
71 #ifndef MSG_ZEROCOPY
72 #define MSG_ZEROCOPY    0x4000000
73 #endif
74 
75 #ifndef min
76 #define min(a, b)  ((a) < (b) ? (a) : (b))
77 #endif
78 
79 #define FILE_SZ (1ULL << 35)
80 static int cfg_family = AF_INET6;
81 static socklen_t cfg_alen = sizeof(struct sockaddr_in6);
82 static int cfg_port = 8787;
83 
84 static int rcvbuf; /* Default: autotuning.  Can be set with -r <integer> option */
85 static int sndbuf; /* Default: autotuning.  Can be set with -w <integer> option */
86 static int zflg; /* zero copy option. (MSG_ZEROCOPY for sender, mmap() for receiver */
87 static int xflg; /* hash received data (simple xor) (-h option) */
88 static int keepflag; /* -k option: receiver shall keep all received file in memory (no munmap() calls) */
89 static int integrity; /* -i option: sender and receiver compute sha256 over the data.*/
90 
91 static size_t chunk_size  = 512*1024;
92 
93 static size_t map_align;
94 
95 unsigned long htotal;
96 unsigned int digest_len;
97 
98 static inline void prefetch(const void *x)
99 {
100 #if defined(__x86_64__)
101 	asm volatile("prefetcht0 %P0" : : "m" (*(const char *)x));
102 #endif
103 }
104 
105 void hash_zone(void *zone, unsigned int length)
106 {
107 	unsigned long temp = htotal;
108 
109 	while (length >= 8*sizeof(long)) {
110 		prefetch(zone + 384);
111 		temp ^= *(unsigned long *)zone;
112 		temp ^= *(unsigned long *)(zone + sizeof(long));
113 		temp ^= *(unsigned long *)(zone + 2*sizeof(long));
114 		temp ^= *(unsigned long *)(zone + 3*sizeof(long));
115 		temp ^= *(unsigned long *)(zone + 4*sizeof(long));
116 		temp ^= *(unsigned long *)(zone + 5*sizeof(long));
117 		temp ^= *(unsigned long *)(zone + 6*sizeof(long));
118 		temp ^= *(unsigned long *)(zone + 7*sizeof(long));
119 		zone += 8*sizeof(long);
120 		length -= 8*sizeof(long);
121 	}
122 	while (length >= 1) {
123 		temp ^= *(unsigned char *)zone;
124 		zone += 1;
125 		length--;
126 	}
127 	htotal = temp;
128 }
129 
130 #define ALIGN_UP(x, align_to)	(((x) + ((align_to)-1)) & ~((align_to)-1))
131 #define ALIGN_PTR_UP(p, ptr_align_to)	((typeof(p))ALIGN_UP((unsigned long)(p), ptr_align_to))
132 
133 
134 static void *mmap_large_buffer(size_t need, size_t *allocated)
135 {
136 	void *buffer;
137 	size_t sz;
138 
139 	/* Attempt to use huge pages if possible. */
140 	sz = ALIGN_UP(need, map_align);
141 	buffer = mmap(NULL, sz, PROT_READ | PROT_WRITE,
142 		      MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB, -1, 0);
143 
144 	if (buffer == (void *)-1) {
145 		sz = need;
146 		buffer = mmap(NULL, sz, PROT_READ | PROT_WRITE,
147 			      MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE,
148 			      -1, 0);
149 		if (buffer != (void *)-1)
150 			fprintf(stderr, "MAP_HUGETLB attempt failed, look at /sys/kernel/mm/hugepages for optimal performance\n");
151 	}
152 	*allocated = sz;
153 	return buffer;
154 }
155 
156 static uint32_t tcp_info_get_rcv_mss(int fd)
157 {
158 	socklen_t sz = sizeof(struct tcp_info);
159 	struct tcp_info info;
160 
161 	if (getsockopt(fd, IPPROTO_TCP, TCP_INFO, &info, &sz)) {
162 		fprintf(stderr, "Error fetching TCP_INFO\n");
163 		return 0;
164 	}
165 
166 	return info.tcpi_rcv_mss;
167 }
168 
169 void *child_thread(void *arg)
170 {
171 	unsigned char digest[SHA256_DIGEST_LENGTH];
172 	unsigned long total_mmap = 0, total = 0;
173 	struct tcp_zerocopy_receive zc;
174 	unsigned char *buffer = NULL;
175 	unsigned long delta_usec;
176 	EVP_MD_CTX *ctx = NULL;
177 	int flags = MAP_SHARED;
178 	struct timeval t0, t1;
179 	void *raddr = NULL;
180 	void *addr = NULL;
181 	double throughput;
182 	struct rusage ru;
183 	size_t buffer_sz;
184 	int lu, fd;
185 
186 	fd = (int)(unsigned long)arg;
187 
188 	gettimeofday(&t0, NULL);
189 
190 	fcntl(fd, F_SETFL, O_NDELAY);
191 	buffer = mmap_large_buffer(chunk_size, &buffer_sz);
192 	if (buffer == (void *)-1) {
193 		perror("mmap");
194 		goto error;
195 	}
196 	if (zflg) {
197 		raddr = mmap(NULL, chunk_size + map_align, PROT_READ, flags, fd, 0);
198 		if (raddr == (void *)-1) {
199 			perror("mmap");
200 			zflg = 0;
201 		} else {
202 			addr = ALIGN_PTR_UP(raddr, map_align);
203 		}
204 	}
205 	if (integrity) {
206 		ctx = EVP_MD_CTX_new();
207 		if (!ctx) {
208 			perror("cannot enable SHA computing");
209 			goto error;
210 		}
211 		EVP_DigestInit_ex(ctx, EVP_sha256(), NULL);
212 	}
213 	while (1) {
214 		struct pollfd pfd = { .fd = fd, .events = POLLIN, };
215 		int sub;
216 
217 		poll(&pfd, 1, 10000);
218 		if (zflg) {
219 			socklen_t zc_len = sizeof(zc);
220 			int res;
221 
222 			memset(&zc, 0, sizeof(zc));
223 			zc.address = (__u64)((unsigned long)addr);
224 			zc.length = min(chunk_size, FILE_SZ - total);
225 
226 			res = getsockopt(fd, IPPROTO_TCP, TCP_ZEROCOPY_RECEIVE,
227 					 &zc, &zc_len);
228 			if (res == -1)
229 				break;
230 
231 			if (zc.length) {
232 				assert(zc.length <= chunk_size);
233 				if (integrity)
234 					EVP_DigestUpdate(ctx, addr, zc.length);
235 				total_mmap += zc.length;
236 				if (xflg)
237 					hash_zone(addr, zc.length);
238 				/* It is more efficient to unmap the pages right now,
239 				 * instead of doing this in next TCP_ZEROCOPY_RECEIVE.
240 				 */
241 				madvise(addr, zc.length, MADV_DONTNEED);
242 				total += zc.length;
243 			}
244 			if (zc.recv_skip_hint) {
245 				assert(zc.recv_skip_hint <= chunk_size);
246 				lu = read(fd, buffer, min(zc.recv_skip_hint,
247 							  FILE_SZ - total));
248 				if (lu > 0) {
249 					if (integrity)
250 						EVP_DigestUpdate(ctx, buffer, lu);
251 					if (xflg)
252 						hash_zone(buffer, lu);
253 					total += lu;
254 				}
255 				if (lu == 0)
256 					goto end;
257 			}
258 			continue;
259 		}
260 		sub = 0;
261 		while (sub < chunk_size) {
262 			lu = read(fd, buffer + sub, min(chunk_size - sub,
263 							FILE_SZ - total));
264 			if (lu == 0)
265 				goto end;
266 			if (lu < 0)
267 				break;
268 			if (integrity)
269 				EVP_DigestUpdate(ctx, buffer + sub, lu);
270 			if (xflg)
271 				hash_zone(buffer + sub, lu);
272 			total += lu;
273 			sub += lu;
274 		}
275 	}
276 end:
277 	gettimeofday(&t1, NULL);
278 	delta_usec = (t1.tv_sec - t0.tv_sec) * 1000000 + t1.tv_usec - t0.tv_usec;
279 
280 	if (integrity) {
281 		fcntl(fd, F_SETFL, 0);
282 		EVP_DigestFinal_ex(ctx, digest, &digest_len);
283 		lu = read(fd, buffer, SHA256_DIGEST_LENGTH);
284 		if (lu != SHA256_DIGEST_LENGTH)
285 			perror("Error: Cannot read SHA256\n");
286 
287 		if (memcmp(digest, buffer,
288 			   SHA256_DIGEST_LENGTH))
289 			fprintf(stderr, "Error: SHA256 of the data is not right\n");
290 		else
291 			printf("\nSHA256 is correct\n");
292 	}
293 
294 	throughput = 0;
295 	if (delta_usec)
296 		throughput = total * 8.0 / (double)delta_usec / 1000.0;
297 	getrusage(RUSAGE_THREAD, &ru);
298 	if (total > 1024*1024) {
299 		unsigned long total_usec;
300 		unsigned long mb = total >> 20;
301 		total_usec = 1000000*ru.ru_utime.tv_sec + ru.ru_utime.tv_usec +
302 			     1000000*ru.ru_stime.tv_sec + ru.ru_stime.tv_usec;
303 		printf("received %lg MB (%lg %% mmap'ed) in %lg s, %lg Gbit\n"
304 		       "  cpu usage user:%lg sys:%lg, %lg usec per MB, %lu c-switches, rcv_mss %u\n",
305 				total / (1024.0 * 1024.0),
306 				100.0*total_mmap/total,
307 				(double)delta_usec / 1000000.0,
308 				throughput,
309 				(double)ru.ru_utime.tv_sec + (double)ru.ru_utime.tv_usec / 1000000.0,
310 				(double)ru.ru_stime.tv_sec + (double)ru.ru_stime.tv_usec / 1000000.0,
311 				(double)total_usec/mb,
312 				ru.ru_nvcsw,
313 				tcp_info_get_rcv_mss(fd));
314 	}
315 error:
316 	if (ctx)
317 		EVP_MD_CTX_free(ctx);
318 	munmap(buffer, buffer_sz);
319 	close(fd);
320 	if (zflg)
321 		munmap(raddr, chunk_size + map_align);
322 	pthread_exit(0);
323 }
324 
325 static void apply_rcvsnd_buf(int fd)
326 {
327 	if (rcvbuf && setsockopt(fd, SOL_SOCKET,
328 				 SO_RCVBUF, &rcvbuf, sizeof(rcvbuf)) == -1) {
329 		perror("setsockopt SO_RCVBUF");
330 	}
331 
332 	if (sndbuf && setsockopt(fd, SOL_SOCKET,
333 				 SO_SNDBUF, &sndbuf, sizeof(sndbuf)) == -1) {
334 		perror("setsockopt SO_SNDBUF");
335 	}
336 }
337 
338 
339 static void setup_sockaddr(int domain, const char *str_addr,
340 			   struct sockaddr_storage *sockaddr)
341 {
342 	struct sockaddr_in6 *addr6 = (void *) sockaddr;
343 	struct sockaddr_in *addr4 = (void *) sockaddr;
344 
345 	switch (domain) {
346 	case PF_INET:
347 		memset(addr4, 0, sizeof(*addr4));
348 		addr4->sin_family = AF_INET;
349 		addr4->sin_port = htons(cfg_port);
350 		if (str_addr &&
351 		    inet_pton(AF_INET, str_addr, &(addr4->sin_addr)) != 1)
352 			error(1, 0, "ipv4 parse error: %s", str_addr);
353 		break;
354 	case PF_INET6:
355 		memset(addr6, 0, sizeof(*addr6));
356 		addr6->sin6_family = AF_INET6;
357 		addr6->sin6_port = htons(cfg_port);
358 		if (str_addr &&
359 		    inet_pton(AF_INET6, str_addr, &(addr6->sin6_addr)) != 1)
360 			error(1, 0, "ipv6 parse error: %s", str_addr);
361 		break;
362 	default:
363 		error(1, 0, "illegal domain");
364 	}
365 }
366 
367 static void do_accept(int fdlisten)
368 {
369 	pthread_attr_t attr;
370 	int rcvlowat;
371 
372 	pthread_attr_init(&attr);
373 	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
374 
375 	rcvlowat = chunk_size;
376 	if (setsockopt(fdlisten, SOL_SOCKET, SO_RCVLOWAT,
377 		       &rcvlowat, sizeof(rcvlowat)) == -1) {
378 		perror("setsockopt SO_RCVLOWAT");
379 	}
380 
381 	apply_rcvsnd_buf(fdlisten);
382 
383 	while (1) {
384 		struct sockaddr_in addr;
385 		socklen_t addrlen = sizeof(addr);
386 		pthread_t th;
387 		int fd, res;
388 
389 		fd = accept(fdlisten, (struct sockaddr *)&addr, &addrlen);
390 		if (fd == -1) {
391 			perror("accept");
392 			continue;
393 		}
394 		res = pthread_create(&th, &attr, child_thread,
395 				     (void *)(unsigned long)fd);
396 		if (res) {
397 			errno = res;
398 			perror("pthread_create");
399 			close(fd);
400 		}
401 	}
402 }
403 
404 /* Each thread should reserve a big enough vma to avoid
405  * spinlock collisions in ptl locks.
406  * This size is 2MB on x86_64, and is exported in /proc/meminfo.
407  */
408 static unsigned long default_huge_page_size(void)
409 {
410 	FILE *f = fopen("/proc/meminfo", "r");
411 	unsigned long hps = 0;
412 	size_t linelen = 0;
413 	char *line = NULL;
414 
415 	if (!f)
416 		return 0;
417 	while (getline(&line, &linelen, f) > 0) {
418 		if (sscanf(line, "Hugepagesize:       %lu kB", &hps) == 1) {
419 			hps <<= 10;
420 			break;
421 		}
422 	}
423 	free(line);
424 	fclose(f);
425 	return hps;
426 }
427 
428 static void randomize(void *target, size_t count)
429 {
430 	static int urandom = -1;
431 	ssize_t got;
432 
433 	urandom = open("/dev/urandom", O_RDONLY);
434 	if (urandom < 0) {
435 		perror("open /dev/urandom");
436 		exit(1);
437 	}
438 	got = read(urandom, target, count);
439 	if (got != count) {
440 		perror("read /dev/urandom");
441 		exit(1);
442 	}
443 }
444 
445 int main(int argc, char *argv[])
446 {
447 	unsigned char digest[SHA256_DIGEST_LENGTH];
448 	struct sockaddr_storage listenaddr, addr;
449 	unsigned int max_pacing_rate = 0;
450 	EVP_MD_CTX *ctx = NULL;
451 	unsigned char *buffer;
452 	uint64_t total = 0;
453 	char *host = NULL;
454 	int fd, c, on = 1;
455 	size_t buffer_sz;
456 	int sflg = 0;
457 	int mss = 0;
458 
459 	while ((c = getopt(argc, argv, "46p:svr:w:H:zxkP:M:C:a:i")) != -1) {
460 		switch (c) {
461 		case '4':
462 			cfg_family = PF_INET;
463 			cfg_alen = sizeof(struct sockaddr_in);
464 			break;
465 		case '6':
466 			cfg_family = PF_INET6;
467 			cfg_alen = sizeof(struct sockaddr_in6);
468 			break;
469 		case 'p':
470 			cfg_port = atoi(optarg);
471 			break;
472 		case 'H':
473 			host = optarg;
474 			break;
475 		case 's': /* server : listen for incoming connections */
476 			sflg++;
477 			break;
478 		case 'r':
479 			rcvbuf = atoi(optarg);
480 			break;
481 		case 'w':
482 			sndbuf = atoi(optarg);
483 			break;
484 		case 'z':
485 			zflg = 1;
486 			break;
487 		case 'M':
488 			mss = atoi(optarg);
489 			break;
490 		case 'x':
491 			xflg = 1;
492 			break;
493 		case 'k':
494 			keepflag = 1;
495 			break;
496 		case 'P':
497 			max_pacing_rate = atoi(optarg) ;
498 			break;
499 		case 'C':
500 			chunk_size = atol(optarg);
501 			break;
502 		case 'a':
503 			map_align = atol(optarg);
504 			break;
505 		case 'i':
506 			integrity = 1;
507 			break;
508 		default:
509 			exit(1);
510 		}
511 	}
512 	if (!map_align) {
513 		map_align = default_huge_page_size();
514 		/* if really /proc/meminfo is not helping,
515 		 * we use the default x86_64 hugepagesize.
516 		 */
517 		if (!map_align)
518 			map_align = 2*1024*1024;
519 	}
520 	if (sflg) {
521 		int fdlisten = socket(cfg_family, SOCK_STREAM, 0);
522 
523 		if (fdlisten == -1) {
524 			perror("socket");
525 			exit(1);
526 		}
527 		apply_rcvsnd_buf(fdlisten);
528 		setsockopt(fdlisten, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
529 
530 		setup_sockaddr(cfg_family, host, &listenaddr);
531 
532 		if (mss &&
533 		    setsockopt(fdlisten, IPPROTO_TCP, TCP_MAXSEG,
534 			       &mss, sizeof(mss)) == -1) {
535 			perror("setsockopt TCP_MAXSEG");
536 			exit(1);
537 		}
538 		if (bind(fdlisten, (const struct sockaddr *)&listenaddr, cfg_alen) == -1) {
539 			perror("bind");
540 			exit(1);
541 		}
542 		if (listen(fdlisten, 128) == -1) {
543 			perror("listen");
544 			exit(1);
545 		}
546 		do_accept(fdlisten);
547 	}
548 
549 	buffer = mmap_large_buffer(chunk_size, &buffer_sz);
550 	if (buffer == (unsigned char *)-1) {
551 		perror("mmap");
552 		exit(1);
553 	}
554 
555 	fd = socket(cfg_family, SOCK_STREAM, 0);
556 	if (fd == -1) {
557 		perror("socket");
558 		exit(1);
559 	}
560 	apply_rcvsnd_buf(fd);
561 
562 	setup_sockaddr(cfg_family, host, &addr);
563 
564 	if (mss &&
565 	    setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &mss, sizeof(mss)) == -1) {
566 		perror("setsockopt TCP_MAXSEG");
567 		exit(1);
568 	}
569 	if (connect(fd, (const struct sockaddr *)&addr, cfg_alen) == -1) {
570 		perror("connect");
571 		exit(1);
572 	}
573 	if (max_pacing_rate &&
574 	    setsockopt(fd, SOL_SOCKET, SO_MAX_PACING_RATE,
575 		       &max_pacing_rate, sizeof(max_pacing_rate)) == -1)
576 		perror("setsockopt SO_MAX_PACING_RATE");
577 
578 	if (zflg && setsockopt(fd, SOL_SOCKET, SO_ZEROCOPY,
579 			       &on, sizeof(on)) == -1) {
580 		perror("setsockopt SO_ZEROCOPY, (-z option disabled)");
581 		zflg = 0;
582 	}
583 	if (integrity) {
584 		randomize(buffer, buffer_sz);
585 		ctx = EVP_MD_CTX_new();
586 		if (!ctx) {
587 			perror("cannot enable SHA computing");
588 			exit(1);
589 		}
590 		EVP_DigestInit_ex(ctx, EVP_sha256(), NULL);
591 	}
592 	while (total < FILE_SZ) {
593 		size_t offset = total % chunk_size;
594 		int64_t wr = FILE_SZ - total;
595 
596 		if (wr > chunk_size - offset)
597 			wr = chunk_size - offset;
598 		/* Note : we just want to fill the pipe with random bytes */
599 		wr = send(fd, buffer + offset,
600 			  (size_t)wr, zflg ? MSG_ZEROCOPY : 0);
601 		if (wr <= 0)
602 			break;
603 		if (integrity)
604 			EVP_DigestUpdate(ctx, buffer + offset, wr);
605 		total += wr;
606 	}
607 	if (integrity && total == FILE_SZ) {
608 		EVP_DigestFinal_ex(ctx, digest, &digest_len);
609 		send(fd, digest, (size_t)SHA256_DIGEST_LENGTH, 0);
610 	}
611 	if (ctx)
612 		EVP_MD_CTX_free(ctx);
613 	close(fd);
614 	munmap(buffer, buffer_sz);
615 	return 0;
616 }
617