xref: /aosp_15_r20/external/ltp/utils/sctp/func_tests/test_1_to_1_threads.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 /* SCTP kernel Implementation
2  * Copyright (c) 2003 Hewlett-Packard Development Company, L.P
3  * (C) Copyright IBM Corp. 2004
4  *
5  * This file does send and receive for 500 threads on a unique association for
6  * THREAD_SND_RCV_LOOPS = 10 many times. To change the number of threads
7  * change the THREADS valuen and loop change the THREAD_SND_RCV_LOOPS.
8  *
9  * The SCTP implementation is free software;
10  * you can redistribute it and/or modify it under the terms of
11  * the GNU General Public License as published by
12  * the Free Software Foundation; either version 2, or (at your option)
13  * any later version.
14  *
15  * The SCTP implementation is distributed in the hope that it
16  * will be useful, but WITHOUT ANY WARRANTY; without even the implied
17  *                 ************************
18  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19  * See the GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with GNU CC; see the file COPYING.  If not, write to
23  * the Free Software Foundation, 59 Temple Place - Suite 330,
24  * Boston, MA 02111-1307, USA.
25  *
26  * Please send any bug reports or fixes you make to the
27  * email address(es):
28  *    lksctp developers <[email protected]>
29  *
30  * Or submit a bug report through the following website:
31  *    http://www.sf.net/projects/lksctp
32  *
33  * Any bugs reported given to us we will try to fix... any fixes shared will
34  * be incorporated into the next SCTP release
35  *
36  */
37 #include <pthread.h>
38 #include <stdio.h>
39 #include <unistd.h>
40 #include <fcntl.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <sys/types.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>         /* for sockaddr_in */
46 #include <arpa/inet.h>
47 #include <errno.h>
48 #include <netinet/sctp.h>
49 #include <sys/uio.h>
50 #include <linux/socket.h>
51 #include <sctputil.h>
52 #include "tst_kernel.h"
53 
54 #define THREADS 10    /* FIXME should be 500 instead of 10 */
55 #define THREAD_SND_RCV_LOOPS 10
56 
57 char *TCID = __FILE__;
58 int TST_TOTAL = 1;
59 int TST_CNT = 0;
60 
61 int client_sk;
62 int server_sk;
63 int acpt_sk;
64 struct sockaddr_in  conn_addr;
65 char *message = "hello, world!\n";
66 
67 void
t_recv(void)68 t_recv(void) {
69 	int cnt;
70 	struct msghdr inmessage;
71 	struct iovec iov;
72         char incmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
73         char * buffer;
74 
75 	memset(&inmessage, 0, sizeof(inmessage));
76         buffer = malloc(100);
77 
78         iov.iov_base = buffer;
79         iov.iov_len = 100;
80         inmessage.msg_iov = &iov;
81         inmessage.msg_iovlen = 1;
82         inmessage.msg_control = incmsg;
83         inmessage.msg_controllen = sizeof(incmsg);
84 
85 	cnt = test_recvmsg(acpt_sk,&inmessage, MSG_WAITALL);
86         test_check_msg_data(&inmessage, cnt, strlen(message) + 1, MSG_EOR,
87 			    0, 0);
88 }
89 
90 void
t_send(void)91 t_send(void) {
92         struct msghdr outmessage;
93         struct sctp_sndrcvinfo *sinfo;
94         struct cmsghdr *cmsg;
95         struct iovec out_iov;
96         char outcmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))];
97 
98         memset(&outmessage, 0, sizeof(outmessage));
99         outmessage.msg_name = &conn_addr;
100         outmessage.msg_namelen = sizeof(conn_addr);
101         outmessage.msg_iov = &out_iov;
102         outmessage.msg_iovlen = 1;
103         outmessage.msg_control = outcmsg;
104         outmessage.msg_controllen = sizeof(outcmsg);
105         outmessage.msg_flags = 0;
106 
107         cmsg = CMSG_FIRSTHDR(&outmessage);
108 	cmsg->cmsg_level = IPPROTO_SCTP;
109         cmsg->cmsg_type = SCTP_SNDRCV;
110         cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
111         outmessage.msg_controllen = cmsg->cmsg_len;
112 
113         sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
114         memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
115         outmessage.msg_iov->iov_base = message;
116         outmessage.msg_iov->iov_len = (strlen(message) + 1);
117 
118         test_sendmsg(client_sk, &outmessage, 0, strlen(message)+1);
119 }
120 
relay(void * arg)121 void *relay(void *arg)
122 {
123 	int id = *(int *) arg;
124 
125 	if (id == 0) {
126 		t_send();
127 	} else {
128 		t_recv();
129 		t_send();
130 	}
131 
132 	pthread_exit(NULL);
133 }
134 
135 int
main(void)136 main(void)
137 {
138 
139 	int      cnt,i;
140 	int      pth[THREADS];
141 	pthread_t       thread[THREADS];
142 	int  status;
143 	int  exit_status;
144 	void *      result;
145 	pthread_attr_t attr;
146 	struct sockaddr_in lstn_addr;
147 	socklen_t len = sizeof(struct sockaddr_in);
148 	struct sockaddr_in svr_addr;
149 
150 	if (tst_check_driver("sctp"))
151 		tst_brkm(TCONF, tst_exit, "sctp driver not available");
152 
153 	pthread_attr_init(&attr);
154 	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
155 
156 	server_sk = test_socket(PF_INET, SOCK_STREAM, IPPROTO_SCTP);
157 	client_sk = test_socket(PF_INET, SOCK_STREAM, IPPROTO_SCTP);
158 
159 	lstn_addr.sin_family = AF_INET;
160         lstn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
161 	lstn_addr.sin_port = htons(SCTP_TESTPORT_1);
162 
163 	conn_addr.sin_family = AF_INET;
164         conn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
165 	conn_addr.sin_port = htons(SCTP_TESTPORT_1);
166 
167 	test_bind(server_sk, (struct sockaddr *)&lstn_addr,
168 		 sizeof(struct sockaddr_in));
169 
170 	test_listen(server_sk,10);
171 
172 	test_connect(client_sk,(struct sockaddr *)&conn_addr,len);
173 
174 	acpt_sk = test_accept(server_sk, (struct sockaddr *)&svr_addr, &len);
175 
176 	for ( i = 0; i < THREAD_SND_RCV_LOOPS; i++ ) {
177 		for (cnt = 0; cnt < THREADS; cnt++) {
178 			pth[cnt] = cnt;
179 			status = pthread_create(&thread[cnt], &attr, relay, &pth[cnt]);
180 			if (status)
181 				tst_brkm(TBROK, tst_exit, "pthread_create "
182                          		 "failed status:%d, errno:%d", status,
183 					 errno);
184 		}
185 
186 		pthread_attr_destroy(&attr);
187 		for (cnt = 0; cnt < THREADS ; cnt++) {
188 			exit_status = pthread_join (thread[cnt], &result);
189 			if (exit_status == -1)
190 				tst_brkm(TBROK, tst_exit, "pthread_join "
191                          		 "Thread #%d exited with status:%d",
192 					 cnt, exit_status);
193 		}
194 	}
195 
196 	tst_resm(TPASS, "send and receive data across multiple threads - "
197 		 "SUCCESS");
198 
199 	return 0;
200 }
201