xref: /aosp_15_r20/external/ltp/testcases/network/netstress/netstress.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2014-2016 Oracle and/or its affiliates. All Rights Reserved.
4  * Copyright (c) 2014-2023 Petr Vorel <[email protected]>
5  * Author: Alexey Kodanev <[email protected]>
6  */
7 
8 #include <pthread.h>
9 #include <stdlib.h>
10 #include <limits.h>
11 #include <linux/dccp.h>
12 #include <sys/types.h>
13 #include <sys/socket.h>
14 #include <netdb.h>
15 #include <netinet/in.h>
16 #include <netinet/tcp.h>
17 #include <arpa/inet.h>
18 #include <poll.h>
19 #include <time.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <errno.h>
23 
24 #include "lapi/udp.h"
25 #include "lapi/dccp.h"
26 #include "lapi/netinet_in.h"
27 #include "lapi/posix_clocks.h"
28 #include "lapi/socket.h"
29 #include "lapi/tcp.h"
30 #include "tst_safe_stdio.h"
31 #include "tst_safe_pthread.h"
32 #include "tst_test.h"
33 #include "tst_safe_net.h"
34 
35 #if !defined(HAVE_RAND_R)
rand_r(LTP_ATTRIBUTE_UNUSED unsigned int * seed)36 static int rand_r(LTP_ATTRIBUTE_UNUSED unsigned int *seed)
37 {
38     return rand();
39 }
40 #endif
41 
42 static const int max_msg_len = (1 << 16) - 1;
43 static const int min_msg_len = 5;
44 
45 enum {
46 	SERVER_HOST = 0,
47 	CLIENT_HOST,
48 };
49 static char *client_mode;
50 
51 enum {
52 	TFO_DISABLED = 0,
53 	TFO_ENABLED,
54 };
55 static int tfo_value = -1;
56 static char *fastopen_api, *fastopen_sapi;
57 
58 static const char tfo_cfg[]		= "/proc/sys/net/ipv4/tcp_fastopen";
59 static const char tcp_tw_reuse[]	= "/proc/sys/net/ipv4/tcp_tw_reuse";
60 static int tw_reuse_changed;
61 static int tfo_cfg_value;
62 static int tfo_cfg_changed;
63 static int tfo_queue_size	= 100;
64 static int max_queue_len	= 100;
65 static const int client_byte	= 0x43;
66 static const int server_byte	= 0x53;
67 static const int start_byte	= 0x24;
68 static const int start_fin_byte	= 0x25;
69 static const int end_byte	= 0x0a;
70 static int init_cln_msg_len	= 32;
71 static int init_srv_msg_len	= 128;
72 static int max_rand_msg_len;
73 static int init_seed;
74 
75 /*
76  * The number of requests from client after
77  * which server has to close the connection.
78  */
79 static int server_max_requests	= 3;
80 static int client_max_requests	= 10;
81 static int clients_num;
82 static char *tcp_port;
83 static char *server_addr;
84 static char *source_addr;
85 static char *server_bg;
86 static int busy_poll		= -1;
87 static int max_etime_cnt = 21; /* ~60 sec max timeout if no connection */
88 static int max_eshutdown_cnt = 10;
89 static int max_pmtu_err = 10;
90 
91 enum {
92 	TYPE_TCP = 0,
93 	TYPE_UDP,
94 	TYPE_UDP_LITE,
95 	TYPE_DCCP,
96 	TYPE_SCTP
97 };
98 static uint proto_type;
99 static char *type;
100 static char *dev;
101 static int sock_type = SOCK_STREAM;
102 static int protocol;
103 static int family = AF_INET6;
104 
105 static uint32_t service_code = 0xffff;
106 
107 /* server socket */
108 static int sfd;
109 
110 /* how long a client must wait for the server's reply */
111 static int wait_timeout = 60000;
112 
113 /* in the end test will save time result in this file */
114 static char *rpath;
115 static char *port_path = "netstress_port";
116 static char *log_path = "netstress.log";
117 
118 static char *narg, *Narg, *qarg, *rarg, *Rarg, *aarg, *Targ, *barg, *targ,
119 	    *Aarg;
120 
121 /* common structure for TCP/UDP server and TCP/UDP client */
122 struct net_func {
123 	void (*init)(void);
124 	void (*run)(void);
125 	void (*cleanup)(void);
126 };
127 static struct net_func net;
128 
129 #define MAX_THREADS	10000
130 static pthread_attr_t attr;
131 static pthread_t *thread_ids;
132 
133 static struct addrinfo *remote_addrinfo;
134 static struct addrinfo *local_addrinfo;
135 
136 struct sock_info {
137 	int fd;
138 	struct sockaddr_storage raddr;
139 	socklen_t raddr_len;
140 	int etime_cnt;
141 	int pmtu_err_cnt;
142 	int eshutdown_cnt;
143 	int timeout;
144 };
145 
146 static char *zcopy;
147 static int send_flags = MSG_NOSIGNAL;
148 static char *reuse_port;
149 
init_socket_opts(int sd)150 static void init_socket_opts(int sd)
151 {
152 	if (busy_poll >= 0)
153 		SAFE_SETSOCKOPT_INT(sd, SOL_SOCKET, SO_BUSY_POLL, busy_poll);
154 
155 	if (dev)
156 		SAFE_SETSOCKOPT(sd, SOL_SOCKET, SO_BINDTODEVICE, dev,
157 				strlen(dev) + 1);
158 
159 	switch (proto_type) {
160 	case TYPE_TCP:
161 		if (client_mode && fastopen_sapi) {
162 			SAFE_SETSOCKOPT_INT(sd, IPPROTO_TCP,
163 					    TCP_FASTOPEN_CONNECT, 1);
164 		}
165 		if (client_mode && zcopy)
166 			SAFE_SETSOCKOPT_INT(sd, SOL_SOCKET, SO_ZEROCOPY, 1);
167 	break;
168 	case TYPE_DCCP:
169 		SAFE_SETSOCKOPT_INT(sd, SOL_DCCP, DCCP_SOCKOPT_SERVICE,
170 				    service_code);
171 	break;
172 	case TYPE_UDP_LITE: {
173 		int cscov = init_srv_msg_len >> 1;
174 
175 		if (cscov < 8)
176 			cscov = 8;
177 		tst_res(TINFO, "UDP-Lite send cscov is %d", cscov);
178 		/* set checksum for header and partially for payload */
179 		SAFE_SETSOCKOPT_INT(sd, SOL_UDPLITE, UDPLITE_SEND_CSCOV, cscov);
180 		SAFE_SETSOCKOPT_INT(sd, SOL_UDPLITE, UDPLITE_RECV_CSCOV, 8);
181 	} break;
182 	}
183 }
184 
do_cleanup(void)185 static void do_cleanup(void)
186 {
187 	if (net.cleanup)
188 		net.cleanup();
189 
190 	if (tfo_cfg_changed) {
191 		tst_res(TINFO, "unset '%s' back to '%d'",
192 			tfo_cfg, tfo_cfg_value);
193 		SAFE_FILE_PRINTF(tfo_cfg, "%d", tfo_cfg_value);
194 	}
195 
196 	if (tw_reuse_changed) {
197 		SAFE_FILE_PRINTF(tcp_tw_reuse, "0");
198 		tst_res(TINFO, "unset '%s' back to '0'", tcp_tw_reuse);
199 	}
200 }
TST_DECLARE_ONCE_FN(cleanup,do_cleanup)201 TST_DECLARE_ONCE_FN(cleanup, do_cleanup)
202 
203 static int sock_recv_poll(char *buf, int size, struct sock_info *i)
204 {
205 	struct pollfd pfd;
206 	pfd.fd = i->fd;
207 	pfd.events = POLLIN;
208 	int len = -1;
209 
210 	while (1) {
211 		errno = 0;
212 		int ret = poll(&pfd, 1, i->timeout);
213 		if (ret == -1) {
214 			if (errno == EINTR)
215 				continue;
216 			break;
217 		}
218 
219 		if (ret != 1) {
220 			if (!errno)
221 				errno = ETIME;
222 			break;
223 		}
224 
225 		if (!(pfd.revents & POLLIN)) {
226 			if (pfd.revents & POLLERR) {
227 				int err = 0;
228 				socklen_t err_len = sizeof(err);
229 
230 				getsockopt(i->fd, SOL_SOCKET, SO_ERROR,
231 					   &err, &err_len);
232 				if (!err)
233 					continue;
234 				errno = err;
235 			}
236 			break;
237 		}
238 
239 		errno = 0;
240 		len = recvfrom(i->fd, buf, size, MSG_DONTWAIT,
241 			       (struct sockaddr *)&i->raddr,
242 			       &i->raddr_len);
243 
244 		if (len == -1 && errno == EINTR)
245 			continue;
246 
247 		if (len == 0)
248 			errno = ESHUTDOWN;
249 
250 		break;
251 	}
252 
253 	return len;
254 }
255 
client_recv(char * buf,int srv_msg_len,struct sock_info * i)256 static int client_recv(char *buf, int srv_msg_len, struct sock_info *i)
257 {
258 	int len, offset = 0;
259 
260 	while (1) {
261 		errno = 0;
262 		len = sock_recv_poll(buf + offset, srv_msg_len - offset, i);
263 
264 		/* socket closed or msg is not valid */
265 		if (len < 1 || (offset + len) > srv_msg_len ||
266 		   (buf[0] != start_byte && buf[0] != start_fin_byte)) {
267 			/* packet too big message, resend with new pmtu */
268 			if (errno == EMSGSIZE) {
269 				if (++(i->pmtu_err_cnt) < max_pmtu_err)
270 					return 0;
271 				tst_brk(TFAIL, "too many pmtu errors %d",
272 					i->pmtu_err_cnt);
273 			} else if (!errno) {
274 				errno = ENOMSG;
275 			}
276 			break;
277 		}
278 		offset += len;
279 		if (buf[offset - 1] != end_byte)
280 			continue;
281 
282 		/* recv last msg, close socket */
283 		if (buf[0] == start_fin_byte)
284 			break;
285 		return 0;
286 	}
287 
288 	if (sock_type != SOCK_STREAM) {
289 		if (errno == ETIME) {
290 			if (++(i->etime_cnt) > max_etime_cnt)
291 				tst_brk(TFAIL, "client requests timeout %d times, last timeout %dms",
292 					i->etime_cnt, i->timeout);
293 			/* Increase timeout in poll up to 3.2 sec */
294 			if (i->timeout < 3000)
295 				i->timeout <<= 1;
296 			return 0;
297 		}
298 		if (errno == ESHUTDOWN) {
299 			if (++(i->eshutdown_cnt) > max_eshutdown_cnt)
300 				tst_brk(TFAIL, "too many zero-length msgs");
301 			tst_res(TINFO, "%d-length msg on sock %d", len, i->fd);
302 			return 0;
303 		}
304 	}
305 
306 	SAFE_CLOSE(i->fd);
307 	return (errno) ? -1 : 0;
308 }
309 
310 static int bind_no_port;
bind_before_connect(int sd)311 static void bind_before_connect(int sd)
312 {
313 	if (!local_addrinfo)
314 		return;
315 
316 	if (bind_no_port)
317 		SAFE_SETSOCKOPT_INT(sd, SOL_IP, IP_BIND_ADDRESS_NO_PORT, 1);
318 	if (reuse_port)
319 		SAFE_SETSOCKOPT_INT(sd, SOL_SOCKET, SO_REUSEPORT, 1);
320 
321 	SAFE_BIND(sd, local_addrinfo->ai_addr, local_addrinfo->ai_addrlen);
322 
323 	if (bind_no_port && proto_type != TYPE_SCTP) {
324 		int port = TST_GETSOCKPORT(sd);
325 
326 		if (port)
327 			tst_brk(TFAIL, "port not zero after bind(): %d", port);
328 	}
329 }
330 
client_connect_send(const char * msg,int size)331 static int client_connect_send(const char *msg, int size)
332 {
333 	int cfd = SAFE_SOCKET(family, sock_type, protocol);
334 
335 	init_socket_opts(cfd);
336 
337 	if (fastopen_api) {
338 		/* Replaces connect() + send()/write() */
339 		SAFE_SENDTO(1, cfd, msg, size, send_flags | MSG_FASTOPEN,
340 			remote_addrinfo->ai_addr, remote_addrinfo->ai_addrlen);
341 	} else {
342 		bind_before_connect(cfd);
343 		/* old TCP API */
344 		SAFE_CONNECT(cfd, remote_addrinfo->ai_addr,
345 			     remote_addrinfo->ai_addrlen);
346 		SAFE_SEND(1, cfd, msg, size, send_flags);
347 	}
348 	return cfd;
349 }
350 
351 union net_size_field {
352 	char bytes[2];
353 	uint16_t value;
354 };
355 
make_client_request(char client_msg[],int * cln_len,int * srv_len,unsigned int * seed)356 static void make_client_request(char client_msg[], int *cln_len, int *srv_len,
357 				unsigned int *seed)
358 {
359 	if (max_rand_msg_len)
360 		*cln_len = *srv_len = min_msg_len + rand_r(seed) % max_rand_msg_len;
361 
362 	memset(client_msg, client_byte, *cln_len);
363 	client_msg[0] = start_byte;
364 
365 	/* set size for reply */
366 	union net_size_field net_size;
367 
368 	net_size.value = htons(*srv_len);
369 	client_msg[1] = net_size.bytes[0];
370 	client_msg[2] = net_size.bytes[1];
371 
372 	client_msg[*cln_len - 1] = end_byte;
373 }
374 
client_fn(void * id)375 void *client_fn(void *id)
376 {
377 	int cln_len = init_cln_msg_len,
378 	    srv_len = init_srv_msg_len;
379 	struct sock_info inf;
380 	char buf[max_msg_len];
381 	char client_msg[max_msg_len];
382 	int i = 0;
383 	intptr_t err = 0;
384 	unsigned int seed = init_seed ^ (intptr_t)id;
385 
386 	inf.raddr_len = sizeof(inf.raddr);
387 	inf.etime_cnt = 0;
388 	inf.eshutdown_cnt = 0;
389 	inf.timeout = wait_timeout;
390 	inf.pmtu_err_cnt = 0;
391 
392 	make_client_request(client_msg, &cln_len, &srv_len, &seed);
393 
394 	/* connect & send requests */
395 	inf.fd = client_connect_send(client_msg, cln_len);
396 	if (inf.fd == -1) {
397 		err = errno;
398 		goto out;
399 	}
400 
401 	if (client_recv(buf, srv_len, &inf)) {
402 		err = errno;
403 		goto out;
404 	}
405 
406 	for (i = 1; i < client_max_requests; ++i) {
407 		if (inf.fd == -1) {
408 			inf.fd = client_connect_send(client_msg, cln_len);
409 			if (inf.fd == -1) {
410 				err = errno;
411 				goto out;
412 			}
413 
414 			if (client_recv(buf, srv_len, &inf)) {
415 				err = errno;
416 				break;
417 			}
418 			continue;
419 		}
420 
421 		if (max_rand_msg_len)
422 			make_client_request(client_msg, &cln_len, &srv_len, &seed);
423 
424 		SAFE_SEND(1, inf.fd, client_msg, cln_len, send_flags);
425 
426 		if (client_recv(buf, srv_len, &inf)) {
427 			err = errno;
428 			break;
429 		}
430 	}
431 
432 	if (inf.fd != -1)
433 		SAFE_CLOSE(inf.fd);
434 
435 out:
436 	if (i != client_max_requests)
437 		tst_res(TWARN, "client exit on '%d' request", i);
438 
439 	return (void *) err;
440 }
441 
parse_client_request(const char * msg)442 static int parse_client_request(const char *msg)
443 {
444 	union net_size_field net_size;
445 	net_size.bytes[0] = msg[1];
446 	net_size.bytes[1] = msg[2];
447 	int size = ntohs(net_size.value);
448 	if (size < 2 || size > max_msg_len)
449 		return -1;
450 
451 	return size;
452 }
453 
454 static struct timespec tv_client_start;
455 static struct timespec tv_client_end;
456 
client_init(void)457 static void client_init(void)
458 {
459 	if (clients_num >= MAX_THREADS) {
460 		tst_brk(TBROK, "Unexpected num of clients '%d'",
461 			clients_num);
462 	}
463 
464 	thread_ids = SAFE_MALLOC(sizeof(pthread_t) * clients_num);
465 
466 	struct addrinfo hints;
467 	memset(&hints, 0, sizeof(struct addrinfo));
468 	hints.ai_family = AF_UNSPEC;
469 	hints.ai_socktype = sock_type;
470 	hints.ai_flags = 0;
471 	hints.ai_protocol = 0;
472 
473 	if (source_addr)
474 		SAFE_GETADDRINFO(source_addr, NULL, &hints, &local_addrinfo);
475 	SAFE_GETADDRINFO(server_addr, tcp_port, &hints, &remote_addrinfo);
476 
477 	tst_res(TINFO, "Running the test over IPv%s",
478 		(remote_addrinfo->ai_family == AF_INET6) ? "6" : "4");
479 
480 	family = remote_addrinfo->ai_family;
481 
482 	clock_gettime(CLOCK_MONOTONIC_RAW, &tv_client_start);
483 	intptr_t i;
484 	for (i = 0; i < clients_num; ++i)
485 		SAFE_PTHREAD_CREATE(&thread_ids[i], &attr, client_fn, (void *)i);
486 }
487 
client_run(void)488 static void client_run(void)
489 {
490 	void *res = NULL;
491 	long clnt_time = 0;
492 	int i;
493 	for (i = 0; i < clients_num; ++i) {
494 		pthread_join(thread_ids[i], &res);
495 		if (res) {
496 			tst_brk(TBROK, "client[%d] failed: %s",
497 				i, strerror((intptr_t)res));
498 		}
499 	}
500 
501 	clock_gettime(CLOCK_MONOTONIC_RAW, &tv_client_end);
502 	clnt_time = (tv_client_end.tv_sec - tv_client_start.tv_sec) * 1000 +
503 		(tv_client_end.tv_nsec - tv_client_start.tv_nsec) / 1000000;
504 
505 	tst_res(TINFO, "total time '%ld' ms", clnt_time);
506 
507 	char client_msg[min_msg_len];
508 	int msg_len = min_msg_len;
509 
510 	max_rand_msg_len = 0;
511 	make_client_request(client_msg, &msg_len, &msg_len, NULL);
512 	/* ask server to terminate */
513 	client_msg[0] = start_fin_byte;
514 	int cfd = client_connect_send(client_msg, msg_len);
515 	if (cfd != -1) {
516 		shutdown(cfd, SHUT_WR);
517 		SAFE_CLOSE(cfd);
518 	}
519 	/* the script tcp_fastopen_run.sh will remove it */
520 	if (rpath)
521 		SAFE_FILE_PRINTF(rpath, "%ld", clnt_time);
522 
523 	tst_res(TPASS, "test completed");
524 }
525 
client_cleanup(void)526 static void client_cleanup(void)
527 {
528 	free(thread_ids);
529 
530 	if (remote_addrinfo)
531 		freeaddrinfo(remote_addrinfo);
532 }
533 
make_server_reply(char * send_msg,int size)534 static void make_server_reply(char *send_msg, int size)
535 {
536 	memset(send_msg, server_byte, size - 1);
537 	send_msg[0] = start_byte;
538 	send_msg[size - 1] = end_byte;
539 }
540 
server_fn(void * cfd)541 void *server_fn(void *cfd)
542 {
543 	int num_requests = 0, offset = 0;
544 	char send_msg[max_msg_len], end[] = { end_byte };
545 	int start_send_type = (sock_type == SOCK_DGRAM) ? 1 : 0;
546 	int send_msg_len, send_type = start_send_type;
547 	char recv_msg[max_msg_len];
548 	struct sock_info inf;
549 	ssize_t recv_len;
550 	struct iovec iov[2];
551 	struct msghdr msg;
552 
553 	inf.fd = (intptr_t) cfd;
554 	inf.raddr_len = sizeof(inf.raddr);
555 	inf.timeout = wait_timeout;
556 
557 	iov[0].iov_base = send_msg;
558 	iov[1].iov_base = end;
559 	iov[1].iov_len = 1;
560 	memset(&msg, 0, sizeof(msg));
561 	msg.msg_name = &inf.raddr;
562 	msg.msg_iov = iov;
563 	msg.msg_iovlen = 2;
564 
565 	init_socket_opts(inf.fd);
566 
567 	while (1) {
568 		recv_len = sock_recv_poll(recv_msg + offset,
569 					  max_msg_len - offset, &inf);
570 
571 		if (recv_len == 0)
572 			break;
573 
574 		if (recv_len < 0 || (offset + recv_len) > max_msg_len ||
575 		   (recv_msg[0] != start_byte &&
576 		    recv_msg[0] != start_fin_byte)) {
577 			tst_res(TFAIL, "recv failed, sock '%d'", inf.fd);
578 			goto out;
579 		}
580 
581 		offset += recv_len;
582 
583 		if (recv_msg[offset - 1] != end_byte) {
584 			/* msg is not complete, continue recv */
585 			continue;
586 		}
587 
588 		/* client asks to terminate */
589 		if (recv_msg[0] == start_fin_byte)
590 			goto out;
591 
592 		send_msg_len = parse_client_request(recv_msg);
593 		if (send_msg_len < 0) {
594 			tst_res(TFAIL, "wrong msg size '%d'",
595 				send_msg_len);
596 			goto out;
597 		}
598 		make_server_reply(send_msg, send_msg_len);
599 
600 		offset = 0;
601 
602 		/*
603 		 * It will tell client that server is going
604 		 * to close this connection.
605 		 */
606 		if (sock_type == SOCK_STREAM &&
607 		    ++num_requests >= server_max_requests)
608 			send_msg[0] = start_fin_byte;
609 
610 		switch (send_type) {
611 		case 0:
612 			SAFE_SEND(1, inf.fd, send_msg, send_msg_len,
613 				  send_flags);
614 			if (proto_type != TYPE_SCTP)
615 				++send_type;
616 			break;
617 		case 1:
618 			SAFE_SENDTO(1, inf.fd, send_msg, send_msg_len,
619 				    send_flags, (struct sockaddr *)&inf.raddr,
620 				    inf.raddr_len);
621 			++send_type;
622 			break;
623 		default:
624 			iov[0].iov_len = send_msg_len - 1;
625 			msg.msg_namelen = inf.raddr_len;
626 			SAFE_SENDMSG(send_msg_len, inf.fd, &msg, send_flags);
627 			send_type = start_send_type;
628 			break;
629 		}
630 
631 		if (sock_type == SOCK_STREAM &&
632 		    num_requests >= server_max_requests) {
633 			/* max reqs, close socket */
634 			shutdown(inf.fd, SHUT_WR);
635 			break;
636 		}
637 	}
638 
639 	SAFE_CLOSE(inf.fd);
640 	return NULL;
641 
642 out:
643 	SAFE_CLOSE(inf.fd);
644 	tst_brk(TBROK, "Server closed");
645 	return NULL;
646 }
647 
server_thread_add(intptr_t client_fd)648 static pthread_t server_thread_add(intptr_t client_fd)
649 {
650 	pthread_t id;
651 	SAFE_PTHREAD_CREATE(&id, &attr, server_fn, (void *) client_fd);
652 	return id;
653 }
654 
server_init(void)655 static void server_init(void)
656 {
657 	char *src_addr = NULL;
658 	struct addrinfo hints;
659 
660 	memset(&hints, 0, sizeof(struct addrinfo));
661 	hints.ai_family = AF_INET6;
662 	hints.ai_socktype = sock_type;
663 	hints.ai_flags = AI_PASSIVE;
664 
665 	if (!source_addr && !tcp_port)
666 		tcp_port = "0";
667 
668 	if (source_addr && !strchr(source_addr, ':'))
669 		SAFE_ASPRINTF(&src_addr, "::ffff:%s", source_addr);
670 	SAFE_GETADDRINFO(src_addr ? src_addr : source_addr, tcp_port,
671 		       &hints, &local_addrinfo);
672 	free(src_addr);
673 
674 	/* IPv6 socket is also able to access IPv4 protocol stack */
675 	sfd = SAFE_SOCKET(family, sock_type, protocol);
676 	SAFE_SETSOCKOPT_INT(sfd, SOL_SOCKET, SO_REUSEADDR, 1);
677 	if (reuse_port)
678 		SAFE_SETSOCKOPT_INT(sfd, SOL_SOCKET, SO_REUSEPORT, 1);
679 
680 	tst_res(TINFO, "assigning a name to the server socket...");
681 	SAFE_BIND(sfd, local_addrinfo->ai_addr, local_addrinfo->ai_addrlen);
682 
683 	freeaddrinfo(local_addrinfo);
684 
685 	int port = TST_GETSOCKPORT(sfd);
686 
687 	tst_res(TINFO, "bind to port %d", port);
688 	if (server_bg) {
689 		SAFE_CHDIR(server_bg);
690 		SAFE_FILE_PRINTF(port_path, "%d", port);
691 	}
692 
693 	if (sock_type == SOCK_DGRAM)
694 		return;
695 
696 	init_socket_opts(sfd);
697 
698 	if (fastopen_api || fastopen_sapi) {
699 		SAFE_SETSOCKOPT_INT(sfd, IPPROTO_TCP, TCP_FASTOPEN,
700 			tfo_queue_size);
701 	}
702 
703 	if (zcopy)
704 		SAFE_SETSOCKOPT_INT(sfd, SOL_SOCKET, SO_ZEROCOPY, 1);
705 
706 	SAFE_LISTEN(sfd, max_queue_len);
707 
708 	tst_res(TINFO, "Listen on the socket '%d'", sfd);
709 }
710 
server_cleanup(void)711 static void server_cleanup(void)
712 {
713 	SAFE_CLOSE(sfd);
714 }
715 
move_to_background(void)716 static void move_to_background(void)
717 {
718 	if (SAFE_FORK()) {
719 		TST_CHECKPOINT_WAIT(0);
720 		exit(0);
721 	}
722 
723 	SAFE_SETSID();
724 
725 	TST_CHECKPOINT_WAKE(0);
726 
727 	close(STDIN_FILENO);
728 	SAFE_OPEN("/dev/null", O_RDONLY);
729 	close(STDOUT_FILENO);
730 	close(STDERR_FILENO);
731 
732 	int fd = SAFE_OPEN(log_path, O_CREAT | O_TRUNC | O_WRONLY, 0644);
733 
734 	SAFE_DUP(fd);
735 }
736 
server_run_udp(void)737 static void server_run_udp(void)
738 {
739 	if (server_bg)
740 		move_to_background();
741 
742 	pthread_t p_id = server_thread_add(sfd);
743 
744 	SAFE_PTHREAD_JOIN(p_id, NULL);
745 }
746 
server_run(void)747 static void server_run(void)
748 {
749 	if (server_bg)
750 		move_to_background();
751 
752 	/* IPv4 source address will be mapped to IPv6 address */
753 	struct sockaddr_in6 addr6;
754 	socklen_t addr_size = sizeof(addr6);
755 
756 	/*
757 	 * detaching threads allow to reclaim thread's resources
758 	 * once a thread finishes its work.
759 	 */
760 	if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0)
761 		tst_brk(TBROK | TERRNO, "setdetachstate failed");
762 
763 	while (1) {
764 		int client_fd = accept(sfd, (struct sockaddr *)&addr6,
765 			&addr_size);
766 
767 		if (client_fd == -1)
768 			tst_brk(TBROK, "Can't create client socket");
769 
770 		server_thread_add(client_fd);
771 	}
772 }
773 
require_root(const char * file)774 static void require_root(const char *file)
775 {
776 	if (!geteuid())
777 		return;
778 	tst_brk(TCONF, "Test needs to be run as root to change %s", file);
779 }
780 
check_tfo_value(void)781 static void check_tfo_value(void)
782 {
783 	/* Check if we can write to tcp_fastopen knob. We might be
784 	 * inside netns and either have read-only permission or
785 	 * doesn't have the knob at all.
786 	 */
787 	if (access(tfo_cfg, W_OK) < 0) {
788 		/* TODO check /proc/self/ns/ or TST_USE_NETNS env var */
789 		tst_res(TINFO, "can't read %s, assume server runs in netns",
790 			tfo_cfg);
791 		return;
792 	}
793 
794 	SAFE_FILE_SCANF(tfo_cfg, "%d", &tfo_cfg_value);
795 	tst_res(TINFO, "'%s' is %d", tfo_cfg, tfo_cfg_value);
796 
797 	/* The check can be the first in this function but set here
798 	 * to allow to print information about the currently set config
799 	 */
800 	if (tfo_value < 0)
801 		return;
802 
803 	if (tfo_cfg_value == tfo_value)
804 		return;
805 
806 	require_root(tfo_cfg);
807 
808 	tst_res(TINFO, "set '%s' to '%d'", tfo_cfg, tfo_value);
809 
810 	SAFE_FILE_PRINTF(tfo_cfg, "%d", tfo_value);
811 	tfo_cfg_changed = 1;
812 }
813 
check_tw_reuse(void)814 static void check_tw_reuse(void)
815 {
816 	if (access(tcp_tw_reuse, W_OK) < 0)
817 		return;
818 
819 	int reuse_value = 0;
820 
821 	SAFE_FILE_SCANF(tcp_tw_reuse, "%d", &reuse_value);
822 	if (reuse_value) {
823 		tst_res(TINFO, "tcp_tw_reuse is already set");
824 		return;
825 	}
826 
827 	require_root(tfo_cfg);
828 
829 	SAFE_FILE_PRINTF(tcp_tw_reuse, "1");
830 	tw_reuse_changed = 1;
831 	tst_res(TINFO, "set '%s' to '1'", tcp_tw_reuse);
832 }
833 
set_protocol_type(void)834 static void set_protocol_type(void)
835 {
836 	if (!type || !strcmp(type, "tcp"))
837 		proto_type = TYPE_TCP;
838 	else if (!strcmp(type, "udp"))
839 		proto_type = TYPE_UDP;
840 	else if (!strcmp(type, "udp_lite"))
841 		proto_type = TYPE_UDP_LITE;
842 	else if (!strcmp(type, "dccp"))
843 		proto_type = TYPE_DCCP;
844 	else if (!strcmp(type, "sctp"))
845 		proto_type = TYPE_SCTP;
846 	else
847 		tst_brk(TBROK, "Invalid proto_type: '%s'", type);
848 }
849 
setup(void)850 static void setup(void)
851 {
852 	if (tst_parse_int(aarg, &clients_num, 1, INT_MAX))
853 		tst_brk(TBROK, "Invalid client number '%s'", aarg);
854 	if (tst_parse_int(rarg, &client_max_requests, 1, INT_MAX))
855 		tst_brk(TBROK, "Invalid client max requests '%s'", rarg);
856 	if (tst_parse_int(Rarg, &server_max_requests, 1, INT_MAX))
857 		tst_brk(TBROK, "Invalid server max requests '%s'", Rarg);
858 	if (tst_parse_int(narg, &init_cln_msg_len, min_msg_len, max_msg_len))
859 		tst_brk(TBROK, "Invalid client msg size '%s'", narg);
860 	if (tst_parse_int(Narg, &init_srv_msg_len, min_msg_len, max_msg_len))
861 		tst_brk(TBROK, "Invalid server msg size '%s'", Narg);
862 	if (tst_parse_int(qarg, &tfo_queue_size, 1, INT_MAX))
863 		tst_brk(TBROK, "Invalid TFO queue size '%s'", qarg);
864 	if (tst_parse_int(Targ, &wait_timeout, 0, INT_MAX))
865 		tst_brk(TBROK, "Invalid wait timeout '%s'", Targ);
866 	if (tst_parse_int(barg, &busy_poll, 0, INT_MAX))
867 		tst_brk(TBROK, "Invalid busy poll timeout'%s'", barg);
868 	if (tst_parse_int(targ, &tfo_value, 0, INT_MAX))
869 		tst_brk(TBROK, "Invalid net.ipv4.tcp_fastopen '%s'", targ);
870 	if (tst_parse_int(Aarg, &max_rand_msg_len, 10, max_msg_len))
871 		tst_brk(TBROK, "Invalid max random payload size '%s'", Aarg);
872 
873 	if (!server_addr)
874 		server_addr = "localhost";
875 
876 	if (max_rand_msg_len) {
877 		max_rand_msg_len -= min_msg_len;
878 		init_seed = max_rand_msg_len ^ client_max_requests;
879 		srand(init_seed); /* in case rand_r() is missing */
880 		tst_res(TINFO, "rand start seed 0x%x", init_seed);
881 	}
882 
883 	/* if client_num is not set, use num of processors */
884 	if (!clients_num)
885 		clients_num = sysconf(_SC_NPROCESSORS_ONLN);
886 
887 	if (busy_poll >= 0 && tst_kvercmp(3, 11, 0) < 0)
888 		tst_brk(TCONF, "Test must be run with kernel 3.11 or newer");
889 
890 	set_protocol_type();
891 
892 	if (client_mode) {
893 		if (source_addr && tst_kvercmp(4, 2, 0) >= 0) {
894 			bind_no_port = 1;
895 			tst_res(TINFO, "IP_BIND_ADDRESS_NO_PORT is used");
896 		}
897 		tst_res(TINFO, "connection: addr '%s', port '%s'",
898 			server_addr, tcp_port);
899 		tst_res(TINFO, "client max req: %d", client_max_requests);
900 		tst_res(TINFO, "clients num: %d", clients_num);
901 		if (max_rand_msg_len) {
902 			tst_res(TINFO, "random msg size [%d %d]",
903 				min_msg_len, max_rand_msg_len);
904 		} else {
905 			tst_res(TINFO, "client msg size: %d", init_cln_msg_len);
906 			tst_res(TINFO, "server msg size: %d", init_srv_msg_len);
907 		}
908 		net.init	= client_init;
909 		net.run		= client_run;
910 		net.cleanup	= client_cleanup;
911 
912 		switch (proto_type) {
913 		case TYPE_TCP:
914 			check_tw_reuse();
915 			break;
916 		case TYPE_DCCP:
917 		case TYPE_UDP:
918 		case TYPE_UDP_LITE:
919 			if (max_etime_cnt >= client_max_requests)
920 				max_etime_cnt = client_max_requests - 1;
921 			tst_res(TINFO, "maximum allowed timeout errors %d", max_etime_cnt);
922 			wait_timeout = 100;
923 		}
924 	} else {
925 		tst_res(TINFO, "max requests '%d'",
926 			server_max_requests);
927 		net.init	= server_init;
928 		switch (proto_type) {
929 		case TYPE_TCP:
930 		case TYPE_DCCP:
931 		case TYPE_SCTP:
932 			net.run		= server_run;
933 			net.cleanup	= server_cleanup;
934 		break;
935 		case TYPE_UDP:
936 		case TYPE_UDP_LITE:
937 			net.run		= server_run_udp;
938 			net.cleanup	= NULL;
939 		break;
940 		}
941 	}
942 
943 	if (zcopy)
944 		send_flags |= MSG_ZEROCOPY;
945 
946 	switch (proto_type) {
947 	case TYPE_TCP:
948 		tst_res(TINFO, "TCP %s is using %s TCP API.",
949 			(client_mode) ? "client" : "server",
950 			(fastopen_api) ? "Fastopen" : "old");
951 		check_tfo_value();
952 	break;
953 	case TYPE_UDP:
954 		tst_res(TINFO, "using UDP");
955 		fastopen_api = fastopen_sapi = NULL;
956 		sock_type = SOCK_DGRAM;
957 	break;
958 	case TYPE_UDP_LITE:
959 		tst_res(TINFO, "using UDP Lite");
960 		fastopen_api = fastopen_sapi = NULL;
961 		sock_type = SOCK_DGRAM;
962 		protocol = IPPROTO_UDPLITE;
963 	break;
964 	case TYPE_DCCP: {
965 		/* dccp* modules can be blacklisted, load them manually */
966 		const char * const argv[] = {"modprobe", "dccp_ipv6", NULL};
967 
968 		if (tst_cmd(argv, NULL, NULL, TST_CMD_PASS_RETVAL))
969 			tst_brk(TCONF, "Failed to load dccp_ipv6 module");
970 
971 		tst_res(TINFO, "DCCP %s", (client_mode) ? "client" : "server");
972 		fastopen_api = fastopen_sapi = NULL;
973 		sock_type = SOCK_DCCP;
974 		protocol = IPPROTO_DCCP;
975 		service_code = htonl(service_code);
976 	} break;
977 	case TYPE_SCTP:
978 		tst_res(TINFO, "SCTP %s", (client_mode) ? "client" : "server");
979 		fastopen_api = fastopen_sapi = NULL;
980 		protocol = IPPROTO_SCTP;
981 	break;
982 	}
983 
984 	if ((errno = pthread_attr_init(&attr)))
985 		tst_brk(TBROK | TERRNO, "pthread_attr_init failed");
986 
987 	if ((errno = pthread_attr_setstacksize(&attr, 256*1024)))
988 		tst_brk(TBROK | TERRNO, "pthread_attr_setstacksize(256*1024) failed");
989 
990 	net.init();
991 }
992 
do_test(void)993 static void do_test(void)
994 {
995 	net.run();
996 }
997 
998 static struct tst_test test = {
999 	.test_all = do_test,
1000 	.forks_child = 1,
1001 	.setup = setup,
1002 	.cleanup = cleanup,
1003 	.options = (struct tst_option[]) {
1004 		{"f", &fastopen_api, "Use TFO API, default is old API"},
1005 		{"F", &fastopen_sapi, "TCP_FASTOPEN_CONNECT socket option and standard API"},
1006 		{"t:", &targ, "Set tcp_fastopen value"},
1007 		{"S:", &source_addr, "Source address to bind"},
1008 		{"g:", &tcp_port, "Server port"},
1009 		{"b:", &barg, "Low latency busy poll timeout"},
1010 		{"T:", &type, "Tcp (default), udp, udp_lite, dccp, sctp"},
1011 		{"z", &zcopy, "Enable SO_ZEROCOPY"},
1012 		{"P:", &reuse_port, "Enable SO_REUSEPORT"},
1013 		{"d:", &dev, "Bind to device x"},
1014 
1015 		{"H:", &server_addr, "Server name or IP address"},
1016 		{"l", &client_mode, "Become client, default is server"},
1017 		{"a:", &aarg, "Number of clients running in parallel"},
1018 		{"r:", &rarg, "Number of client requests"},
1019 		{"n:", &narg, "Client message size"},
1020 		{"N:", &Narg, "Server message size"},
1021 		{"m:", &Targ, "Receive timeout in milliseconds (not used by UDP/DCCP client)"},
1022 		{"c:", &rpath, "Path to file where result is saved"},
1023 		{"A:", &Aarg, "Max payload length (generated randomly)"},
1024 
1025 		{"R:", &Rarg, "Server requests after which conn.closed"},
1026 		{"q:", &qarg, "TFO queue"},
1027 		{"B:", &server_bg, "Run in background, arg is the process directory"},
1028 		{}
1029 	},
1030 	.max_runtime = 300,
1031 	.needs_checkpoints = 1,
1032 };
1033