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 <lksctp-developers@lists.sourceforge.net>
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
53 #define THREADS 10 /* FIXME should be 500 instead of 10 */
54 #define THREAD_SND_RCV_LOOPS 10
55
56 char *TCID = __FILE__;
57 int TST_TOTAL = 1;
58 int TST_CNT = 0;
59
60 int client_sk;
61 int server_sk;
62 int acpt_sk;
63 struct sockaddr_in conn_addr;
64 char *message = "hello, world!\n";
65
66 void
t_recv(void)67 t_recv(void) {
68 int cnt;
69 struct msghdr inmessage;
70 struct iovec iov;
71 char incmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
72 char * buffer;
73
74 memset(&inmessage, 0, sizeof(inmessage));
75 buffer = malloc(100);
76
77 iov.iov_base = buffer;
78 iov.iov_len = 100;
79 inmessage.msg_iov = &iov;
80 inmessage.msg_iovlen = 1;
81 inmessage.msg_control = incmsg;
82 inmessage.msg_controllen = sizeof(incmsg);
83
84 cnt = test_recvmsg(acpt_sk,&inmessage, MSG_WAITALL);
85 test_check_msg_data(&inmessage, cnt, strlen(message) + 1, MSG_EOR,
86 0, 0);
87 }
88
89 void
t_send(void)90 t_send(void) {
91 struct msghdr outmessage;
92 struct sctp_sndrcvinfo *sinfo;
93 struct cmsghdr *cmsg;
94 struct iovec out_iov;
95 char outcmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))];
96
97 memset(&outmessage, 0, sizeof(outmessage));
98 outmessage.msg_name = &conn_addr;
99 outmessage.msg_namelen = sizeof(conn_addr);
100 outmessage.msg_iov = &out_iov;
101 outmessage.msg_iovlen = 1;
102 outmessage.msg_control = outcmsg;
103 outmessage.msg_controllen = sizeof(outcmsg);
104 outmessage.msg_flags = 0;
105
106 cmsg = CMSG_FIRSTHDR(&outmessage);
107 cmsg->cmsg_level = IPPROTO_SCTP;
108 cmsg->cmsg_type = SCTP_SNDRCV;
109 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
110 outmessage.msg_controllen = cmsg->cmsg_len;
111
112 sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
113 memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
114 outmessage.msg_iov->iov_base = message;
115 outmessage.msg_iov->iov_len = (strlen(message) + 1);
116
117 test_sendmsg(client_sk, &outmessage, 0, strlen(message)+1);
118 }
119
relay(void * arg)120 void *relay(void *arg)
121 {
122 int id = *(int *) arg;
123
124 if (id == 0) {
125 t_send();
126 } else {
127 t_recv();
128 t_send();
129 }
130
131 pthread_exit(NULL);
132 }
133
134 int
main(void)135 main(void)
136 {
137
138 int cnt,i;
139 int pth[THREADS];
140 pthread_t thread[THREADS];
141 int status;
142 int exit_status;
143 void * result;
144 pthread_attr_t attr;
145 struct sockaddr_in lstn_addr;
146 socklen_t len = sizeof(struct sockaddr_in);
147 struct sockaddr_in svr_addr;
148
149 pthread_attr_init(&attr);
150 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
151
152 server_sk = test_socket(PF_INET, SOCK_STREAM, IPPROTO_SCTP);
153 client_sk = test_socket(PF_INET, SOCK_STREAM, IPPROTO_SCTP);
154
155 lstn_addr.sin_family = AF_INET;
156 lstn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
157 lstn_addr.sin_port = htons(SCTP_TESTPORT_1);
158
159 conn_addr.sin_family = AF_INET;
160 conn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
161 conn_addr.sin_port = htons(SCTP_TESTPORT_1);
162
163 test_bind(server_sk, (struct sockaddr *)&lstn_addr,
164 sizeof(struct sockaddr_in));
165
166 test_listen(server_sk,10);
167
168 test_connect(client_sk,(struct sockaddr *)&conn_addr,len);
169
170 acpt_sk = test_accept(server_sk, (struct sockaddr *)&svr_addr, &len);
171
172 for ( i = 0; i < THREAD_SND_RCV_LOOPS; i++ ) {
173 for (cnt = 0; cnt < THREADS; cnt++) {
174 pth[cnt] = cnt;
175 status = pthread_create(&thread[cnt], &attr, relay, &pth[cnt]);
176 if (status)
177 tst_brkm(TBROK, tst_exit, "pthread_create "
178 "failed status:%d, errno:%d", status,
179 errno);
180 }
181
182 pthread_attr_destroy(&attr);
183 for (cnt = 0; cnt < THREADS ; cnt++) {
184 exit_status = pthread_join (thread[cnt], &result);
185 if (exit_status == -1)
186 tst_brkm(TBROK, tst_exit, "pthread_join "
187 "Thread #%d exited with status:%d",
188 cnt, exit_status);
189 }
190 }
191
192 tst_resm(TPASS, "send and receive data across multiple threads - "
193 "SUCCESS");
194
195 return 0;
196 }
197