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 TTD@lbl.gov. 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 iperf_time sent_time, arrival_time, temp_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.secs = sec; 94 sent_time.usecs = 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.secs = sec; 105 sent_time.usecs = 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 iperf_time_now(&arrival_time); 167 168 iperf_time_diff(&arrival_time, &sent_time, &temp_time); 169 transit = iperf_time_in_secs(&temp_time); 170 d = transit - sp->prev_transit; 171 if (d < 0) 172 d = -d; 173 sp->prev_transit = transit; 174 sp->jitter += (d - sp->jitter) / 16.0; 175 } 176 else { 177 if (sp->test->debug) 178 printf("Late receive, state = %d\n", sp->test->state); 179 } 180 181 return r; 182 } 183 184 185 /* iperf_udp_send 186 * 187 * sends the data for UDP 188 */ 189 int 190 iperf_udp_send(struct iperf_stream *sp) 191 { 192 int r; 193 int size = sp->settings->blksize; 194 struct iperf_time before; 195 196 iperf_time_now(&before); 197 198 ++sp->packet_count; 199 200 if (sp->test->udp_counters_64bit) { 201 202 uint32_t sec, usec; 203 uint64_t pcount; 204 205 sec = htonl(before.secs); 206 usec = htonl(before.usecs); 207 pcount = htobe64(sp->packet_count); 208 209 memcpy(sp->buffer, &sec, sizeof(sec)); 210 memcpy(sp->buffer+4, &usec, sizeof(usec)); 211 memcpy(sp->buffer+8, &pcount, sizeof(pcount)); 212 213 } 214 else { 215 216 uint32_t sec, usec, pcount; 217 218 sec = htonl(before.secs); 219 usec = htonl(before.usecs); 220 pcount = htonl(sp->packet_count); 221 222 memcpy(sp->buffer, &sec, sizeof(sec)); 223 memcpy(sp->buffer+4, &usec, sizeof(usec)); 224 memcpy(sp->buffer+8, &pcount, sizeof(pcount)); 225 226 } 227 228 r = Nwrite(sp->socket, sp->buffer, size, Pudp); 229 230 if (r < 0) 231 return r; 232 233 sp->result->bytes_sent += r; 234 sp->result->bytes_sent_this_interval += r; 235 236 if (sp->test->debug) 237 printf("sent %d bytes of %d, total %" PRIu64 "\n", r, sp->settings->blksize, sp->result->bytes_sent); 238 239 return r; 240 } 241 242 243 /**************************************************************************/ 244 245 /* 246 * The following functions all have to do with managing UDP data sockets. 247 * UDP of course is connectionless, so there isn't really a concept of 248 * setting up a connection, although connect(2) can (and is) used to 249 * bind the remote end of sockets. We need to simulate some of the 250 * connection management that is built-in to TCP so that each side of the 251 * connection knows about each other before the real data transfers begin. 252 */ 253 254 /* 255 * Set and verify socket buffer sizes. 256 * Return 0 if no error, -1 if an error, +1 if socket buffers are 257 * potentially too small to hold a message. 258 */ 259 int 260 iperf_udp_buffercheck(struct iperf_test *test, int s) 261 { 262 int rc = 0; 263 int sndbuf_actual, rcvbuf_actual; 264 265 /* 266 * Set socket buffer size if requested. Do this for both sending and 267 * receiving so that we can cover both normal and --reverse operation. 268 */ 269 int opt; 270 socklen_t optlen; 271 272 if ((opt = test->settings->socket_bufsize)) { 273 if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt)) < 0) { 274 i_errno = IESETBUF; 275 return -1; 276 } 277 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &opt, sizeof(opt)) < 0) { 278 i_errno = IESETBUF; 279 return -1; 280 } 281 } 282 283 /* Read back and verify the sender socket buffer size */ 284 optlen = sizeof(sndbuf_actual); 285 if (getsockopt(s, SOL_SOCKET, SO_SNDBUF, &sndbuf_actual, &optlen) < 0) { 286 i_errno = IESETBUF; 287 return -1; 288 } 289 if (test->debug) { 290 printf("SNDBUF is %u, expecting %u\n", sndbuf_actual, test->settings->socket_bufsize); 291 } 292 if (test->settings->socket_bufsize && test->settings->socket_bufsize > sndbuf_actual) { 293 i_errno = IESETBUF2; 294 return -1; 295 } 296 if (test->settings->blksize > sndbuf_actual) { 297 char str[80]; 298 snprintf(str, sizeof(str), 299 "Block size %d > sending socket buffer size %d", 300 test->settings->blksize, sndbuf_actual); 301 warning(str); 302 rc = 1; 303 } 304 305 /* Read back and verify the receiver socket buffer size */ 306 optlen = sizeof(rcvbuf_actual); 307 if (getsockopt(s, SOL_SOCKET, SO_RCVBUF, &rcvbuf_actual, &optlen) < 0) { 308 i_errno = IESETBUF; 309 return -1; 310 } 311 if (test->debug) { 312 printf("RCVBUF is %u, expecting %u\n", rcvbuf_actual, test->settings->socket_bufsize); 313 } 314 if (test->settings->socket_bufsize && test->settings->socket_bufsize > rcvbuf_actual) { 315 i_errno = IESETBUF2; 316 return -1; 317 } 318 if (test->settings->blksize > rcvbuf_actual) { 319 char str[80]; 320 snprintf(str, sizeof(str), 321 "Block size %d > receiving socket buffer size %d", 322 test->settings->blksize, rcvbuf_actual); 323 warning(str); 324 rc = 1; 325 } 326 327 if (test->json_output) { 328 cJSON_AddNumberToObject(test->json_start, "sock_bufsize", test->settings->socket_bufsize); 329 cJSON_AddNumberToObject(test->json_start, "sndbuf_actual", sndbuf_actual); 330 cJSON_AddNumberToObject(test->json_start, "rcvbuf_actual", rcvbuf_actual); 331 } 332 333 return rc; 334 } 335 336 /* 337 * iperf_udp_accept 338 * 339 * Accepts a new UDP "connection" 340 */ 341 int 342 iperf_udp_accept(struct iperf_test *test) 343 { 344 struct sockaddr_storage sa_peer; 345 int buf; 346 socklen_t len; 347 int sz, s; 348 int rc; 349 350 /* 351 * Get the current outstanding socket. This socket will be used to handle 352 * data transfers and a new "listening" socket will be created. 353 */ 354 s = test->prot_listener; 355 356 /* 357 * Grab the UDP packet sent by the client. From that we can extract the 358 * client's address, and then use that information to bind the remote side 359 * of the socket to the client. 360 */ 361 len = sizeof(sa_peer); 362 if ((sz = recvfrom(test->prot_listener, &buf, sizeof(buf), 0, (struct sockaddr *) &sa_peer, &len)) < 0) { 363 i_errno = IESTREAMACCEPT; 364 return -1; 365 } 366 367 if (connect(s, (struct sockaddr *) &sa_peer, len) < 0) { 368 i_errno = IESTREAMACCEPT; 369 return -1; 370 } 371 372 /* Check and set socket buffer sizes */ 373 rc = iperf_udp_buffercheck(test, s); 374 if (rc < 0) 375 /* error */ 376 return rc; 377 /* 378 * If the socket buffer was too small, but it was the default 379 * size, then try explicitly setting it to something larger. 380 */ 381 if (rc > 0) { 382 if (test->settings->socket_bufsize == 0) { 383 int bufsize = test->settings->blksize + UDP_BUFFER_EXTRA; 384 printf("Increasing socket buffer size to %d\n", 385 bufsize); 386 test->settings->socket_bufsize = bufsize; 387 rc = iperf_udp_buffercheck(test, s); 388 if (rc < 0) 389 return rc; 390 } 391 } 392 393 #if defined(HAVE_SO_MAX_PACING_RATE) 394 /* If socket pacing is specified, try it. */ 395 if (test->settings->fqrate) { 396 /* Convert bits per second to bytes per second */ 397 unsigned int fqrate = test->settings->fqrate / 8; 398 if (fqrate > 0) { 399 if (test->debug) { 400 printf("Setting fair-queue socket pacing to %u\n", fqrate); 401 } 402 if (setsockopt(s, SOL_SOCKET, SO_MAX_PACING_RATE, &fqrate, sizeof(fqrate)) < 0) { 403 warning("Unable to set socket pacing"); 404 } 405 } 406 } 407 #endif /* HAVE_SO_MAX_PACING_RATE */ 408 { 409 unsigned int rate = test->settings->rate / 8; 410 if (rate > 0) { 411 if (test->debug) { 412 printf("Setting application pacing to %u\n", rate); 413 } 414 } 415 } 416 417 /* 418 * Create a new "listening" socket to replace the one we were using before. 419 */ 420 test->prot_listener = netannounce(test->settings->domain, Pudp, test->bind_address, test->server_port); 421 if (test->prot_listener < 0) { 422 i_errno = IESTREAMLISTEN; 423 return -1; 424 } 425 426 FD_SET(test->prot_listener, &test->read_set); 427 test->max_fd = (test->max_fd < test->prot_listener) ? test->prot_listener : test->max_fd; 428 429 /* Let the client know we're ready "accept" another UDP "stream" */ 430 buf = 987654321; /* any content will work here */ 431 if (write(s, &buf, sizeof(buf)) < 0) { 432 i_errno = IESTREAMWRITE; 433 return -1; 434 } 435 436 return s; 437 } 438 439 440 /* 441 * iperf_udp_listen 442 * 443 * Start up a listener for UDP stream connections. Unlike for TCP, 444 * there is no listen(2) for UDP. This socket will however accept 445 * a UDP datagram from a client (indicating the client's presence). 446 */ 447 int 448 iperf_udp_listen(struct iperf_test *test) 449 { 450 int s; 451 452 if ((s = netannounce(test->settings->domain, Pudp, test->bind_address, test->server_port)) < 0) { 453 i_errno = IESTREAMLISTEN; 454 return -1; 455 } 456 457 /* 458 * The caller will put this value into test->prot_listener. 459 */ 460 return s; 461 } 462 463 464 /* 465 * iperf_udp_connect 466 * 467 * "Connect" to a UDP stream listener. 468 */ 469 int 470 iperf_udp_connect(struct iperf_test *test) 471 { 472 int s, buf, sz; 473 #ifdef SO_RCVTIMEO 474 struct timeval tv; 475 #endif 476 int rc; 477 478 /* Create and bind our local socket. */ 479 if ((s = netdial(test->settings->domain, Pudp, test->bind_address, test->bind_port, test->server_hostname, test->server_port, -1)) < 0) { 480 i_errno = IESTREAMCONNECT; 481 return -1; 482 } 483 484 /* Check and set socket buffer sizes */ 485 rc = iperf_udp_buffercheck(test, s); 486 if (rc < 0) 487 /* error */ 488 return rc; 489 /* 490 * If the socket buffer was too small, but it was the default 491 * size, then try explicitly setting it to something larger. 492 */ 493 if (rc > 0) { 494 if (test->settings->socket_bufsize == 0) { 495 int bufsize = test->settings->blksize + UDP_BUFFER_EXTRA; 496 printf("Increasing socket buffer size to %d\n", 497 bufsize); 498 test->settings->socket_bufsize = bufsize; 499 rc = iperf_udp_buffercheck(test, s); 500 if (rc < 0) 501 return rc; 502 } 503 } 504 505 #if defined(HAVE_SO_MAX_PACING_RATE) 506 /* If socket pacing is available and not disabled, try it. */ 507 if (test->settings->fqrate) { 508 /* Convert bits per second to bytes per second */ 509 unsigned int fqrate = test->settings->fqrate / 8; 510 if (fqrate > 0) { 511 if (test->debug) { 512 printf("Setting fair-queue socket pacing to %u\n", fqrate); 513 } 514 if (setsockopt(s, SOL_SOCKET, SO_MAX_PACING_RATE, &fqrate, sizeof(fqrate)) < 0) { 515 warning("Unable to set socket pacing"); 516 } 517 } 518 } 519 #endif /* HAVE_SO_MAX_PACING_RATE */ 520 { 521 unsigned int rate = test->settings->rate / 8; 522 if (rate > 0) { 523 if (test->debug) { 524 printf("Setting application pacing to %u\n", rate); 525 } 526 } 527 } 528 529 #ifdef SO_RCVTIMEO 530 /* 30 sec timeout for a case when there is a network problem. */ 531 tv.tv_sec = 30; 532 tv.tv_usec = 0; 533 setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (struct timeval *)&tv, sizeof(struct timeval)); 534 #endif 535 536 /* 537 * Write a datagram to the UDP stream to let the server know we're here. 538 * The server learns our address by obtaining its peer's address. 539 */ 540 buf = 123456789; /* this can be pretty much anything */ 541 if (write(s, &buf, sizeof(buf)) < 0) { 542 // XXX: Should this be changed to IESTREAMCONNECT? 543 i_errno = IESTREAMWRITE; 544 return -1; 545 } 546 547 /* 548 * Wait until the server replies back to us. 549 */ 550 if ((sz = recv(s, &buf, sizeof(buf), 0)) < 0) { 551 i_errno = IESTREAMREAD; 552 return -1; 553 } 554 555 return s; 556 } 557 558 559 /* iperf_udp_init 560 * 561 * initializer for UDP streams in TEST_START 562 */ 563 int 564 iperf_udp_init(struct iperf_test *test) 565 { 566 return 0; 567 } 568