| 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 | * goes into a receive loop waiting for 1,000,000 packets. Then it calculates |
| 43 | * the packet receive rate, i.e. packets/second. |
| 44 | */ |
| 45 | |
| 46 | #include <stdio.h> |
| 47 | #include <unistd.h> |
| 48 | #include <stdlib.h> |
| 49 | #include <string.h> |
| 50 | #include <sys/types.h> |
| 51 | #include <sys/socket.h> |
| 52 | #include <sys/uio.h> |
| 53 | #include <netinet/in.h> |
| 54 | #include <errno.h> |
| 55 | #include <netinet/sctp.h> |
| 56 | #include <sctputil.h> |
| 57 | #include <getopt.h> |
| 58 | #include <netdb.h> |
| 59 | #include <time.h> |
| 60 | |
| 61 | char *TCID = __FILE__; |
| 62 | int TST_TOTAL = 1; |
| 63 | int TST_CNT = 0; |
| 64 | |
| 65 | void |
| 66 | usage(char *progname) |
| 67 | { |
| 68 | fprintf(stderr, "Usage: %s -H hostname [-P port]\n", progname); |
| 69 | fprintf(stderr, " -H, --local\t\t local hostname,\n"); |
| 70 | fprintf(stderr, " -P, --local-port\t local port,\n"); |
| 71 | } |
| 72 | |
| 73 | int |
| 74 | main(int argc, char *argv[]) |
| 75 | { |
| 76 | int sk, i; |
| 77 | struct addrinfo *hst_res; |
| 78 | sockaddr_storage_t host; |
| 79 | sockaddr_storage_t msgname; |
| 80 | struct iovec iov; |
| 81 | struct msghdr inmessage; |
| 82 | char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))]; |
| 83 | int error, pf_class; |
| 84 | char *big_buffer; |
| 85 | char *local_host = NULL; |
| 86 | int local_port = SCTP_TESTPORT_1; |
| 87 | char port_buffer[10]; |
| 88 | int option_index = 0; |
| 89 | time_t from, to; |
| 90 | int bytes_received = 0; |
| 91 | int c; |
| 92 | static struct option long_options[] = { |
| 93 | {"local", 1, 0, 1}, |
| 94 | {"local-port", 1, 0, 2}, |
| 95 | {0, 0, 0, 0} |
| 96 | }; |
| 97 | |
| 98 | /* Rather than fflush() throughout the code, set stdout to |
| 99 | * be unbuffered. |
| 100 | */ |
| 101 | setvbuf(stdout, NULL, _IONBF, 0); |
| 102 | |
| 103 | /* Parse the arguments. */ |
| 104 | while (1) { |
| 105 | c = getopt_long (argc, argv, "H:P:", |
| 106 | long_options, &option_index); |
| 107 | if (c == -1) |
| 108 | break; |
| 109 | |
| 110 | switch (c) { |
| 111 | case 0: |
| 112 | printf("option %s", long_options[option_index].name); |
| 113 | if (optarg) { |
| 114 | printf(" with arg %s", optarg); |
| 115 | } |
| 116 | printf("\n"); |
| 117 | break; |
| 118 | case 1: /* local host */ |
| 119 | case 'H': |
| 120 | local_host = optarg; |
| 121 | break; |
| 122 | case 2: /* local port */ |
| 123 | case 'P': |
| 124 | local_port = atoi(optarg); |
| 125 | break; |
| 126 | case '?': |
| 127 | usage(argv[0]); |
| 128 | exit(0); |
| 129 | |
| 130 | default: |
| 131 | printf ("%s: unrecognized option 0%c\n", argv[0], c); |
| 132 | usage(argv[0]); |
| 133 | exit(1); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | if (optind < argc) |
| 138 | { |
| 139 | fprintf(stderr, "%s: non-option arguments are illegal: ", |
| 140 | argv[0]); |
| 141 | while (optind < argc) |
| 142 | fprintf(stderr, "%s ", argv[optind++]); |
| 143 | fprintf (stderr, "\n"); |
| 144 | usage(argv[0]); |
| 145 | exit(1); |
| 146 | } |
| 147 | |
| 148 | if (!local_host) { |
| 149 | fprintf(stderr, "%s: : option -H, --local is required\n", |
| 150 | argv[0]); |
| 151 | usage(argv[0]); |
| 152 | exit(1); |
| 153 | } |
| 154 | |
| 155 | /* Set some basic values which depend on the address family. */ |
| 156 | if (!strcmp(local_host, "0")) |
| 157 | local_host = "0.0.0.0"; |
| 158 | |
| 159 | snprintf(port_buffer, 10, "%d", local_port); |
| 160 | error = getaddrinfo(local_host, port_buffer, NULL, &hst_res); |
| 161 | if (error) { |
| 162 | fprintf(stderr, "%s: getaddrinfo failed: %s\n", argv[0], local_host); |
| 163 | exit(1); |
| 164 | } |
| 165 | |
| 166 | pf_class = hst_res->ai_family; |
| 167 | switch (pf_class) { |
| 168 | case AF_INET: |
| 169 | case AF_INET6: |
| 170 | memcpy(&host.sa, hst_res->ai_addr, hst_res->ai_addrlen); |
| 171 | break; |
| 172 | default: |
| 173 | fprintf(stderr, "Invalid address type.\n"); |
| 174 | exit(1); |
| 175 | break; |
| 176 | } |
| 177 | |
| 178 | freeaddrinfo(hst_res); |
| 179 | |
| 180 | /* Create the endpoint which will talk to nagle_snd. */ |
| 181 | sk = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP); |
| 182 | |
| 183 | /* Enable ASSOC_CHANGE and SNDRCVINFO notifications. */ |
| 184 | test_enable_assoc_change(sk); |
| 185 | |
| 186 | /* Bind the sockets to the test port. */ |
| 187 | test_bind(sk, &host.sa, sizeof(host)); |
| 188 | |
| 189 | /* Mark sk as being able to accept new associations. */ |
| 190 | test_listen(sk, 1); |
| 191 | |
| 192 | printf("Listening on port:%d\n", local_port); |
| 193 | |
| 194 | /* Initialize inmessage for receives. */ |
| 195 | memset(&inmessage, 0, sizeof(inmessage)); |
| 196 | big_buffer = test_malloc(REALLY_BIG); |
| 197 | iov.iov_base = big_buffer; |
| 198 | iov.iov_len = REALLY_BIG; |
| 199 | inmessage.msg_iov = &iov; |
| 200 | inmessage.msg_iovlen = 1; |
| 201 | inmessage.msg_control = incmsg; |
| 202 | inmessage.msg_controllen = sizeof(incmsg); |
| 203 | inmessage.msg_name = &msgname; |
| 204 | inmessage.msg_namelen = sizeof(msgname); |
| 205 | memset(&msgname, 0, sizeof(msgname)); |
| 206 | |
| 207 | /* Get the communication up message on sk. */ |
| 208 | error = test_recvmsg(sk, &inmessage, MSG_WAITALL); |
| 209 | test_check_msg_notification(&inmessage, error, |
| 210 | sizeof(struct sctp_assoc_change), |
| 211 | SCTP_ASSOC_CHANGE, SCTP_COMM_UP); |
| 212 | |
| 213 | printf("Established connection with "); |
| 214 | if (AF_INET == msgname.sa.sa_family) |
| 215 | printf("%d.%d.%d.%d(%d)\n", NIPQUAD(msgname.v4.sin_addr), |
| 216 | ntohs(msgname.v4.sin_port)); |
| 217 | if (AF_INET6 == msgname.sa.sa_family) |
| 218 | printf("%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x(%d)\n", |
| 219 | NIP6(msgname.v6.sin6_addr), ntohs(msgname.v6.sin6_port)); |
| 220 | |
| 221 | time(&from); |
| 222 | for (i=0; i<1000000; i++) { |
| 223 | inmessage.msg_controllen = sizeof(incmsg); |
| 224 | inmessage.msg_namelen = sizeof(msgname); |
| 225 | error = test_recvmsg(sk, &inmessage, MSG_WAITALL); |
| 226 | if (inmessage.msg_flags & MSG_NOTIFICATION) |
| 227 | break; |
| 228 | printf("Received %d bytes of data\n", error); |
| 229 | bytes_received += error; |
| 230 | } |
| 231 | time(&to); |
| 232 | |
| 233 | printf("\t%d messages(%d bytes) successfully received in %ld " |
| 234 | "seconds.\n", i, bytes_received, to - from); |
| 235 | printf("The receive rate is %ld bytes/second\n", |
| 236 | bytes_received/(to - from)); |
| 237 | |
| 238 | /* Shut down the link. */ |
| 239 | error = 0; |
| 240 | close(sk); |
| 241 | |
| 242 | return 0; |
| 243 | } |