| 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 | * https://github.com/sctp/lksctp-tools/ |
| 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(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 = test_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 | free(buffer); |
| 88 | } |
| 89 | |
| 90 | 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 | |
| 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 |
| 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 | pthread_attr_init(&attr); |
| 151 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); |
| 152 | |
| 153 | server_sk = test_socket(PF_INET, SOCK_STREAM, IPPROTO_SCTP); |
| 154 | client_sk = test_socket(PF_INET, SOCK_STREAM, IPPROTO_SCTP); |
| 155 | |
| 156 | lstn_addr.sin_family = AF_INET; |
| 157 | lstn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK; |
| 158 | lstn_addr.sin_port = htons(SCTP_TESTPORT_1); |
| 159 | |
| 160 | conn_addr.sin_family = AF_INET; |
| 161 | conn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK; |
| 162 | conn_addr.sin_port = htons(SCTP_TESTPORT_1); |
| 163 | |
| 164 | test_bind(server_sk, (struct sockaddr *)&lstn_addr, |
| 165 | sizeof(struct sockaddr_in)); |
| 166 | |
| 167 | test_listen(server_sk,10); |
| 168 | |
| 169 | test_connect(client_sk,(struct sockaddr *)&conn_addr,len); |
| 170 | |
| 171 | acpt_sk = test_accept(server_sk, (struct sockaddr *)&svr_addr, &len); |
| 172 | |
| 173 | for ( i = 0; i < THREAD_SND_RCV_LOOPS; i++ ) { |
| 174 | for (cnt = 0; cnt < THREADS; cnt++) { |
| 175 | pth[cnt] = cnt; |
| 176 | status = pthread_create(&thread[cnt], &attr, relay, &pth[cnt]); |
| 177 | if (status) |
| 178 | tst_brkm(TBROK, tst_exit, "pthread_create " |
| 179 | "failed status:%d, errno:%d", status, |
| 180 | errno); |
| 181 | } |
| 182 | |
| 183 | pthread_attr_destroy(&attr); |
| 184 | for (cnt = 0; cnt < THREADS ; cnt++) { |
| 185 | exit_status = pthread_join (thread[cnt], &result); |
| 186 | if (exit_status == -1) |
| 187 | tst_brkm(TBROK, tst_exit, "pthread_join " |
| 188 | "Thread #%d exited with status:%d", |
| 189 | cnt, exit_status); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | tst_resm(TPASS, "send and receive data across multiple threads - " |
| 194 | "SUCCESS"); |
| 195 | |
| 196 | return 0; |
| 197 | } |