1 /* 2 * iperf, Copyright (c) 2014-2018, The Regents of the University of 3 * California, through Lawrence Berkeley National Laboratory (subject 4 * to receipt of any required approvals from the U.S. Dept. of 5 * Energy). All rights reserved. 6 * 7 * If you have questions about your rights to use or distribute this 8 * software, please contact Berkeley Lab's Technology Transfer 9 * Department at [email protected]. 10 * 11 * NOTICE. This software is owned by the U.S. Department of Energy. 12 * As such, the U.S. Government has been granted for itself and others 13 * acting on its behalf a paid-up, nonexclusive, irrevocable, 14 * worldwide license in the Software to reproduce, prepare derivative 15 * works, and perform publicly and display publicly. Beginning five 16 * (5) years after the date permission to assert copyright is obtained 17 * from the U.S. Department of Energy, and subject to any subsequent 18 * five (5) year renewals, the U.S. Government is granted for itself 19 * and others acting on its behalf a paid-up, nonexclusive, 20 * irrevocable, worldwide license in the Software to reproduce, 21 * prepare derivative works, distribute copies to the public, perform 22 * publicly and display publicly, and to permit others to do so. 23 * 24 * This code is distributed under a BSD style license, see the LICENSE 25 * file for complete information. 26 */ 27 #include <stdio.h> 28 #include <stdlib.h> 29 #include <string.h> 30 #include <errno.h> 31 #include <unistd.h> 32 #include <assert.h> 33 #include <sys/socket.h> 34 #include <sys/types.h> 35 #include <netinet/in.h> 36 #ifdef HAVE_STDINT_H 37 #include <stdint.h> 38 #endif 39 #include <sys/time.h> 40 #include <sys/select.h> 41 42 #include "iperf.h" 43 #include "iperf_api.h" 44 #include "iperf_util.h" 45 #include "iperf_udp.h" 46 #include "timer.h" 47 #include "net.h" 48 #include "cjson.h" 49 #include "portable_endian.h" 50 51 #if defined(HAVE_INTTYPES_H) 52 # include <inttypes.h> 53 #else 54 # define PRIu64 "llu" 55 #endif 56 57 /* iperf_udp_recv 58 * 59 * receives the data for UDP 60 */ 61 int 62 iperf_udp_recv(struct iperf_stream *sp) 63 { 64 uint32_t sec, usec; 65 uint64_t pcount; 66 int r; 67 int size = sp->settings->blksize; 68 double transit = 0, d = 0; 69 struct timeval sent_time, arrival_time; 70 71 r = Nread(sp->socket, sp->buffer, size, Pudp); 72 73 /* 74 * If we got an error in the read, or if we didn't read anything 75 * because the underlying read(2) got a EAGAIN, then skip packet 76 * processing. 77 */ 78 if (r <= 0) 79 return r; 80 81 /* Only count bytes received while we're in the correct state. */ 82 if (sp->test->state == TEST_RUNNING) { 83 sp->result->bytes_received += r; 84 sp->result->bytes_received_this_interval += r; 85 86 if (sp->test->udp_counters_64bit) { 87 memcpy(&sec, sp->buffer, sizeof(sec)); 88 memcpy(&usec, sp->buffer+4, sizeof(usec)); 89 memcpy(&pcount, sp->buffer+8, sizeof(pcount)); 90 sec = ntohl(sec); 91 usec = ntohl(usec); 92 pcount = be64toh(pcount); 93 sent_time.tv_sec = sec; 94 sent_time.tv_usec = usec; 95 } 96 else { 97 uint32_t pc; 98 memcpy(&sec, sp->buffer, sizeof(sec)); 99 memcpy(&usec, sp->buffer+4, sizeof(usec)); 100 memcpy(&pc, sp->buffer+8, sizeof(pc)); 101 sec = ntohl(sec); 102 usec = ntohl(usec); 103 pcount = ntohl(pc); 104 sent_time.tv_sec = sec; 105 sent_time.tv_usec = usec; 106 } 107 108 if (sp->test->debug) 109 fprintf(stderr, "pcount %" PRIu64 " packet_count %d\n", pcount, sp->packet_count); 110 111 /* 112 * Try to handle out of order packets. The way we do this 113 * uses a constant amount of storage but might not be 114 * correct in all cases. In particular we seem to have the 115 * assumption that packets can't be duplicated in the network, 116 * because duplicate packets will possibly cause some problems here. 117 * 118 * First figure out if the sequence numbers are going forward. 119 * Note that pcount is the sequence number read from the packet, 120 * and sp->packet_count is the highest sequence number seen so 121 * far (so we're expecting to see the packet with sequence number 122 * sp->packet_count + 1 arrive next). 123 */ 124 if (pcount >= sp->packet_count + 1) { 125 126 /* Forward, but is there a gap in sequence numbers? */ 127 if (pcount > sp->packet_count + 1) { 128 /* There's a gap so count that as a loss. */ 129 sp->cnt_error += (pcount - 1) - sp->packet_count; 130 } 131 /* Update the highest sequence number seen so far. */ 132 sp->packet_count = pcount; 133 } else { 134 135 /* 136 * Sequence number went backward (or was stationary?!?). 137 * This counts as an out-of-order packet. 138 */ 139 sp->outoforder_packets++; 140 141 /* 142 * If we have lost packets, then the fact that we are now 143 * seeing an out-of-order packet offsets a prior sequence 144 * number gap that was counted as a loss. So we can take 145 * away a loss. 146 */ 147 if (sp->cnt_error > 0) 148 sp->cnt_error--; 149 150 /* Log the out-of-order packet */ 151 if (sp->test->debug) 152 fprintf(stderr, "OUT OF ORDER - incoming packet sequence %" PRIu64 " but expected sequence %d on stream %d", pcount, sp->packet_count, sp->socket); 153 } 154 155 /* 156 * jitter measurement 157 * 158 * This computation is based on RFC 1889 (specifically 159 * sections 6.3.1 and A.8). 160 * 161 * Note that synchronized clocks are not required since 162 * the source packet delta times are known. Also this 163 * computation does not require knowing the round-trip 164 * time. 165 */ 166 gettimeofday(&arrival_time, NULL); 167 168 transit = timeval_diff(&sent_time, &arrival_time); 169 d = transit - sp->prev_transit; 170 if (d < 0) 171 d = -d; 172 sp->prev_transit = transit; 173 sp->jitter += (d - sp->jitter) / 16.0; 174 } 175 else { 176 if (sp->test->debug) 177 printf("Late receive, state = %d\n", sp->test->state); 178 } 179 180 return r; 181 } 182 183 184 /* iperf_udp_send 185 * 186 * sends the data for UDP 187 */ 188 int 189 iperf_udp_send(struct iperf_stream *sp) 190 { 191 int r; 192 int size = sp->settings->blksize; 193 struct timeval before; 194 195 gettimeofday(&before, 0); 196 197 ++sp->packet_count; 198 199 if (sp->test->udp_counters_64bit) { 200 201 uint32_t sec, usec; 202 uint64_t pcount; 203 204 sec = htonl(before.tv_sec); 205 usec = htonl(before.tv_usec); 206 pcount = htobe64(sp->packet_count); 207 208 memcpy(sp->buffer, &sec, sizeof(sec)); 209 memcpy(sp->buffer+4, &usec, sizeof(usec)); 210 memcpy(sp->buffer+8, &pcount, sizeof(pcount)); 211 212 } 213 else { 214 215 uint32_t sec, usec, pcount; 216 217 sec = htonl(before.tv_sec); 218 usec = htonl(before.tv_usec); 219 pcount = htonl(sp->packet_count); 220 221 memcpy(sp->buffer, &sec, sizeof(sec)); 222 memcpy(sp->buffer+4, &usec, sizeof(usec)); 223 memcpy(sp->buffer+8, &pcount, sizeof(pcount)); 224 225 } 226 227 r = Nwrite(sp->socket, sp->buffer, size, Pudp); 228 229 if (r < 0) 230 return r; 231 232 sp->result->bytes_sent += r; 233 sp->result->bytes_sent_this_interval += r; 234 235 if (sp->test->debug) 236 printf("sent %d bytes of %d, total %" PRIu64 "\n", r, sp->settings->blksize, sp->result->bytes_sent); 237 238 return r; 239 } 240 241 242 /**************************************************************************/ 243 244 /* 245 * The following functions all have to do with managing UDP data sockets. 246 * UDP of course is connectionless, so there isn't really a concept of 247 * setting up a connection, although connect(2) can (and is) used to 248 * bind the remote end of sockets. We need to simulate some of the 249 * connection management that is built-in to TCP so that each side of the 250 * connection knows about each other before the real data transfers begin. 251 */ 252 253 /* 254 * Set and verify socket buffer sizes. 255 * Return 0 if no error, -1 if an error, +1 if socket buffers are 256 * potentially too small to hold a message. 257 */ 258 int 259 iperf_udp_buffercheck(struct iperf_test *test, int s) 260 { 261 int rc = 0; 262 int sndbuf_actual, rcvbuf_actual; 263 264 /* 265 * Set socket buffer size if requested. Do this for both sending and 266 * receiving so that we can cover both normal and --reverse operation. 267 */ 268 int opt; 269 socklen_t optlen; 270 271 if ((opt = test->settings->socket_bufsize)) { 272 if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt)) < 0) { 273 i_errno = IESETBUF; 274 return -1; 275 } 276 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &opt, sizeof(opt)) < 0) { 277 i_errno = IESETBUF; 278 return -1; 279 } 280 } 281 282 /* Read back and verify the sender socket buffer size */ 283 optlen = sizeof(sndbuf_actual); 284 if (getsockopt(s, SOL_SOCKET, SO_SNDBUF, &sndbuf_actual, &optlen) < 0) { 285 i_errno = IESETBUF; 286 return -1; 287 } 288 if (test->debug) { 289 printf("SNDBUF is %u, expecting %u\n", sndbuf_actual, test->settings->socket_bufsize); 290 } 291 if (test->settings->socket_bufsize && test->settings->socket_bufsize > sndbuf_actual) { 292 i_errno = IESETBUF2; 293 return -1; 294 } 295 if (test->settings->blksize > sndbuf_actual) { 296 char str[80]; 297 snprintf(str, sizeof(str), 298 "Block size %d > sending socket buffer size %d", 299 test->settings->blksize, sndbuf_actual); 300 warning(str); 301 rc = 1; 302 } 303 304 /* Read back and verify the receiver socket buffer size */ 305 optlen = sizeof(rcvbuf_actual); 306 if (getsockopt(s, SOL_SOCKET, SO_RCVBUF, &rcvbuf_actual, &optlen) < 0) { 307 i_errno = IESETBUF; 308 return -1; 309 } 310 if (test->debug) { 311 printf("RCVBUF is %u, expecting %u\n", rcvbuf_actual, test->settings->socket_bufsize); 312 } 313 if (test->settings->socket_bufsize && test->settings->socket_bufsize > rcvbuf_actual) { 314 i_errno = IESETBUF2; 315 return -1; 316 } 317 if (test->settings->blksize > rcvbuf_actual) { 318 char str[80]; 319 snprintf(str, sizeof(str), 320 "Block size %d > receiving socket buffer size %d", 321 test->settings->blksize, rcvbuf_actual); 322 warning(str); 323 rc = 1; 324 } 325 326 if (test->json_output) { 327 cJSON_AddNumberToObject(test->json_start, "sock_bufsize", test->settings->socket_bufsize); 328 cJSON_AddNumberToObject(test->json_start, "sndbuf_actual", sndbuf_actual); 329 cJSON_AddNumberToObject(test->json_start, "rcvbuf_actual", rcvbuf_actual); 330 } 331 332 return rc; 333 } 334 335 /* 336 * iperf_udp_accept 337 * 338 * Accepts a new UDP "connection" 339 */ 340 int 341 iperf_udp_accept(struct iperf_test *test) 342 { 343 struct sockaddr_storage sa_peer; 344 int buf; 345 socklen_t len; 346 int sz, s; 347 int rc; 348 349 /* 350 * Get the current outstanding socket. This socket will be used to handle 351 * data transfers and a new "listening" socket will be created. 352 */ 353 s = test->prot_listener; 354 355 /* 356 * Grab the UDP packet sent by the client. From that we can extract the 357 * client's address, and then use that information to bind the remote side 358 * of the socket to the client. 359 */ 360 len = sizeof(sa_peer); 361 if ((sz = recvfrom(test->prot_listener, &buf, sizeof(buf), 0, (struct sockaddr *) &sa_peer, &len)) < 0) { 362 i_errno = IESTREAMACCEPT; 363 return -1; 364 } 365 366 if (connect(s, (struct sockaddr *) &sa_peer, len) < 0) { 367 i_errno = IESTREAMACCEPT; 368 return -1; 369 } 370 371 /* Check and set socket buffer sizes */ 372 rc = iperf_udp_buffercheck(test, s); 373 if (rc < 0) 374 /* error */ 375 return rc; 376 /* 377 * If the socket buffer was too small, but it was the default 378 * size, then try explicitly setting it to something larger. 379 */ 380 if (rc > 0) { 381 if (test->settings->socket_bufsize == 0) { 382 int bufsize = test->settings->blksize + UDP_BUFFER_EXTRA; 383 printf("Increasing socket buffer size to %d\n", 384 bufsize); 385 test->settings->socket_bufsize = bufsize; 386 rc = iperf_udp_buffercheck(test, s); 387 if (rc < 0) 388 return rc; 389 } 390 } 391 392 #if defined(HAVE_SO_MAX_PACING_RATE) 393 /* If socket pacing is specified, try it. */ 394 if (test->settings->fqrate) { 395 /* Convert bits per second to bytes per second */ 396 unsigned int fqrate = test->settings->fqrate / 8; 397 if (fqrate > 0) { 398 if (test->debug) { 399 printf("Setting fair-queue socket pacing to %u\n", fqrate); 400 } 401 if (setsockopt(s, SOL_SOCKET, SO_MAX_PACING_RATE, &fqrate, sizeof(fqrate)) < 0) { 402 warning("Unable to set socket pacing"); 403 } 404 } 405 } 406 #endif /* HAVE_SO_MAX_PACING_RATE */ 407 { 408 unsigned int rate = test->settings->rate / 8; 409 if (rate > 0) { 410 if (test->debug) { 411 printf("Setting application pacing to %u\n", rate); 412 } 413 } 414 } 415 416 /* 417 * Create a new "listening" socket to replace the one we were using before. 418 */ 419 test->prot_listener = netannounce(test->settings->domain, Pudp, test->bind_address, test->server_port); 420 if (test->prot_listener < 0) { 421 i_errno = IESTREAMLISTEN; 422 return -1; 423 } 424 425 FD_SET(test->prot_listener, &test->read_set); 426 test->max_fd = (test->max_fd < test->prot_listener) ? test->prot_listener : test->max_fd; 427 428 /* Let the client know we're ready "accept" another UDP "stream" */ 429 buf = 987654321; /* any content will work here */ 430 if (write(s, &buf, sizeof(buf)) < 0) { 431 i_errno = IESTREAMWRITE; 432 return -1; 433 } 434 435 return s; 436 } 437 438 439 /* 440 * iperf_udp_listen 441 * 442 * Start up a listener for UDP stream connections. Unlike for TCP, 443 * there is no listen(2) for UDP. This socket will however accept 444 * a UDP datagram from a client (indicating the client's presence). 445 */ 446 int 447 iperf_udp_listen(struct iperf_test *test) 448 { 449 int s; 450 451 if ((s = netannounce(test->settings->domain, Pudp, test->bind_address, test->server_port)) < 0) { 452 i_errno = IESTREAMLISTEN; 453 return -1; 454 } 455 456 /* 457 * The caller will put this value into test->prot_listener. 458 */ 459 return s; 460 } 461 462 463 /* 464 * iperf_udp_connect 465 * 466 * "Connect" to a UDP stream listener. 467 */ 468 int 469 iperf_udp_connect(struct iperf_test *test) 470 { 471 int s, buf, sz; 472 #ifdef SO_RCVTIMEO 473 struct timeval tv; 474 #endif 475 int rc; 476 477 /* Create and bind our local socket. */ 478 if ((s = netdial(test->settings->domain, Pudp, test->bind_address, test->bind_port, test->server_hostname, test->server_port, -1)) < 0) { 479 i_errno = IESTREAMCONNECT; 480 return -1; 481 } 482 483 /* Check and set socket buffer sizes */ 484 rc = iperf_udp_buffercheck(test, s); 485 if (rc < 0) 486 /* error */ 487 return rc; 488 /* 489 * If the socket buffer was too small, but it was the default 490 * size, then try explicitly setting it to something larger. 491 */ 492 if (rc > 0) { 493 if (test->settings->socket_bufsize == 0) { 494 int bufsize = test->settings->blksize + UDP_BUFFER_EXTRA; 495 printf("Increasing socket buffer size to %d\n", 496 bufsize); 497 test->settings->socket_bufsize = bufsize; 498 rc = iperf_udp_buffercheck(test, s); 499 if (rc < 0) 500 return rc; 501 } 502 } 503 504 #if defined(HAVE_SO_MAX_PACING_RATE) 505 /* If socket pacing is available and not disabled, try it. */ 506 if (test->settings->fqrate) { 507 /* Convert bits per second to bytes per second */ 508 unsigned int fqrate = test->settings->fqrate / 8; 509 if (fqrate > 0) { 510 if (test->debug) { 511 printf("Setting fair-queue socket pacing to %u\n", fqrate); 512 } 513 if (setsockopt(s, SOL_SOCKET, SO_MAX_PACING_RATE, &fqrate, sizeof(fqrate)) < 0) { 514 warning("Unable to set socket pacing"); 515 } 516 } 517 } 518 #endif /* HAVE_SO_MAX_PACING_RATE */ 519 { 520 unsigned int rate = test->settings->rate / 8; 521 if (rate > 0) { 522 if (test->debug) { 523 printf("Setting application pacing to %u\n", rate); 524 } 525 } 526 } 527 528 #ifdef SO_RCVTIMEO 529 /* 30 sec timeout for a case when there is a network problem. */ 530 tv.tv_sec = 30; 531 tv.tv_usec = 0; 532 setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (struct timeval *)&tv, sizeof(struct timeval)); 533 #endif 534 535 /* 536 * Write a datagram to the UDP stream to let the server know we're here. 537 * The server learns our address by obtaining its peer's address. 538 */ 539 buf = 123456789; /* this can be pretty much anything */ 540 if (write(s, &buf, sizeof(buf)) < 0) { 541 // XXX: Should this be changed to IESTREAMCONNECT? 542 i_errno = IESTREAMWRITE; 543 return -1; 544 } 545 546 /* 547 * Wait until the server replies back to us. 548 */ 549 if ((sz = recv(s, &buf, sizeof(buf), 0)) < 0) { 550 i_errno = IESTREAMREAD; 551 return -1; 552 } 553 554 return s; 555 } 556 557 558 /* iperf_udp_init 559 * 560 * initializer for UDP streams in TEST_START 561 */ 562 int 563 iperf_udp_init(struct iperf_test *test) 564 { 565 return 0; 566 } 567