Imported Upstream version 1.0.10
[platform/upstream/lksctp-tools.git] / src / func_tests / test_fragments.c
1 /* SCTP kernel Implementation
2  * (C) Copyright IBM Corp. 2001, 2003
3  * Copyright (c) 1999-2000 Cisco, Inc.
4  * Copyright (c) 1999-2001 Motorola, Inc.
5  * Copyright (c) 2001 Intel Corp.
6  * Copyright (c) 2001 Nokia, Inc.
7  *
8  * The SCTP implementation is free software;
9  * you can redistribute it and/or modify it under the terms of
10  * the GNU General Public License as published by
11  * the Free Software Foundation; either version 2, or (at your option)
12  * any later version.
13  *
14  * The SCTP implementation is distributed in the hope that it
15  * will be useful, but WITHOUT ANY WARRANTY; without even the implied
16  *                 ************************
17  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18  * See the GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with GNU CC; see the file COPYING.  If not, write to
22  * the Free Software Foundation, 59 Temple Place - Suite 330,
23  * Boston, MA 02111-1307, USA.
24  *
25  * Please send any bug reports or fixes you make to the
26  * email address(es):
27  *    lksctp developers <lksctp-developers@lists.sourceforge.net>
28  *
29  * Or submit a bug report through the following website:
30  *    http://www.sf.net/projects/lksctp
31  *
32  * Any bugs reported to us we will try to fix... any fixes shared will
33  * be incorporated into the next SCTP release.
34  *
35  * Written or modified by:
36  *    La Monte H.P. Yarroll <piggy@acm.org>
37  *    Karl Knutson <karl@athena.chicago.il.us>
38  *    Hui Huang <hui.huang@nokia.com>
39  *    Jon Grimm <jgrimm@us.ibm.com>
40  *    Sridhar Samudrala <sri@us.ibm.com>
41  */
42
43 /* This is a functional test to verify the data fragmentation, reassembly 
44  * support and SCTP_DISABLE_FRAGMENTS socket option. 
45  * The following tests are done in sequence.
46  * - Verify SCTP_DISABLE_FRAGMENTS socket option by doing a setsockopt()
47  *   followed by a getsockopt().
48  * - Verify that a message size exceeding the association fragmentation
49  *   point cannot be sent when fragmentation is disabled.
50  * - Send and receive a set of messages that are bigger than the path mtu. 
51  *   The different message sizes to be tested are specified in the array 
52  *   msg_sizes[]. 
53  */
54
55 #include <stdio.h>
56 #include <unistd.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <sys/types.h>
60 #include <sys/socket.h>
61 #include <sys/uio.h>
62 #include <netinet/in.h>
63 #include <sys/errno.h>
64 #include <errno.h>
65 #include <netinet/sctp.h>
66 #include <sctputil.h>
67
68 char *TCID = __FILE__;
69 int TST_TOTAL = 4;
70 int TST_CNT = 0;
71
72 int msg_sizes[] = {1353, 2000, 5000, 10000, 20000, 32768};
73
74 int
75 main(int argc, char *argv[])
76 {
77         int sk1, sk2;
78         sockaddr_storage_t loop1;
79         sockaddr_storage_t loop2;
80         struct iovec iov;
81         struct msghdr inmessage;
82         struct msghdr outmessage;
83         char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))];
84         char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
85         struct cmsghdr *cmsg;
86         struct sctp_sndrcvinfo *sinfo;
87         struct iovec out_iov;
88         int error, bytes_sent;
89         int pf_class, af_family;
90         uint32_t ppid;
91         uint32_t stream;
92         sctp_assoc_t associd1, associd2;
93         struct sctp_assoc_change *sac;
94         char *big_buffer;
95         int msg_len, msg_cnt, i;
96         void *msg_buf;
97         int disable_frag;
98         socklen_t optlen;
99
100         /* Rather than fflush() throughout the code, set stdout to 
101          * be unbuffered. 
102          */
103         setvbuf(stdout, NULL, _IONBF, 0); 
104
105         /* Set some basic values which depend on the address family. */
106 #if TEST_V6
107         pf_class = PF_INET6;
108         af_family = AF_INET6;
109
110         loop1.v6.sin6_family = AF_INET6;
111         loop1.v6.sin6_addr = in6addr_loopback;
112         loop1.v6.sin6_port = htons(SCTP_TESTPORT_1);
113
114         loop2.v6.sin6_family = AF_INET6;
115         loop2.v6.sin6_addr = in6addr_loopback;
116         loop2.v6.sin6_port = htons(SCTP_TESTPORT_2);
117 #else
118         pf_class = PF_INET;
119         af_family = AF_INET;
120
121         loop1.v4.sin_family = AF_INET;
122         loop1.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
123         loop1.v4.sin_port = htons(SCTP_TESTPORT_1);
124
125         loop2.v4.sin_family = AF_INET;
126         loop2.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
127         loop2.v4.sin_port = htons(SCTP_TESTPORT_2);
128 #endif /* TEST_V6 */
129
130         /* Create the two endpoints which will talk to each other.  */
131         sk1 = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP);
132         sk2 = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP);
133
134         /* Enable ASSOC_CHANGE and SNDRCVINFO notifications. */
135         test_enable_assoc_change(sk1);
136         test_enable_assoc_change(sk2);
137
138         /* Bind these sockets to the test ports.  */
139         test_bind(sk1, &loop1.sa, sizeof(loop1));
140         test_bind(sk2, &loop2.sa, sizeof(loop2));
141
142        /* Mark sk2 as being able to accept new associations.  */
143         test_listen(sk2, 1);
144
145         /* Send the first message.  This will create the association.  */
146         outmessage.msg_name = &loop2;
147         outmessage.msg_namelen = sizeof(loop2);
148         outmessage.msg_iov = &out_iov;
149         outmessage.msg_iovlen = 1;
150         outmessage.msg_control = outcmsg;
151         outmessage.msg_controllen = sizeof(outcmsg);
152         outmessage.msg_flags = 0;
153         cmsg = CMSG_FIRSTHDR(&outmessage);
154         cmsg->cmsg_level = IPPROTO_SCTP;
155         cmsg->cmsg_type = SCTP_SNDRCV;
156         cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
157         outmessage.msg_controllen = cmsg->cmsg_len;
158         sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
159         memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
160         ppid = rand(); /* Choose an arbitrary value. */
161         stream = 1; 
162         sinfo->sinfo_ppid = ppid;
163         sinfo->sinfo_stream = stream;
164         msg_len = 10;   
165         msg_buf = test_build_msg(10);
166         outmessage.msg_iov->iov_base = msg_buf;
167         outmessage.msg_iov->iov_len = msg_len;
168         test_sendmsg(sk1, &outmessage, 0, msg_len);
169         
170
171         /* Initialize inmessage for all receives. */
172         big_buffer = test_malloc(REALLY_BIG);
173         memset(&inmessage, 0, sizeof(inmessage));       
174         iov.iov_base = big_buffer;
175         iov.iov_len = REALLY_BIG;
176         inmessage.msg_iov = &iov;
177         inmessage.msg_iovlen = 1;
178         inmessage.msg_control = incmsg;
179
180         /* Get the communication up message on sk2.  */
181         inmessage.msg_controllen = sizeof(incmsg);
182         error = test_recvmsg(sk2, &inmessage, MSG_WAITALL);
183         test_check_msg_notification(&inmessage, error,
184                                     sizeof(struct sctp_assoc_change),
185                                     SCTP_ASSOC_CHANGE, SCTP_COMM_UP);   
186         sac = (struct sctp_assoc_change *)iov.iov_base;
187         associd2 = sac->sac_assoc_id;
188
189         /* Get the communication up message on sk1.  */
190         inmessage.msg_controllen = sizeof(incmsg);
191         error = test_recvmsg(sk1, &inmessage, MSG_WAITALL);
192         test_check_msg_notification(&inmessage, error,
193                                     sizeof(struct sctp_assoc_change),
194                                     SCTP_ASSOC_CHANGE, SCTP_COMM_UP);   
195         sac = (struct sctp_assoc_change *)iov.iov_base;
196         associd1 = sac->sac_assoc_id;
197
198         /* Get the first message which was sent.  */
199         inmessage.msg_controllen = sizeof(incmsg);
200         error = test_recvmsg(sk2, &inmessage, MSG_WAITALL);
201         test_check_msg_data(&inmessage, error, msg_len, MSG_EOR, stream, ppid);
202
203         free(msg_buf);
204
205         /* Disable fragmentation. */
206         disable_frag = 1;
207         test_setsockopt(sk1, SCTP_DISABLE_FRAGMENTS, &disable_frag,
208                         sizeof(disable_frag));
209
210         tst_resm(TPASS, "setsockopt(SCTP_DISABLE_FRAGMENTS)");
211
212         /* Do a getsockopt() and verify that fragmentation is disabled. */ 
213         disable_frag = 0;
214         optlen = sizeof(disable_frag);
215         error = test_getsockopt(sk1, SCTP_DISABLE_FRAGMENTS, &disable_frag,
216                                 &optlen);
217         if ((error != 0) && (disable_frag != 1))
218                 tst_brkm(TBROK, tst_exit, "getsockopt(SCTP_DISABLE_FRAGMENTS) "
219                          "error:%d errno:%d disable_frag:%d",
220                          error, errno, disable_frag);
221
222         tst_resm(TPASS, "getsockopt(SCTP_DISABLE_FRAGMENTS)");
223
224         /* Try to send a messsage that exceeds association fragmentation point
225          * and verify that it fails.
226          */
227         msg_len = 30000;
228         msg_buf = test_build_msg(msg_len);
229         outmessage.msg_iov->iov_base = msg_buf;
230         outmessage.msg_iov->iov_len = msg_len;
231         error = sendmsg(sk1, &outmessage, 0);
232         if ((error != -1) || (errno != EMSGSIZE))
233                 tst_brkm(TBROK, tst_exit, "Send a message that exceeds "
234                          "assoc frag point error:%d errno:%d", error, errno);
235
236         tst_resm(TPASS, "Send a message that exceeds assoc frag point");
237
238         /* Enable Fragmentation. */
239         disable_frag = 0;
240         test_setsockopt(sk1, SCTP_DISABLE_FRAGMENTS, &disable_frag,
241                         sizeof(disable_frag));
242
243         msg_cnt = sizeof(msg_sizes) / sizeof(int);
244
245         /* Send and receive the messages of different sizes specified in the
246          * msg_sizes array in a loop.
247          */
248         for (i = 0; i < msg_cnt; i++) {
249
250                 msg_len = msg_sizes[i];
251                 msg_buf = test_build_msg(msg_len);
252                 outmessage.msg_iov->iov_base = msg_buf;
253                 outmessage.msg_iov->iov_len = msg_len;
254                 bytes_sent = test_sendmsg(sk1, &outmessage, 0, msg_len);
255                 
256                 tst_resm(TINFO, "Sent %d byte message", bytes_sent);
257
258                 inmessage.msg_controllen = sizeof(incmsg);
259                 error = test_recvmsg(sk2, &inmessage, MSG_WAITALL);
260                 /* Handle Partial Reads. */ 
261                 if (inmessage.msg_flags & MSG_EOR) {
262                         test_check_msg_data(&inmessage, error, bytes_sent,
263                                             MSG_EOR, stream, ppid);
264                         tst_resm(TINFO, "Received %d byte message", error);
265                 } else {
266                         int remain;
267
268                         test_check_msg_data(&inmessage, error, error, 0,
269                                             stream, ppid);
270                         tst_resm(TINFO, "Received %d byte message", error);
271
272                         /* Read the remaining message. */
273                         inmessage.msg_controllen = sizeof(incmsg);
274                         remain = test_recvmsg(sk2, &inmessage, MSG_WAITALL);
275                         test_check_msg_data(&inmessage, remain,
276                                             bytes_sent - error,
277                                             MSG_EOR, stream, ppid);
278                         tst_resm(TINFO, "Received %d byte message", error);
279                 }
280
281                 free(msg_buf);
282         }
283
284         tst_resm(TPASS, "Send/Receive fragmented messages");
285
286         /* Shut down the link.  */
287         close(sk1);
288
289         /* Get the shutdown complete notification. */
290         inmessage.msg_controllen = sizeof(incmsg);
291         error = test_recvmsg(sk2, &inmessage, MSG_WAITALL);
292         test_check_msg_notification(&inmessage, error,
293                                     sizeof(struct sctp_assoc_change),
294                                     SCTP_ASSOC_CHANGE, SCTP_SHUTDOWN_COMP);
295                                 
296         close(sk2);
297
298         /* Indicate successful completion.  */
299         return 0; 
300 }