Debian Codegraph @

src/apps/nagle_snd.c

raw
1 /* SCTP kernel Implementation
2 * (C) Copyright IBM Corp. 2002, 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 <[email protected]>
28 *
29 * Or submit a bug report through the following website:
30 * https://github.com/sctp/lksctp-tools/
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 * Ardelle Fan <[email protected]>
37 * Sridhar Samudrala <[email protected]>
38 */
39
40 /* This is a receiver for the performance test to verify Nagle's algorithm.
41 * It creates a socket, binds to a address specified as a parameter and
42 * sends 1,000,000 packets to a specified target.
43 */
44
45 #include <stdio.h>
46 #include <unistd.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <sys/types.h>
50 #include <sys/socket.h>
51 #include <sys/uio.h>
52 #include <netinet/in.h>
53 #include <errno.h>
54 #include <netinet/sctp.h>
55 #include <sctputil.h>
56 #include <getopt.h>
57 #include <netdb.h>
58
59 char *TCID = __FILE__;
60 int TST_TOTAL = 1;
61 int TST_CNT = 0;
62
63 void
64 usage(char *argv0)
65 {
66 fprintf(stderr, "Usage: %s -H localhost [-P localport] "
67 "-h remotehost [-p remoteport]\n"
68 "\t\t[-S msgsize] [-I interval] -N\n"
69 " -H, --local\t\tspecify one of the local addresses,\n"
70 " -P, --local-port\tspecify the port number for local addresses,\n"
71 " -h, --remote\t\tspecify one of the remote addresses,\n"
72 " -p, --remote-port\tspecify the port number for remote addresses,\n"
73 " -S, --size\t\tspecify the size(byte) of the sending message,\n"
74 " -I, --interval\t\tspecify the interval(second) that sending messages at,\n"
75 " -N, --nodelay\t\tspecify whether the SCTP allows Nagle's algorithm\n",
76 argv0);
77 }
78
79 int
80 main(int argc, char *argv[])
81 {
82 int sk, i;
83 struct addrinfo *hst_res, *tgt_res;
84 sockaddr_storage_t host, target;
85 sockaddr_storage_t msgname;
86 struct iovec iov;
87 struct msghdr inmessage;
88 struct msghdr outmessage;
89 char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))];
90 char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
91 struct cmsghdr *cmsg;
92 struct sctp_sndrcvinfo *sinfo;
93 struct iovec out_iov;
94 char *message;
95 int error, pf_class;
96 sctp_assoc_t associd;
97 uint32_t ppid;
98 uint32_t stream;
99 char *remote_host = NULL;
100 int remote_port = SCTP_TESTPORT_1;
101 char *local_host = NULL;
102 int local_port = SCTP_TESTPORT_2;
103 int size = 1;
104 int interval = 0;
105 int nodelay = 0;
106 int option_index = 0;
107 char *big_buffer;
108 char port_buffer[10];
109 int c;
110 static struct option long_options[] = {
111 {"local", 1, 0, 1},
112 {"local-port", 1, 0, 2},
113 {"remote", 1, 0, 3},
114 {"remote-port", 1, 0, 4},
115 {"size", 1, 0, 5},
116 {"interval", 1, 0, 6},
117 {"nodelay", 0, 0, 10},
118 {0, 0, 0, 0}
119 };
120
121 /* Rather than fflush() throughout the code, set stdout to
122 * be unbuffered.
123 */
124 setvbuf(stdout, NULL, _IONBF, 0);
125
126 /* Parse the arguments. */
127 while (1) {
128 c = getopt_long (argc, argv, "H:P:h:p:S:I:N",
129 long_options, &option_index);
130 if (c == -1)
131 break;
132
133 switch (c) {
134 case 0:
135 printf("option %s", long_options[option_index].name);
136 if (optarg) {
137 printf(" with arg %s", optarg);
138 }
139 printf("\n");
140 break;
141 case 1: /* local host */
142 case 'H':
143 local_host = optarg;
144 break;
145 case 2: /* local port */
146 case 'P':
147 local_port = atoi(optarg);
148 break;
149 case 3: /* remote host */
150 case 'h':
151 remote_host = optarg;
152 break;
153 case 4: /* remote port */
154 case 'p':
155 remote_port = atoi(optarg);
156 break;
157 case 5: /* size */
158 case 'S':
159 size = atoi(optarg);
160 break;
161 case 6: /* interval */
162 case 'I':
163 interval = atoi(optarg);
164 break;
165 case 10: /* nodelay */
166 case 'N':
167 nodelay = 1;
168 break;
169 case '?':
170 usage(argv[0]);
171 exit(0);
172
173 default:
174 printf ("%s: unrecognized option 0%c\n",
175 argv[0], c);
176 usage(argv[0]);
177 exit(1);
178 }
179 }
180
181 if (optind < argc)
182 {
183 fprintf(stderr, "%s: non-option arguments are illegal: ",
184 argv[0]);
185 while (optind < argc)
186 fprintf(stderr, "%s ", argv[optind++]);
187 fprintf (stderr, "\n");
188 usage(argv[0]);
189 exit(1);
190 }
191
192 if (!local_host || !remote_host) {
193 fprintf(stderr, "%s: : option --local and --remote are required\n",
194 argv[0]);
195 usage(argv[0]);
196 exit(1);
197 }
198
199 /* Set some basic values which depend on the address family. */
200 if (!strcmp(local_host, "0"))
201 local_host = "0.0.0.0";
202
203 snprintf(port_buffer, 10, "%d", local_port);
204 error = getaddrinfo(local_host, port_buffer, NULL, &hst_res);
205 if (error) {
206 fprintf(stderr, "%s: getaddrinfo failed: %s\n", argv[0], local_host);
207 exit(1);
208 }
209
210 snprintf(port_buffer, 10, "%d", remote_port);
211 error = getaddrinfo(remote_host, port_buffer, NULL, &tgt_res);
212 if (error) {
213 fprintf(stderr, "%s: getaddrinfo failed: %s\n", argv[0], remote_host);
214 exit(1);
215 }
216
217 if ( hst_res->ai_family != tgt_res->ai_family) {
218 fprintf(stderr, "local and reomte hosts should be the "
219 "same address family\n");
220 exit(1);
221 }
222
223 pf_class = hst_res->ai_family;
224 switch (pf_class) {
225 case AF_INET:
226 case AF_INET6:
227 memcpy(&host.sa, hst_res->ai_addr, hst_res->ai_addrlen);
228 memcpy(&target.sa, tgt_res->ai_addr, tgt_res->ai_addrlen);
229 break;
230 default:
231 fprintf(stderr, "Invalid address type.\n");
232 exit(1);
233 break;
234 }
235
236 freeaddrinfo(hst_res);
237 freeaddrinfo(tgt_res);
238
239 sk = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP);
240
241 /* Enable ASSOC_CHANGE and SNDRCVINFO notifications. */
242 test_enable_assoc_change(sk);
243
244 test_setsockopt(sk, SCTP_NODELAY, &nodelay, sizeof(int));
245
246 /* Bind the sockets to the test port. */
247 test_bind(sk, &host.sa, sizeof(host));
248
249 /* Mark sk as being able to accept new associations. */
250 test_listen(sk, 1);
251
252 /* Build up a msghdr structure we can use for all sending. */
253 outmessage.msg_name = &target;
254 outmessage.msg_namelen = sizeof(target);
255 outmessage.msg_iov = &out_iov;
256 outmessage.msg_iovlen = 1;
257 outmessage.msg_control = outcmsg;
258 outmessage.msg_controllen = sizeof(outcmsg);
259 outmessage.msg_flags = 0;
260 cmsg = CMSG_FIRSTHDR(&outmessage);
261 cmsg->cmsg_level = IPPROTO_SCTP;
262 cmsg->cmsg_type = SCTP_SNDRCV;
263 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
264 outmessage.msg_controllen = cmsg->cmsg_len;
265 sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
266 memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
267 ppid = rand(); /* Choose an arbitrary value. */
268 stream = 1;
269 sinfo->sinfo_ppid = ppid;
270 sinfo->sinfo_stream = stream;
271
272 message = test_malloc((size + 1) * sizeof(u_int8_t));
273
274 for(i=0; i + 10 < size; i+= 10)
275 memcpy(message+i, "1234567890", 10);
276 strncpy(message+i, "1234567890", size-i);
277 *(message+size) = 0;
278
279 outmessage.msg_iov->iov_base = message;
280 outmessage.msg_iov->iov_len = size + 1;
281
282 printf("Initiating connection with %s:%d...\n", remote_host,
283 remote_port);
284
285 /* Send the first message. This will create the association. */
286 test_sendmsg(sk, &outmessage, 0, size+1);
287
288 memset(&inmessage, 0, sizeof(inmessage));
289 big_buffer = test_malloc(REALLY_BIG);
290 iov.iov_base = big_buffer;
291 iov.iov_len = REALLY_BIG;
292 inmessage.msg_iov = &iov;
293 inmessage.msg_iovlen = 1;
294 inmessage.msg_control = incmsg;
295 inmessage.msg_controllen = sizeof(incmsg);
296 inmessage.msg_name = &msgname;
297 inmessage.msg_namelen = sizeof(msgname);
298 memset(&msgname, 0, sizeof(msgname));
299
300 /* Get the communication up message on sk. */
301 error = test_recvmsg(sk, &inmessage, MSG_WAITALL);
302 test_check_msg_notification(&inmessage, error,
303 sizeof(struct sctp_assoc_change),
304 SCTP_ASSOC_CHANGE, SCTP_COMM_UP);
305 associd = ((struct sctp_assoc_change *)iov.iov_base)->sac_assoc_id;
306
307 printf("Established connection with ");
308 if (AF_INET == msgname.sa.sa_family)
309 printf("%d.%d.%d.%d(%d)\n", NIPQUAD(msgname.v4.sin_addr),
310 ntohs(msgname.v4.sin_port));
311 if (AF_INET6 == msgname.sa.sa_family)
312 printf("%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x(%d)\n",
313 NIP6(msgname.v6.sin6_addr), ntohs(msgname.v6.sin6_port));
314
315 printf("Sending data to receiver...\n");
316
317 for (i=1; i<1000000; i++) {
318
319 if (interval)
320 sleep(interval);
321
322 outmessage.msg_name = &target;
323 outmessage.msg_namelen = sizeof(target);
324 outmessage.msg_iov = &out_iov;
325 outmessage.msg_iovlen = 1;
326 outmessage.msg_controllen = sizeof(outcmsg);
327 outmessage.msg_flags = 0;
328
329 cmsg = CMSG_FIRSTHDR(&outmessage);
330 cmsg->cmsg_level = IPPROTO_SCTP;
331 cmsg->cmsg_type = SCTP_SNDRCV;
332 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
333 outmessage.msg_controllen = cmsg->cmsg_len;
334 sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
335 memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
336
337 ppid++;
338
339 sinfo->sinfo_ppid = ppid;
340 sinfo->sinfo_stream = stream;
341 sinfo->sinfo_assoc_id = associd;
342
343 outmessage.msg_iov->iov_base = message;
344 outmessage.msg_iov->iov_len = size + 1;
345
346 test_sendmsg(sk, &outmessage, 0, size+1);
347 }
348
349 printf("\n\n\t\tComplete all the data sendings to receiver...\n\n\n");
350
351 error = 0;
352 close(sk);
353
354 free(message);
355
356 /* Indicate successful completion. */
357 return 0;
358
359 }