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