2 * Copyright 2018 Google Inc.
3 * Author: Eric Dumazet (edumazet@google.com)
5 * Reference program demonstrating tcp mmap() usage,
6 * and SO_RCVLOWAT hints for receiver.
8 * Note : NIC with header split is needed to use mmap() on TCP :
9 * Each incoming frame must be a multiple of PAGE_SIZE bytes of TCP payload.
11 * How to use on loopback interface :
13 * ifconfig lo mtu 61512 # 15*4096 + 40 (ipv6 header) + 32 (TCP with TS option header)
17 * Or leave default lo mtu, but use -M option to set TCP_MAXSEG option to (4096 + 12)
18 * (4096 : page size on x86, 12: TCP TS option length)
19 * tcp_mmap -s -z -M $((4096+12)) &
20 * tcp_mmap -H ::1 -z -M $((4096+12))
22 * Note: -z option on sender uses MSG_ZEROCOPY, which forces a copy when packets go through loopback interface.
23 * We might use sendfile() instead, but really this test program is about mmap(), for receivers ;)
25 * $ ./tcp_mmap -s & # Without mmap()
26 * $ for i in {1..4}; do ./tcp_mmap -H ::1 -z ; done
27 * received 32768 MB (0 % mmap'ed) in 14.1157 s, 19.4732 Gbit
28 * cpu usage user:0.057 sys:7.815, 240.234 usec per MB, 65531 c-switches
29 * received 32768 MB (0 % mmap'ed) in 14.6833 s, 18.7204 Gbit
30 * cpu usage user:0.043 sys:8.103, 248.596 usec per MB, 65524 c-switches
31 * received 32768 MB (0 % mmap'ed) in 11.143 s, 24.6682 Gbit
32 * cpu usage user:0.044 sys:6.576, 202.026 usec per MB, 65519 c-switches
33 * received 32768 MB (0 % mmap'ed) in 14.9056 s, 18.4413 Gbit
34 * cpu usage user:0.036 sys:8.193, 251.129 usec per MB, 65530 c-switches
35 * $ kill %1 # kill tcp_mmap server
37 * $ ./tcp_mmap -s -z & # With mmap()
38 * $ for i in {1..4}; do ./tcp_mmap -H ::1 -z ; done
39 * received 32768 MB (99.9939 % mmap'ed) in 6.73792 s, 40.7956 Gbit
40 * cpu usage user:0.045 sys:2.827, 87.6465 usec per MB, 65532 c-switches
41 * received 32768 MB (99.9939 % mmap'ed) in 7.26732 s, 37.8238 Gbit
42 * cpu usage user:0.037 sys:3.087, 95.3369 usec per MB, 65532 c-switches
43 * received 32768 MB (99.9939 % mmap'ed) in 7.61661 s, 36.0893 Gbit
44 * cpu usage user:0.046 sys:3.559, 110.016 usec per MB, 65529 c-switches
45 * received 32768 MB (99.9939 % mmap'ed) in 7.43764 s, 36.9577 Gbit
46 * cpu usage user:0.035 sys:3.467, 106.873 usec per MB, 65530 c-switches
50 * This program is free software; you can redistribute it and/or modify it
51 * under the terms and conditions of the GNU General Public License,
52 * version 2, as published by the Free Software Foundation.
54 * This program is distributed in the hope it will be useful, but WITHOUT
55 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
56 * FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for
59 * You should have received a copy of the GNU General Public License along with
60 * this program; if not, write to the Free Software Foundation, Inc.,
61 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
65 #include <sys/types.h>
68 #include <sys/socket.h>
70 #include <sys/resource.h>
78 #include <netinet/in.h>
79 #include <arpa/inet.h>
81 #include <linux/tcp.h>
85 #define MSG_ZEROCOPY 0x4000000
88 #define FILE_SZ (1UL << 35)
89 static int cfg_family
= AF_INET6
;
90 static socklen_t cfg_alen
= sizeof(struct sockaddr_in6
);
91 static int cfg_port
= 8787;
93 static int rcvbuf
; /* Default: autotuning. Can be set with -r <integer> option */
94 static int sndbuf
; /* Default: autotuning. Can be set with -w <integer> option */
95 static int zflg
; /* zero copy option. (MSG_ZEROCOPY for sender, mmap() for receiver */
96 static int xflg
; /* hash received data (simple xor) (-h option) */
97 static int keepflag
; /* -k option: receiver shall keep all received file in memory (no munmap() calls) */
99 static int chunk_size
= 512*1024;
101 unsigned long htotal
;
103 static inline void prefetch(const void *x
)
105 #if defined(__x86_64__)
106 asm volatile("prefetcht0 %P0" : : "m" (*(const char *)x
));
110 void hash_zone(void *zone
, unsigned int length
)
112 unsigned long temp
= htotal
;
114 while (length
>= 8*sizeof(long)) {
115 prefetch(zone
+ 384);
116 temp
^= *(unsigned long *)zone
;
117 temp
^= *(unsigned long *)(zone
+ sizeof(long));
118 temp
^= *(unsigned long *)(zone
+ 2*sizeof(long));
119 temp
^= *(unsigned long *)(zone
+ 3*sizeof(long));
120 temp
^= *(unsigned long *)(zone
+ 4*sizeof(long));
121 temp
^= *(unsigned long *)(zone
+ 5*sizeof(long));
122 temp
^= *(unsigned long *)(zone
+ 6*sizeof(long));
123 temp
^= *(unsigned long *)(zone
+ 7*sizeof(long));
124 zone
+= 8*sizeof(long);
125 length
-= 8*sizeof(long);
127 while (length
>= 1) {
128 temp
^= *(unsigned char *)zone
;
135 void *child_thread(void *arg
)
137 unsigned long total_mmap
= 0, total
= 0;
138 struct tcp_zerocopy_receive zc
;
139 unsigned long delta_usec
;
140 int flags
= MAP_SHARED
;
141 struct timeval t0
, t1
;
148 fd
= (int)(unsigned long)arg
;
150 gettimeofday(&t0
, NULL
);
152 fcntl(fd
, F_SETFL
, O_NDELAY
);
153 buffer
= malloc(chunk_size
);
159 addr
= mmap(NULL
, chunk_size
, PROT_READ
, flags
, fd
, 0);
160 if (addr
== (void *)-1)
164 struct pollfd pfd
= { .fd
= fd
, .events
= POLLIN
, };
167 poll(&pfd
, 1, 10000);
169 socklen_t zc_len
= sizeof(zc
);
172 zc
.address
= (__u64
)addr
;
173 zc
.length
= chunk_size
;
174 zc
.recv_skip_hint
= 0;
175 res
= getsockopt(fd
, IPPROTO_TCP
, TCP_ZEROCOPY_RECEIVE
,
181 assert(zc
.length
<= chunk_size
);
182 total_mmap
+= zc
.length
;
184 hash_zone(addr
, zc
.length
);
187 if (zc
.recv_skip_hint
) {
188 assert(zc
.recv_skip_hint
<= chunk_size
);
189 lu
= read(fd
, buffer
, zc
.recv_skip_hint
);
192 hash_zone(buffer
, lu
);
199 while (sub
< chunk_size
) {
200 lu
= read(fd
, buffer
+ sub
, chunk_size
- sub
);
206 hash_zone(buffer
+ sub
, lu
);
212 gettimeofday(&t1
, NULL
);
213 delta_usec
= (t1
.tv_sec
- t0
.tv_sec
) * 1000000 + t1
.tv_usec
- t0
.tv_usec
;
217 throughput
= total
* 8.0 / (double)delta_usec
/ 1000.0;
218 getrusage(RUSAGE_THREAD
, &ru
);
219 if (total
> 1024*1024) {
220 unsigned long total_usec
;
221 unsigned long mb
= total
>> 20;
222 total_usec
= 1000000*ru
.ru_utime
.tv_sec
+ ru
.ru_utime
.tv_usec
+
223 1000000*ru
.ru_stime
.tv_sec
+ ru
.ru_stime
.tv_usec
;
224 printf("received %lg MB (%lg %% mmap'ed) in %lg s, %lg Gbit\n"
225 " cpu usage user:%lg sys:%lg, %lg usec per MB, %lu c-switches\n",
226 total
/ (1024.0 * 1024.0),
227 100.0*total_mmap
/total
,
228 (double)delta_usec
/ 1000000.0,
230 (double)ru
.ru_utime
.tv_sec
+ (double)ru
.ru_utime
.tv_usec
/ 1000000.0,
231 (double)ru
.ru_stime
.tv_sec
+ (double)ru
.ru_stime
.tv_usec
/ 1000000.0,
232 (double)total_usec
/mb
,
239 munmap(addr
, chunk_size
);
243 static void apply_rcvsnd_buf(int fd
)
245 if (rcvbuf
&& setsockopt(fd
, SOL_SOCKET
,
246 SO_RCVBUF
, &rcvbuf
, sizeof(rcvbuf
)) == -1) {
247 perror("setsockopt SO_RCVBUF");
250 if (sndbuf
&& setsockopt(fd
, SOL_SOCKET
,
251 SO_SNDBUF
, &sndbuf
, sizeof(sndbuf
)) == -1) {
252 perror("setsockopt SO_SNDBUF");
257 static void setup_sockaddr(int domain
, const char *str_addr
,
258 struct sockaddr_storage
*sockaddr
)
260 struct sockaddr_in6
*addr6
= (void *) sockaddr
;
261 struct sockaddr_in
*addr4
= (void *) sockaddr
;
265 memset(addr4
, 0, sizeof(*addr4
));
266 addr4
->sin_family
= AF_INET
;
267 addr4
->sin_port
= htons(cfg_port
);
269 inet_pton(AF_INET
, str_addr
, &(addr4
->sin_addr
)) != 1)
270 error(1, 0, "ipv4 parse error: %s", str_addr
);
273 memset(addr6
, 0, sizeof(*addr6
));
274 addr6
->sin6_family
= AF_INET6
;
275 addr6
->sin6_port
= htons(cfg_port
);
277 inet_pton(AF_INET6
, str_addr
, &(addr6
->sin6_addr
)) != 1)
278 error(1, 0, "ipv6 parse error: %s", str_addr
);
281 error(1, 0, "illegal domain");
285 static void do_accept(int fdlisten
)
287 if (setsockopt(fdlisten
, SOL_SOCKET
, SO_RCVLOWAT
,
288 &chunk_size
, sizeof(chunk_size
)) == -1) {
289 perror("setsockopt SO_RCVLOWAT");
292 apply_rcvsnd_buf(fdlisten
);
295 struct sockaddr_in addr
;
296 socklen_t addrlen
= sizeof(addr
);
300 fd
= accept(fdlisten
, (struct sockaddr
*)&addr
, &addrlen
);
305 res
= pthread_create(&th
, NULL
, child_thread
,
306 (void *)(unsigned long)fd
);
309 perror("pthread_create");
315 int main(int argc
, char *argv
[])
317 struct sockaddr_storage listenaddr
, addr
;
318 unsigned int max_pacing_rate
= 0;
319 unsigned long total
= 0;
326 while ((c
= getopt(argc
, argv
, "46p:svr:w:H:zxkP:M:")) != -1) {
329 cfg_family
= PF_INET
;
330 cfg_alen
= sizeof(struct sockaddr_in
);
333 cfg_family
= PF_INET6
;
334 cfg_alen
= sizeof(struct sockaddr_in6
);
337 cfg_port
= atoi(optarg
);
342 case 's': /* server : listen for incoming connections */
346 rcvbuf
= atoi(optarg
);
349 sndbuf
= atoi(optarg
);
364 max_pacing_rate
= atoi(optarg
) ;
371 int fdlisten
= socket(cfg_family
, SOCK_STREAM
, 0);
373 if (fdlisten
== -1) {
377 apply_rcvsnd_buf(fdlisten
);
378 setsockopt(fdlisten
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof(on
));
380 setup_sockaddr(cfg_family
, host
, &listenaddr
);
383 setsockopt(fdlisten
, IPPROTO_TCP
, TCP_MAXSEG
,
384 &mss
, sizeof(mss
)) == -1) {
385 perror("setsockopt TCP_MAXSEG");
388 if (bind(fdlisten
, (const struct sockaddr
*)&listenaddr
, cfg_alen
) == -1) {
392 if (listen(fdlisten
, 128) == -1) {
398 buffer
= mmap(NULL
, chunk_size
, PROT_READ
| PROT_WRITE
,
399 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
400 if (buffer
== (char *)-1) {
405 fd
= socket(cfg_family
, SOCK_STREAM
, 0);
410 apply_rcvsnd_buf(fd
);
412 setup_sockaddr(cfg_family
, host
, &addr
);
415 setsockopt(fd
, IPPROTO_TCP
, TCP_MAXSEG
, &mss
, sizeof(mss
)) == -1) {
416 perror("setsockopt TCP_MAXSEG");
419 if (connect(fd
, (const struct sockaddr
*)&addr
, cfg_alen
) == -1) {
423 if (max_pacing_rate
&&
424 setsockopt(fd
, SOL_SOCKET
, SO_MAX_PACING_RATE
,
425 &max_pacing_rate
, sizeof(max_pacing_rate
)) == -1)
426 perror("setsockopt SO_MAX_PACING_RATE");
428 if (zflg
&& setsockopt(fd
, SOL_SOCKET
, SO_ZEROCOPY
,
429 &on
, sizeof(on
)) == -1) {
430 perror("setsockopt SO_ZEROCOPY, (-z option disabled)");
433 while (total
< FILE_SZ
) {
434 long wr
= FILE_SZ
- total
;
438 /* Note : we just want to fill the pipe with 0 bytes */
439 wr
= send(fd
, buffer
, wr
, zflg
? MSG_ZEROCOPY
: 0);
445 munmap(buffer
, chunk_size
);