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.timeout = wait_timeout;
388 inf.pmtu_err_cnt = 0;
389
390 make_client_request(client_msg, &cln_len, &srv_len, &seed);
391
392 /* connect & send requests */
393 inf.fd = client_connect_send(client_msg, cln_len);
394 if (inf.fd == -1) {
395 err = errno;
396 goto out;
397 }
398
399 if (client_recv(buf, srv_len, &inf)) {
400 err = errno;
401 goto out;
402 }
403
404 for (i = 1; i < client_max_requests; ++i) {
405 if (inf.fd == -1) {
406 inf.fd = client_connect_send(client_msg, cln_len);
407 if (inf.fd == -1) {
408 err = errno;
409 goto out;
410 }
411
412 if (client_recv(buf, srv_len, &inf)) {
413 err = errno;
414 break;
415 }
416 continue;
417 }
418
419 if (max_rand_msg_len)
420 make_client_request(client_msg, &cln_len, &srv_len, &seed);
421
422 SAFE_SEND(1, inf.fd, client_msg, cln_len, send_flags);
423
424 if (client_recv(buf, srv_len, &inf)) {
425 err = errno;
426 break;
427 }
428 }
429
430 if (inf.fd != -1)
431 SAFE_CLOSE(inf.fd);
432
433 out:
434 if (i != client_max_requests)
435 tst_res(TWARN, "client exit on '%d' request", i);
436
437 return (void *) err;
438 }
439
parse_client_request(const char * msg)440 static int parse_client_request(const char *msg)
441 {
442 union net_size_field net_size;
443 net_size.bytes[0] = msg[1];
444 net_size.bytes[1] = msg[2];
445 int size = ntohs(net_size.value);
446 if (size < 2 || size > max_msg_len)
447 return -1;
448
449 return size;
450 }
451
452 static struct timespec tv_client_start;
453 static struct timespec tv_client_end;
454
client_init(void)455 static void client_init(void)
456 {
457 if (clients_num >= MAX_THREADS) {
458 tst_brk(TBROK, "Unexpected num of clients '%d'",
459 clients_num);
460 }
461
462 thread_ids = SAFE_MALLOC(sizeof(pthread_t) * clients_num);
463
464 struct addrinfo hints;
465 memset(&hints, 0, sizeof(struct addrinfo));
466 hints.ai_family = AF_UNSPEC;
467 hints.ai_socktype = sock_type;
468 hints.ai_flags = 0;
469 hints.ai_protocol = 0;
470
471 if (source_addr)
472 SAFE_GETADDRINFO(source_addr, NULL, &hints, &local_addrinfo);
473 SAFE_GETADDRINFO(server_addr, tcp_port, &hints, &remote_addrinfo);
474
475 tst_res(TINFO, "Running the test over IPv%s",
476 (remote_addrinfo->ai_family == AF_INET6) ? "6" : "4");
477
478 family = remote_addrinfo->ai_family;
479
480 clock_gettime(CLOCK_MONOTONIC_RAW, &tv_client_start);
481 intptr_t i;
482 for (i = 0; i < clients_num; ++i)
483 SAFE_PTHREAD_CREATE(&thread_ids[i], &attr, client_fn, (void *)i);
484 }
485
client_run(void)486 static void client_run(void)
487 {
488 void *res = NULL;
489 long clnt_time = 0;
490 int i;
491 for (i = 0; i < clients_num; ++i) {
492 pthread_join(thread_ids[i], &res);
493 if (res) {
494 tst_brk(TBROK, "client[%d] failed: %s",
495 i, strerror((intptr_t)res));
496 }
497 }
498
499 clock_gettime(CLOCK_MONOTONIC_RAW, &tv_client_end);
500 clnt_time = (tv_client_end.tv_sec - tv_client_start.tv_sec) * 1000 +
501 (tv_client_end.tv_nsec - tv_client_start.tv_nsec) / 1000000;
502
503 tst_res(TINFO, "total time '%ld' ms", clnt_time);
504
505 char client_msg[min_msg_len];
506 int msg_len = min_msg_len;
507
508 max_rand_msg_len = 0;
509 make_client_request(client_msg, &msg_len, &msg_len, NULL);
510 /* ask server to terminate */
511 client_msg[0] = start_fin_byte;
512 int cfd = client_connect_send(client_msg, msg_len);
513 if (cfd != -1) {
514 shutdown(cfd, SHUT_WR);
515 SAFE_CLOSE(cfd);
516 }
517 /* the script tcp_fastopen_run.sh will remove it */
518 if (rpath)
519 SAFE_FILE_PRINTF(rpath, "%ld", clnt_time);
520
521 tst_res(TPASS, "test completed");
522 }
523
client_cleanup(void)524 static void client_cleanup(void)
525 {
526 free(thread_ids);
527
528 if (remote_addrinfo)
529 freeaddrinfo(remote_addrinfo);
530 }
531
make_server_reply(char * send_msg,int size)532 static void make_server_reply(char *send_msg, int size)
533 {
534 memset(send_msg, server_byte, size - 1);
535 send_msg[0] = start_byte;
536 send_msg[size - 1] = end_byte;
537 }
538
server_fn(void * cfd)539 void *server_fn(void *cfd)
540 {
541 int num_requests = 0, offset = 0;
542 char send_msg[max_msg_len], end[] = { end_byte };
543 int start_send_type = (sock_type == SOCK_DGRAM) ? 1 : 0;
544 int send_msg_len, send_type = start_send_type;
545 char recv_msg[max_msg_len];
546 struct sock_info inf;
547 ssize_t recv_len;
548 struct iovec iov[2];
549 struct msghdr msg;
550
551 inf.fd = (intptr_t) cfd;
552 inf.raddr_len = sizeof(inf.raddr);
553 inf.timeout = wait_timeout;
554
555 iov[0].iov_base = send_msg;
556 iov[1].iov_base = end;
557 iov[1].iov_len = 1;
558 memset(&msg, 0, sizeof(msg));
559 msg.msg_name = &inf.raddr;
560 msg.msg_iov = iov;
561 msg.msg_iovlen = 2;
562
563 init_socket_opts(inf.fd);
564
565 while (1) {
566 recv_len = sock_recv_poll(recv_msg + offset,
567 max_msg_len - offset, &inf);
568
569 if (recv_len == 0)
570 break;
571
572 if (recv_len < 0 || (offset + recv_len) > max_msg_len ||
573 (recv_msg[0] != start_byte &&
574 recv_msg[0] != start_fin_byte)) {
575 tst_res(TFAIL, "recv failed, sock '%d'", inf.fd);
576 goto out;
577 }
578
579 offset += recv_len;
580
581 if (recv_msg[offset - 1] != end_byte) {
582 /* msg is not complete, continue recv */
583 continue;
584 }
585
586 /* client asks to terminate */
587 if (recv_msg[0] == start_fin_byte)
588 goto out;
589
590 send_msg_len = parse_client_request(recv_msg);
591 if (send_msg_len < 0) {
592 tst_res(TFAIL, "wrong msg size '%d'",
593 send_msg_len);
594 goto out;
595 }
596 make_server_reply(send_msg, send_msg_len);
597
598 offset = 0;
599
600 /*
601 * It will tell client that server is going
602 * to close this connection.
603 */
604 if (sock_type == SOCK_STREAM &&
605 ++num_requests >= server_max_requests)
606 send_msg[0] = start_fin_byte;
607
608 switch (send_type) {
609 case 0:
610 SAFE_SEND(1, inf.fd, send_msg, send_msg_len,
611 send_flags);
612 if (proto_type != TYPE_SCTP)
613 ++send_type;
614 break;
615 case 1:
616 SAFE_SENDTO(1, inf.fd, send_msg, send_msg_len,
617 send_flags, (struct sockaddr *)&inf.raddr,
618 inf.raddr_len);
619 ++send_type;
620 break;
621 default:
622 iov[0].iov_len = send_msg_len - 1;
623 msg.msg_namelen = inf.raddr_len;
624 SAFE_SENDMSG(send_msg_len, inf.fd, &msg, send_flags);
625 send_type = start_send_type;
626 break;
627 }
628
629 if (sock_type == SOCK_STREAM &&
630 num_requests >= server_max_requests) {
631 /* max reqs, close socket */
632 shutdown(inf.fd, SHUT_WR);
633 break;
634 }
635 }
636
637 SAFE_CLOSE(inf.fd);
638 return NULL;
639
640 out:
641 SAFE_CLOSE(inf.fd);
642 tst_brk(TBROK, "Server closed");
643 return NULL;
644 }
645
server_thread_add(intptr_t client_fd)646 static pthread_t server_thread_add(intptr_t client_fd)
647 {
648 pthread_t id;
649 SAFE_PTHREAD_CREATE(&id, &attr, server_fn, (void *) client_fd);
650 return id;
651 }
652
server_init(void)653 static void server_init(void)
654 {
655 char *src_addr = NULL;
656 struct addrinfo hints;
657
658 memset(&hints, 0, sizeof(struct addrinfo));
659 hints.ai_family = AF_INET6;
660 hints.ai_socktype = sock_type;
661 hints.ai_flags = AI_PASSIVE;
662
663 if (!source_addr && !tcp_port)
664 tcp_port = "0";
665
666 if (source_addr && !strchr(source_addr, ':'))
667 SAFE_ASPRINTF(&src_addr, "::ffff:%s", source_addr);
668 SAFE_GETADDRINFO(src_addr ? src_addr : source_addr, tcp_port,
669 &hints, &local_addrinfo);
670 free(src_addr);
671
672 /* IPv6 socket is also able to access IPv4 protocol stack */
673 sfd = SAFE_SOCKET(family, sock_type, protocol);
674 SAFE_SETSOCKOPT_INT(sfd, SOL_SOCKET, SO_REUSEADDR, 1);
675 if (reuse_port)
676 SAFE_SETSOCKOPT_INT(sfd, SOL_SOCKET, SO_REUSEPORT, 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_WRONLY, 0644);
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 /*
751 * detaching threads allow to reclaim thread's resources
752 * once a thread finishes its work.
753 */
754 if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0)
755 tst_brk(TBROK | TERRNO, "setdetachstate failed");
756
757 while (1) {
758 int client_fd = accept(sfd, (struct sockaddr *)&addr6,
759 &addr_size);
760
761 if (client_fd == -1)
762 tst_brk(TBROK, "Can't create client socket");
763
764 server_thread_add(client_fd);
765 }
766 }
767
require_root(const char * file)768 static void require_root(const char *file)
769 {
770 if (!geteuid())
771 return;
772 tst_brk(TCONF, "Test needs to be run as root to change %s", file);
773 }
774
check_tfo_value(void)775 static void check_tfo_value(void)
776 {
777 /* Check if we can write to tcp_fastopen knob. We might be
778 * inside netns and either have read-only permission or
779 * doesn't have the knob at all.
780 */
781 if (access(tfo_cfg, W_OK) < 0) {
782 /* TODO check /proc/self/ns/ or TST_USE_NETNS env var */
783 tst_res(TINFO, "can't read %s, assume server runs in netns",
784 tfo_cfg);
785 return;
786 }
787
788 SAFE_FILE_SCANF(tfo_cfg, "%d", &tfo_cfg_value);
789 tst_res(TINFO, "'%s' is %d", tfo_cfg, tfo_cfg_value);
790
791 /* The check can be the first in this function but set here
792 * to allow to print information about the currently set config
793 */
794 if (tfo_value < 0)
795 return;
796
797 if (tfo_cfg_value == tfo_value)
798 return;
799
800 require_root(tfo_cfg);
801
802 tst_res(TINFO, "set '%s' to '%d'", tfo_cfg, tfo_value);
803
804 SAFE_FILE_PRINTF(tfo_cfg, "%d", tfo_value);
805 tfo_cfg_changed = 1;
806 }
807
check_tw_reuse(void)808 static void check_tw_reuse(void)
809 {
810 if (access(tcp_tw_reuse, W_OK) < 0)
811 return;
812
813 int reuse_value = 0;
814
815 SAFE_FILE_SCANF(tcp_tw_reuse, "%d", &reuse_value);
816 if (reuse_value) {
817 tst_res(TINFO, "tcp_tw_reuse is already set");
818 return;
819 }
820
821 require_root(tfo_cfg);
822
823 SAFE_FILE_PRINTF(tcp_tw_reuse, "1");
824 tw_reuse_changed = 1;
825 tst_res(TINFO, "set '%s' to '1'", tcp_tw_reuse);
826 }
827
set_protocol_type(void)828 static void set_protocol_type(void)
829 {
830 if (!type || !strcmp(type, "tcp"))
831 proto_type = TYPE_TCP;
832 else if (!strcmp(type, "udp"))
833 proto_type = TYPE_UDP;
834 else if (!strcmp(type, "udp_lite"))
835 proto_type = TYPE_UDP_LITE;
836 else if (!strcmp(type, "dccp"))
837 proto_type = TYPE_DCCP;
838 else if (!strcmp(type, "sctp"))
839 proto_type = TYPE_SCTP;
840 else
841 tst_brk(TBROK, "Invalid proto_type: '%s'", type);
842 }
843
setup(void)844 static void setup(void)
845 {
846 if (tst_parse_int(aarg, &clients_num, 1, INT_MAX))
847 tst_brk(TBROK, "Invalid client number '%s'", aarg);
848 if (tst_parse_int(rarg, &client_max_requests, 1, INT_MAX))
849 tst_brk(TBROK, "Invalid client max requests '%s'", rarg);
850 if (tst_parse_int(Rarg, &server_max_requests, 1, INT_MAX))
851 tst_brk(TBROK, "Invalid server max requests '%s'", Rarg);
852 if (tst_parse_int(narg, &init_cln_msg_len, min_msg_len, max_msg_len))
853 tst_brk(TBROK, "Invalid client msg size '%s'", narg);
854 if (tst_parse_int(Narg, &init_srv_msg_len, min_msg_len, max_msg_len))
855 tst_brk(TBROK, "Invalid server msg size '%s'", Narg);
856 if (tst_parse_int(qarg, &tfo_queue_size, 1, INT_MAX))
857 tst_brk(TBROK, "Invalid TFO queue size '%s'", qarg);
858 if (tst_parse_int(Targ, &wait_timeout, 0, INT_MAX))
859 tst_brk(TBROK, "Invalid wait timeout '%s'", Targ);
860 if (tst_parse_int(barg, &busy_poll, 0, INT_MAX))
861 tst_brk(TBROK, "Invalid busy poll timeout'%s'", barg);
862 if (tst_parse_int(targ, &tfo_value, 0, INT_MAX))
863 tst_brk(TBROK, "Invalid net.ipv4.tcp_fastopen '%s'", targ);
864 if (tst_parse_int(Aarg, &max_rand_msg_len, 10, max_msg_len))
865 tst_brk(TBROK, "Invalid max random payload size '%s'", Aarg);
866
867 if (!server_addr)
868 server_addr = "localhost";
869
870 if (max_rand_msg_len) {
871 max_rand_msg_len -= min_msg_len;
872 init_seed = max_rand_msg_len ^ client_max_requests;
873 srand(init_seed); /* in case rand_r() is missing */
874 tst_res(TINFO, "rand start seed 0x%x", init_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 if (zcopy)
941 send_flags |= MSG_ZEROCOPY;
942
943 switch (proto_type) {
944 case TYPE_TCP:
945 tst_res(TINFO, "TCP %s is using %s TCP API.",
946 (client_mode) ? "client" : "server",
947 (fastopen_api) ? "Fastopen" : "old");
948 check_tfo_value();
949 break;
950 case TYPE_UDP:
951 tst_res(TINFO, "using UDP");
952 fastopen_api = fastopen_sapi = NULL;
953 sock_type = SOCK_DGRAM;
954 break;
955 case TYPE_UDP_LITE:
956 tst_res(TINFO, "using UDP Lite");
957 fastopen_api = fastopen_sapi = NULL;
958 sock_type = SOCK_DGRAM;
959 protocol = IPPROTO_UDPLITE;
960 break;
961 case TYPE_DCCP: {
962 /* dccp* modules can be blacklisted, load them manually */
963 const char * const argv[] = {"modprobe", "dccp_ipv6", NULL};
964
965 if (tst_cmd(argv, NULL, NULL, TST_CMD_PASS_RETVAL))
966 tst_brk(TCONF, "Failed to load dccp_ipv6 module");
967
968 tst_res(TINFO, "DCCP %s", (client_mode) ? "client" : "server");
969 fastopen_api = fastopen_sapi = NULL;
970 sock_type = SOCK_DCCP;
971 protocol = IPPROTO_DCCP;
972 service_code = htonl(service_code);
973 } break;
974 case TYPE_SCTP:
975 tst_res(TINFO, "SCTP %s", (client_mode) ? "client" : "server");
976 fastopen_api = fastopen_sapi = NULL;
977 protocol = IPPROTO_SCTP;
978 break;
979 }
980
981 if ((errno = pthread_attr_init(&attr)))
982 tst_brk(TBROK | TERRNO, "pthread_attr_init failed");
983
984 if ((errno = pthread_attr_setstacksize(&attr, 256*1024)))
985 tst_brk(TBROK | TERRNO, "pthread_attr_setstacksize(256*1024) failed");
986
987 net.init();
988 }
989
do_test(void)990 static void do_test(void)
991 {
992 net.run();
993 }
994
995 static struct tst_test test = {
996 .test_all = do_test,
997 .forks_child = 1,
998 .setup = setup,
999 .cleanup = cleanup,
1000 .options = (struct tst_option[]) {
1001 {"f", &fastopen_api, "Use TFO API, default is old API"},
1002 {"F", &fastopen_sapi, "TCP_FASTOPEN_CONNECT socket option and standard API"},
1003 {"t:", &targ, "Set tcp_fastopen value"},
1004 {"S:", &source_addr, "Source address to bind"},
1005 {"g:", &tcp_port, "Server port"},
1006 {"b:", &barg, "Low latency busy poll timeout"},
1007 {"T:", &type, "Tcp (default), udp, udp_lite, dccp, sctp"},
1008 {"z", &zcopy, "Enable SO_ZEROCOPY"},
1009 {"P:", &reuse_port, "Enable SO_REUSEPORT"},
1010 {"D:", &dev, "Bind to device x"},
1011
1012 {"H:", &server_addr, "Server name or IP address"},
1013 {"l", &client_mode, "Become client, default is server"},
1014 {"a:", &aarg, "Number of clients running in parallel"},
1015 {"r:", &rarg, "Number of client requests"},
1016 {"n:", &narg, "Client message size"},
1017 {"N:", &Narg, "Server message size"},
1018 {"m:", &Targ, "Receive timeout in milliseconds (not used by UDP/DCCP client)"},
1019 {"d:", &rpath, "Path to file where result is saved"},
1020 {"A:", &Aarg, "Max payload length (generated randomly)"},
1021
1022 {"R:", &Rarg, "Server requests after which conn.closed"},
1023 {"q:", &qarg, "TFO queue"},
1024 {"B:", &server_bg, "Run in background, arg is the process directory"},
1025 {}
1026 },
1027 };
1028