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