Imported Upstream version 1.0.10
[platform/upstream/lksctp-tools.git] / src / func_tests / test_1_to_1_threads.c
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 
67 t_recv (int id) {
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
90 t_send(int id) {
91         struct msghdr outmessage;
92         struct sctp_sndrcvinfo *sinfo;
93         char *buffer_snd;
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         buffer_snd = malloc(100);
100
101         outmessage.msg_name = &conn_addr;
102         outmessage.msg_namelen = sizeof(conn_addr);
103         outmessage.msg_iov = &out_iov;
104         outmessage.msg_iovlen = 1;
105         outmessage.msg_control = outcmsg;
106         outmessage.msg_controllen = sizeof(outcmsg);
107         outmessage.msg_flags = 0;
108
109         cmsg = CMSG_FIRSTHDR(&outmessage);
110         cmsg->cmsg_level = IPPROTO_SCTP;
111         cmsg->cmsg_type = SCTP_SNDRCV;
112         cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
113         outmessage.msg_controllen = cmsg->cmsg_len;
114
115         sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
116         memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
117         outmessage.msg_iov->iov_base = message;
118         outmessage.msg_iov->iov_len = (strlen(message) + 1);
119
120         test_sendmsg(client_sk, &outmessage, 0, strlen(message)+1);
121 }
122
123 void * relay (int id) {
124         if (id == 0) {
125                 t_send(id);
126         } else if (id == THREADS -1) {
127                 t_send(id);     
128         } else {
129                 t_recv (id);
130                 t_send(id);
131         }
132
133         return 0;
134 }
135         
136
137 int 
138 main(void) 
139 {
140
141         int      cnt,i;
142         pthread_t       thread[THREADS];
143         int  status;
144         int  exit_status;
145         void *      result;
146         pthread_attr_t attr;
147         struct sockaddr_in lstn_addr;
148         socklen_t len = sizeof(struct sockaddr_in);
149         struct sockaddr_in svr_addr;
150
151         pthread_attr_init(&attr);
152         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
153         
154         server_sk = test_socket(PF_INET, SOCK_STREAM, IPPROTO_SCTP);
155         client_sk = test_socket(PF_INET, SOCK_STREAM, IPPROTO_SCTP);
156
157         lstn_addr.sin_family = AF_INET;
158         lstn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
159         lstn_addr.sin_port = htons(SCTP_TESTPORT_1);
160
161         conn_addr.sin_family = AF_INET;
162         conn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
163         conn_addr.sin_port = htons(SCTP_TESTPORT_1);
164
165         test_bind(server_sk, (struct sockaddr *)&lstn_addr,
166                  sizeof(struct sockaddr_in));
167
168         test_listen(server_sk,10);
169
170         test_connect(client_sk,(struct sockaddr *)&conn_addr,len);
171
172         acpt_sk = test_accept(server_sk, (struct sockaddr *)&svr_addr, &len);
173
174         for ( i = 0; i < THREAD_SND_RCV_LOOPS; i++ ) {
175                 for (cnt = 1; cnt < THREADS; cnt++) {
176                         status = pthread_create(&thread[cnt], &attr,
177                                                 (void *)relay, (void*)cnt);
178                         if (status)
179                                 tst_brkm(TBROK, tst_exit, "pthread_create "
180                                          "failed status:%d, errno:%d", status,
181                                          errno);
182                 }
183
184                 pthread_attr_destroy(&attr);
185                 for (cnt = 1; cnt < THREADS ; cnt++) {
186                         exit_status = pthread_join (thread[cnt], &result);
187                         if (exit_status == -1)
188                                 tst_brkm(TBROK, tst_exit, "pthread_join "
189                                          "Thread #%d exited with status:%d",
190                                          cnt, exit_status);
191                 }
192         }
193
194         tst_resm(TPASS, "send and receive data across multiple threads - "
195                  "SUCCESS");
196
197         pthread_exit(NULL);
198 }