1a951c980SBrian Tierney /* 20fd60a36SBruce A. Mah * iperf, Copyright (c) 2014, 2016, 2017, The Regents of the University of 3da9f046fSBruce A. Mah * California, through Lawrence Berkeley National Laboratory (subject 4da9f046fSBruce A. Mah * to receipt of any required approvals from the U.S. Dept. of 5da9f046fSBruce A. Mah * Energy). All rights reserved. 67d375156SJon Dugan * 7da9f046fSBruce A. Mah * If you have questions about your rights to use or distribute this 8da9f046fSBruce A. Mah * software, please contact Berkeley Lab's Technology Transfer 9da9f046fSBruce A. Mah * Department at [email protected]. 10da9f046fSBruce A. Mah * 11da9f046fSBruce A. Mah * NOTICE. This software is owned by the U.S. Department of Energy. 12da9f046fSBruce A. Mah * As such, the U.S. Government has been granted for itself and others 13da9f046fSBruce A. Mah * acting on its behalf a paid-up, nonexclusive, irrevocable, 14da9f046fSBruce A. Mah * worldwide license in the Software to reproduce, prepare derivative 15da9f046fSBruce A. Mah * works, and perform publicly and display publicly. Beginning five 16da9f046fSBruce A. Mah * (5) years after the date permission to assert copyright is obtained 17da9f046fSBruce A. Mah * from the U.S. Department of Energy, and subject to any subsequent 18da9f046fSBruce A. Mah * five (5) year renewals, the U.S. Government is granted for itself 19da9f046fSBruce A. Mah * and others acting on its behalf a paid-up, nonexclusive, 20da9f046fSBruce A. Mah * irrevocable, worldwide license in the Software to reproduce, 21da9f046fSBruce A. Mah * prepare derivative works, distribute copies to the public, perform 22da9f046fSBruce A. Mah * publicly and display publicly, and to permit others to do so. 23da9f046fSBruce A. Mah * 24da9f046fSBruce A. Mah * This code is distributed under a BSD style license, see the LICENSE 25da9f046fSBruce A. Mah * file for complete information. 26a951c980SBrian Tierney */ 27a951c980SBrian Tierney #include <stdio.h> 28a951c980SBrian Tierney #include <stdlib.h> 29a951c980SBrian Tierney #include <string.h> 30a951c980SBrian Tierney #include <errno.h> 31a951c980SBrian Tierney #include <unistd.h> 32a951c980SBrian Tierney #include <assert.h> 33a951c980SBrian Tierney #include <sys/socket.h> 34a951c980SBrian Tierney #include <sys/types.h> 35a951c980SBrian Tierney #include <netinet/in.h> 36426221a3SBruce A. Mah #ifdef HAVE_STDINT_H 37a951c980SBrian Tierney #include <stdint.h> 38426221a3SBruce A. Mah #endif 39a951c980SBrian Tierney #include <sys/time.h> 4066ce7ad4Ssethdelliott #include <sys/select.h> 41a951c980SBrian Tierney 42a951c980SBrian Tierney #include "iperf.h" 43a951c980SBrian Tierney #include "iperf_api.h" 449673370fSJef Poskanzer #include "iperf_util.h" 45a951c980SBrian Tierney #include "iperf_udp.h" 46a951c980SBrian Tierney #include "timer.h" 47a951c980SBrian Tierney #include "net.h" 48e73821cdSBruce A. Mah #include "cjson.h" 49329523a5SBruce A. Mah #include "portable_endian.h" 50a951c980SBrian Tierney 51*d88f4cecSPhilip Prindeville #if defined(HAVE_INTTYPES_H) 52*d88f4cecSPhilip Prindeville # include <inttypes.h> 53*d88f4cecSPhilip Prindeville #else 54*d88f4cecSPhilip Prindeville # define PRIu64 "llu" 55*d88f4cecSPhilip Prindeville #endif 56*d88f4cecSPhilip Prindeville 578a0b5a5dSsethdelliott /* iperf_udp_recv 58a951c980SBrian Tierney * 598a0b5a5dSsethdelliott * receives the data for UDP 60a951c980SBrian Tierney */ 61a951c980SBrian Tierney int 62a951c980SBrian Tierney iperf_udp_recv(struct iperf_stream *sp) 63a951c980SBrian Tierney { 64329523a5SBruce A. Mah uint32_t sec, usec; 65329523a5SBruce A. Mah uint64_t pcount; 6643929b36SJef Poskanzer int r; 67a951c980SBrian Tierney int size = sp->settings->blksize; 68a951c980SBrian Tierney double transit = 0, d = 0; 698556db5dSsethdelliott struct timeval sent_time, arrival_time; 70a951c980SBrian Tierney 7143929b36SJef Poskanzer r = Nread(sp->socket, sp->buffer, size, Pudp); 72a951c980SBrian Tierney 738694d1dbSBruce A. Mah /* 748694d1dbSBruce A. Mah * If we got an error in the read, or if we didn't read anything 758694d1dbSBruce A. Mah * because the underlying read(2) got a EAGAIN, then skip packet 768694d1dbSBruce A. Mah * processing. 778694d1dbSBruce A. Mah */ 788694d1dbSBruce A. Mah if (r <= 0) 7943929b36SJef Poskanzer return r; 808a0b5a5dSsethdelliott 8143929b36SJef Poskanzer sp->result->bytes_received += r; 8243929b36SJef Poskanzer sp->result->bytes_received_this_interval += r; 838556db5dSsethdelliott 84329523a5SBruce A. Mah if (sp->test->udp_counters_64bit) { 858556db5dSsethdelliott memcpy(&sec, sp->buffer, sizeof(sec)); 868556db5dSsethdelliott memcpy(&usec, sp->buffer+4, sizeof(usec)); 878556db5dSsethdelliott memcpy(&pcount, sp->buffer+8, sizeof(pcount)); 888556db5dSsethdelliott sec = ntohl(sec); 898556db5dSsethdelliott usec = ntohl(usec); 90329523a5SBruce A. Mah pcount = be64toh(pcount); 918556db5dSsethdelliott sent_time.tv_sec = sec; 928556db5dSsethdelliott sent_time.tv_usec = usec; 93329523a5SBruce A. Mah } 94329523a5SBruce A. Mah else { 95329523a5SBruce A. Mah uint32_t pc; 96329523a5SBruce A. Mah memcpy(&sec, sp->buffer, sizeof(sec)); 97329523a5SBruce A. Mah memcpy(&usec, sp->buffer+4, sizeof(usec)); 98329523a5SBruce A. Mah memcpy(&pc, sp->buffer+8, sizeof(pc)); 99329523a5SBruce A. Mah sec = ntohl(sec); 100329523a5SBruce A. Mah usec = ntohl(usec); 101329523a5SBruce A. Mah pcount = ntohl(pc); 102329523a5SBruce A. Mah sent_time.tv_sec = sec; 103329523a5SBruce A. Mah sent_time.tv_usec = usec; 104329523a5SBruce A. Mah } 1058556db5dSsethdelliott 1067f061462SBruce A. Mah if (sp->test->debug) 107*d88f4cecSPhilip Prindeville fprintf(stderr, "pcount %" PRIu64 " packet_count %d\n", pcount, sp->packet_count); 1087f061462SBruce A. Mah 1097f061462SBruce A. Mah /* 1107f061462SBruce A. Mah * Try to handle out of order packets. The way we do this 1117f061462SBruce A. Mah * uses a constant amount of storage but might not be 1127f061462SBruce A. Mah * correct in all cases. In particular we seem to have the 1137f061462SBruce A. Mah * assumption that packets can't be duplicated in the network, 1147f061462SBruce A. Mah * because duplicate packets will possibly cause some problems here. 1157f061462SBruce A. Mah * 1167f061462SBruce A. Mah * First figure out if the sequence numbers are going forward. 1177f061462SBruce A. Mah * Note that pcount is the sequence number read from the packet, 1187f061462SBruce A. Mah * and sp->packet_count is the highest sequence number seen so 1197f061462SBruce A. Mah * far (so we're expecting to see the packet with sequence number 1207f061462SBruce A. Mah * sp->packet_count + 1 arrive next). 1217f061462SBruce A. Mah */ 1228556db5dSsethdelliott if (pcount >= sp->packet_count + 1) { 1237f061462SBruce A. Mah 1247f061462SBruce A. Mah /* Forward, but is there a gap in sequence numbers? */ 1258556db5dSsethdelliott if (pcount > sp->packet_count + 1) { 1267f061462SBruce A. Mah /* There's a gap so count that as a loss. */ 1278556db5dSsethdelliott sp->cnt_error += (pcount - 1) - sp->packet_count; 1288556db5dSsethdelliott } 1297f061462SBruce A. Mah /* Update the highest sequence number seen so far. */ 1308556db5dSsethdelliott sp->packet_count = pcount; 1318556db5dSsethdelliott } else { 1327f061462SBruce A. Mah 1337f061462SBruce A. Mah /* 1347f061462SBruce A. Mah * Sequence number went backward (or was stationary?!?). 1357f061462SBruce A. Mah * This counts as an out-of-order packet. 1367f061462SBruce A. Mah */ 1378556db5dSsethdelliott sp->outoforder_packets++; 1387f061462SBruce A. Mah 1397f061462SBruce A. Mah /* 1407f061462SBruce A. Mah * If we have lost packets, then the fact that we are now 1417f061462SBruce A. Mah * seeing an out-of-order packet offsets a prior sequence 1427f061462SBruce A. Mah * number gap that was counted as a loss. So we can take 1437f061462SBruce A. Mah * away a loss. 1447f061462SBruce A. Mah */ 1457f061462SBruce A. Mah if (sp->cnt_error > 0) 1467f061462SBruce A. Mah sp->cnt_error--; 1477f061462SBruce A. Mah 1487f061462SBruce A. Mah /* Log the out-of-order packet */ 1497f061462SBruce A. Mah if (sp->test->debug) 150*d88f4cecSPhilip Prindeville fprintf(stderr, "OUT OF ORDER - incoming packet sequence %" PRIu64 " but expected sequence %d on stream %d", pcount, sp->packet_count, sp->socket); 1518556db5dSsethdelliott } 152a951c980SBrian Tierney 15367653543SBruce A. Mah /* 15467653543SBruce A. Mah * jitter measurement 15567653543SBruce A. Mah * 15667653543SBruce A. Mah * This computation is based on RFC 1889 (specifically 15767653543SBruce A. Mah * sections 6.3.1 and A.8). 15867653543SBruce A. Mah * 15967653543SBruce A. Mah * Note that synchronized clocks are not required since 16067653543SBruce A. Mah * the source packet delta times are known. Also this 16167653543SBruce A. Mah * computation does not require knowing the round-trip 16267653543SBruce A. Mah * time. 16367653543SBruce A. Mah */ 164b60a49ddSsethdelliott gettimeofday(&arrival_time, NULL); 165b60a49ddSsethdelliott 1668556db5dSsethdelliott transit = timeval_diff(&sent_time, &arrival_time); 167a951c980SBrian Tierney d = transit - sp->prev_transit; 168a951c980SBrian Tierney if (d < 0) 169a951c980SBrian Tierney d = -d; 170a951c980SBrian Tierney sp->prev_transit = transit; 171a951c980SBrian Tierney sp->jitter += (d - sp->jitter) / 16.0; 172a951c980SBrian Tierney 17343929b36SJef Poskanzer return r; 174ec2d0670SJef Poskanzer } 175ec2d0670SJef Poskanzer 176ec2d0670SJef Poskanzer 1778a0b5a5dSsethdelliott /* iperf_udp_send 1788a0b5a5dSsethdelliott * 1798a0b5a5dSsethdelliott * sends the data for UDP 1808a0b5a5dSsethdelliott */ 181a951c980SBrian Tierney int 182a951c980SBrian Tierney iperf_udp_send(struct iperf_stream *sp) 183a951c980SBrian Tierney { 18443929b36SJef Poskanzer int r; 1858556db5dSsethdelliott int size = sp->settings->blksize; 186647d3b95SJef Poskanzer struct timeval before; 18743929b36SJef Poskanzer 188b60a49ddSsethdelliott gettimeofday(&before, 0); 189b60a49ddSsethdelliott 1908556db5dSsethdelliott ++sp->packet_count; 191329523a5SBruce A. Mah 192329523a5SBruce A. Mah if (sp->test->udp_counters_64bit) { 193329523a5SBruce A. Mah 194329523a5SBruce A. Mah uint32_t sec, usec; 195329523a5SBruce A. Mah uint64_t pcount; 196329523a5SBruce A. Mah 197329523a5SBruce A. Mah sec = htonl(before.tv_sec); 198329523a5SBruce A. Mah usec = htonl(before.tv_usec); 199329523a5SBruce A. Mah pcount = htobe64(sp->packet_count); 200329523a5SBruce A. Mah 201329523a5SBruce A. Mah memcpy(sp->buffer, &sec, sizeof(sec)); 202329523a5SBruce A. Mah memcpy(sp->buffer+4, &usec, sizeof(usec)); 203329523a5SBruce A. Mah memcpy(sp->buffer+8, &pcount, sizeof(pcount)); 204329523a5SBruce A. Mah 205329523a5SBruce A. Mah } 206329523a5SBruce A. Mah else { 207329523a5SBruce A. Mah 208329523a5SBruce A. Mah uint32_t sec, usec, pcount; 209329523a5SBruce A. Mah 2108556db5dSsethdelliott sec = htonl(before.tv_sec); 2118556db5dSsethdelliott usec = htonl(before.tv_usec); 2128556db5dSsethdelliott pcount = htonl(sp->packet_count); 213a951c980SBrian Tierney 2148556db5dSsethdelliott memcpy(sp->buffer, &sec, sizeof(sec)); 2158556db5dSsethdelliott memcpy(sp->buffer+4, &usec, sizeof(usec)); 2168556db5dSsethdelliott memcpy(sp->buffer+8, &pcount, sizeof(pcount)); 217a951c980SBrian Tierney 218329523a5SBruce A. Mah } 219329523a5SBruce A. Mah 22043929b36SJef Poskanzer r = Nwrite(sp->socket, sp->buffer, size, Pudp); 221a951c980SBrian Tierney 22243929b36SJef Poskanzer if (r < 0) 22343929b36SJef Poskanzer return r; 2248556db5dSsethdelliott 22543929b36SJef Poskanzer sp->result->bytes_sent += r; 22643929b36SJef Poskanzer sp->result->bytes_sent_this_interval += r; 227aa89cda2Ssethdelliott 228c458a115SBruce A. Mah if (sp->test->debug) 229*d88f4cecSPhilip Prindeville printf("sent %d bytes of %d, total %" PRIu64 "\n", r, sp->settings->blksize, sp->result->bytes_sent); 230c458a115SBruce A. Mah 23143929b36SJef Poskanzer return r; 232a951c980SBrian Tierney } 233a951c980SBrian Tierney 234a951c980SBrian Tierney 235a951c980SBrian Tierney /**************************************************************************/ 236a951c980SBrian Tierney 237d6598603SBruce A. Mah /* 238d6598603SBruce A. Mah * The following functions all have to do with managing UDP data sockets. 239d6598603SBruce A. Mah * UDP of course is connectionless, so there isn't really a concept of 240d6598603SBruce A. Mah * setting up a connection, although connect(2) can (and is) used to 241d6598603SBruce A. Mah * bind the remote end of sockets. We need to simulate some of the 242d6598603SBruce A. Mah * connection management that is built-in to TCP so that each side of the 243d6598603SBruce A. Mah * connection knows about each other before the real data transfers begin. 244d6598603SBruce A. Mah */ 245d6598603SBruce A. Mah 246d6598603SBruce A. Mah /* 2470fd60a36SBruce A. Mah * Set and verify socket buffer sizes. 2480fd60a36SBruce A. Mah * Return 0 if no error, -1 if an error, +1 if socket buffers are 2490fd60a36SBruce A. Mah * potentially too small to hold a message. 250a951c980SBrian Tierney */ 251a951c980SBrian Tierney int 2520fd60a36SBruce A. Mah iperf_udp_buffercheck(struct iperf_test *test, int s) 253a951c980SBrian Tierney { 2540fd60a36SBruce A. Mah int rc = 0; 255e73821cdSBruce A. Mah int sndbuf_actual, rcvbuf_actual; 256a951c980SBrian Tierney 257d6598603SBruce A. Mah /* 25872ac83e7SBruce A. Mah * Set socket buffer size if requested. Do this for both sending and 25972ac83e7SBruce A. Mah * receiving so that we can cover both normal and --reverse operation. 26072ac83e7SBruce A. Mah */ 26172ac83e7SBruce A. Mah int opt; 262d2202ee3SBruce A. Mah socklen_t optlen; 263d2202ee3SBruce A. Mah 26472ac83e7SBruce A. Mah if ((opt = test->settings->socket_bufsize)) { 26572ac83e7SBruce A. Mah if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt)) < 0) { 26672ac83e7SBruce A. Mah i_errno = IESETBUF; 26772ac83e7SBruce A. Mah return -1; 26872ac83e7SBruce A. Mah } 26972ac83e7SBruce A. Mah if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &opt, sizeof(opt)) < 0) { 27072ac83e7SBruce A. Mah i_errno = IESETBUF; 27172ac83e7SBruce A. Mah return -1; 27272ac83e7SBruce A. Mah } 27372ac83e7SBruce A. Mah } 27472ac83e7SBruce A. Mah 275d2202ee3SBruce A. Mah /* Read back and verify the sender socket buffer size */ 276e73821cdSBruce A. Mah optlen = sizeof(sndbuf_actual); 277e73821cdSBruce A. Mah if (getsockopt(s, SOL_SOCKET, SO_SNDBUF, &sndbuf_actual, &optlen) < 0) { 278d2202ee3SBruce A. Mah i_errno = IESETBUF; 279d2202ee3SBruce A. Mah return -1; 280d2202ee3SBruce A. Mah } 28155598a07SBruce A. Mah if (test->debug) { 282e73821cdSBruce A. Mah printf("SNDBUF is %u, expecting %u\n", sndbuf_actual, test->settings->socket_bufsize); 28355598a07SBruce A. Mah } 284e73821cdSBruce A. Mah if (test->settings->socket_bufsize && test->settings->socket_bufsize > sndbuf_actual) { 285d2202ee3SBruce A. Mah i_errno = IESETBUF2; 286d2202ee3SBruce A. Mah return -1; 287d2202ee3SBruce A. Mah } 288e73821cdSBruce A. Mah if (test->settings->blksize > sndbuf_actual) { 2890fd60a36SBruce A. Mah char str[80]; 2900fd60a36SBruce A. Mah snprintf(str, sizeof(str), 2910fd60a36SBruce A. Mah "Block size %d > sending socket buffer size %d", 292e73821cdSBruce A. Mah test->settings->blksize, sndbuf_actual); 2930fd60a36SBruce A. Mah warning(str); 2940fd60a36SBruce A. Mah rc = 1; 2950fd60a36SBruce A. Mah } 296d2202ee3SBruce A. Mah 297d2202ee3SBruce A. Mah /* Read back and verify the receiver socket buffer size */ 298e73821cdSBruce A. Mah optlen = sizeof(rcvbuf_actual); 299e73821cdSBruce A. Mah if (getsockopt(s, SOL_SOCKET, SO_RCVBUF, &rcvbuf_actual, &optlen) < 0) { 300d2202ee3SBruce A. Mah i_errno = IESETBUF; 301d2202ee3SBruce A. Mah return -1; 302d2202ee3SBruce A. Mah } 30355598a07SBruce A. Mah if (test->debug) { 304e73821cdSBruce A. Mah printf("RCVBUF is %u, expecting %u\n", rcvbuf_actual, test->settings->socket_bufsize); 30555598a07SBruce A. Mah } 306e73821cdSBruce A. Mah if (test->settings->socket_bufsize && test->settings->socket_bufsize > rcvbuf_actual) { 307d2202ee3SBruce A. Mah i_errno = IESETBUF2; 308d2202ee3SBruce A. Mah return -1; 309d2202ee3SBruce A. Mah } 310e73821cdSBruce A. Mah if (test->settings->blksize > rcvbuf_actual) { 3110fd60a36SBruce A. Mah char str[80]; 3120fd60a36SBruce A. Mah snprintf(str, sizeof(str), 3130fd60a36SBruce A. Mah "Block size %d > receiving socket buffer size %d", 314e73821cdSBruce A. Mah test->settings->blksize, rcvbuf_actual); 3150fd60a36SBruce A. Mah warning(str); 3160fd60a36SBruce A. Mah rc = 1; 3170fd60a36SBruce A. Mah } 318e73821cdSBruce A. Mah 319e73821cdSBruce A. Mah if (test->json_output) { 320e73821cdSBruce A. Mah cJSON_AddNumberToObject(test->json_start, "sock_bufsize", test->settings->socket_bufsize); 321e73821cdSBruce A. Mah cJSON_AddNumberToObject(test->json_start, "sndbuf_actual", sndbuf_actual); 322e73821cdSBruce A. Mah cJSON_AddNumberToObject(test->json_start, "rcvbuf_actual", rcvbuf_actual); 323e73821cdSBruce A. Mah } 324e73821cdSBruce A. Mah 3250fd60a36SBruce A. Mah return rc; 3260fd60a36SBruce A. Mah } 3270fd60a36SBruce A. Mah 3280fd60a36SBruce A. Mah /* 3290fd60a36SBruce A. Mah * iperf_udp_accept 3300fd60a36SBruce A. Mah * 3310fd60a36SBruce A. Mah * Accepts a new UDP "connection" 3320fd60a36SBruce A. Mah */ 3330fd60a36SBruce A. Mah int 3340fd60a36SBruce A. Mah iperf_udp_accept(struct iperf_test *test) 3350fd60a36SBruce A. Mah { 3360fd60a36SBruce A. Mah struct sockaddr_storage sa_peer; 3370fd60a36SBruce A. Mah int buf; 3380fd60a36SBruce A. Mah socklen_t len; 3390fd60a36SBruce A. Mah int sz, s; 3400fd60a36SBruce A. Mah int rc; 3410fd60a36SBruce A. Mah 3420fd60a36SBruce A. Mah /* 3430fd60a36SBruce A. Mah * Get the current outstanding socket. This socket will be used to handle 3440fd60a36SBruce A. Mah * data transfers and a new "listening" socket will be created. 3450fd60a36SBruce A. Mah */ 3460fd60a36SBruce A. Mah s = test->prot_listener; 3470fd60a36SBruce A. Mah 3480fd60a36SBruce A. Mah /* 3490fd60a36SBruce A. Mah * Grab the UDP packet sent by the client. From that we can extract the 3500fd60a36SBruce A. Mah * client's address, and then use that information to bind the remote side 3510fd60a36SBruce A. Mah * of the socket to the client. 3520fd60a36SBruce A. Mah */ 3530fd60a36SBruce A. Mah len = sizeof(sa_peer); 3540fd60a36SBruce A. Mah if ((sz = recvfrom(test->prot_listener, &buf, sizeof(buf), 0, (struct sockaddr *) &sa_peer, &len)) < 0) { 3550fd60a36SBruce A. Mah i_errno = IESTREAMACCEPT; 3560fd60a36SBruce A. Mah return -1; 3570fd60a36SBruce A. Mah } 3580fd60a36SBruce A. Mah 3590fd60a36SBruce A. Mah if (connect(s, (struct sockaddr *) &sa_peer, len) < 0) { 3600fd60a36SBruce A. Mah i_errno = IESTREAMACCEPT; 3610fd60a36SBruce A. Mah return -1; 3620fd60a36SBruce A. Mah } 3630fd60a36SBruce A. Mah 3640fd60a36SBruce A. Mah /* Check and set socket buffer sizes */ 3650fd60a36SBruce A. Mah rc = iperf_udp_buffercheck(test, s); 3660fd60a36SBruce A. Mah if (rc < 0) 3670fd60a36SBruce A. Mah /* error */ 3680fd60a36SBruce A. Mah return rc; 3690fd60a36SBruce A. Mah /* 3700fd60a36SBruce A. Mah * If the socket buffer was too small, but it was the default 3710fd60a36SBruce A. Mah * size, then try explicitly setting it to something larger. 3720fd60a36SBruce A. Mah */ 3730fd60a36SBruce A. Mah if (rc > 0) { 3740fd60a36SBruce A. Mah if (test->settings->socket_bufsize == 0) { 3751ff12f4cSBruce A. Mah int bufsize = test->settings->blksize + UDP_BUFFER_EXTRA; 3760fd60a36SBruce A. Mah printf("Increasing socket buffer size to %d\n", 3771ff12f4cSBruce A. Mah bufsize); 3781ff12f4cSBruce A. Mah test->settings->socket_bufsize = bufsize; 3790fd60a36SBruce A. Mah rc = iperf_udp_buffercheck(test, s); 3800fd60a36SBruce A. Mah if (rc < 0) 3810fd60a36SBruce A. Mah return rc; 3820fd60a36SBruce A. Mah } 3830fd60a36SBruce A. Mah } 384d2202ee3SBruce A. Mah 3859915746aSBruce A. Mah #if defined(HAVE_SO_MAX_PACING_RATE) 386a094d9feSBruce A. Mah /* If socket pacing is specified, try it. */ 387a094d9feSBruce A. Mah if (test->settings->fqrate) { 3889915746aSBruce A. Mah /* Convert bits per second to bytes per second */ 389a094d9feSBruce A. Mah unsigned int fqrate = test->settings->fqrate / 8; 390a094d9feSBruce A. Mah if (fqrate > 0) { 3919915746aSBruce A. Mah if (test->debug) { 392a094d9feSBruce A. Mah printf("Setting fair-queue socket pacing to %u\n", fqrate); 3939915746aSBruce A. Mah } 394a094d9feSBruce A. Mah if (setsockopt(s, SOL_SOCKET, SO_MAX_PACING_RATE, &fqrate, sizeof(fqrate)) < 0) { 395a094d9feSBruce A. Mah warning("Unable to set socket pacing"); 3969915746aSBruce A. Mah } 3979915746aSBruce A. Mah } 3989915746aSBruce A. Mah } 3999915746aSBruce A. Mah #endif /* HAVE_SO_MAX_PACING_RATE */ 400a094d9feSBruce A. Mah { 401a621cc26SBruce A. Mah unsigned int rate = test->settings->rate / 8; 402a621cc26SBruce A. Mah if (rate > 0) { 403a621cc26SBruce A. Mah if (test->debug) { 404a621cc26SBruce A. Mah printf("Setting application pacing to %u\n", rate); 405a621cc26SBruce A. Mah } 406a621cc26SBruce A. Mah } 407a621cc26SBruce A. Mah } 4089915746aSBruce A. Mah 40972ac83e7SBruce A. Mah /* 410d6598603SBruce A. Mah * Create a new "listening" socket to replace the one we were using before. 411d6598603SBruce A. Mah */ 412f99bd3b2Ssethdelliott test->prot_listener = netannounce(test->settings->domain, Pudp, test->bind_address, test->server_port); 4138a0b5a5dSsethdelliott if (test->prot_listener < 0) { 4148a0b5a5dSsethdelliott i_errno = IESTREAMLISTEN; 415ec2d0670SJef Poskanzer return -1; 416b60a49ddSsethdelliott } 417a951c980SBrian Tierney 4188a0b5a5dSsethdelliott FD_SET(test->prot_listener, &test->read_set); 4198a0b5a5dSsethdelliott test->max_fd = (test->max_fd < test->prot_listener) ? test->prot_listener : test->max_fd; 420a951c980SBrian Tierney 4218556db5dSsethdelliott /* Let the client know we're ready "accept" another UDP "stream" */ 422d6598603SBruce A. Mah buf = 987654321; /* any content will work here */ 4238a0b5a5dSsethdelliott if (write(s, &buf, sizeof(buf)) < 0) { 424b60a49ddSsethdelliott i_errno = IESTREAMWRITE; 425ec2d0670SJef Poskanzer return -1; 426a951c980SBrian Tierney } 427a951c980SBrian Tierney 428ec2d0670SJef Poskanzer return s; 4298a0b5a5dSsethdelliott } 4308a0b5a5dSsethdelliott 4318a0b5a5dSsethdelliott 432d6598603SBruce A. Mah /* 433d6598603SBruce A. Mah * iperf_udp_listen 4348a0b5a5dSsethdelliott * 435d6598603SBruce A. Mah * Start up a listener for UDP stream connections. Unlike for TCP, 436d6598603SBruce A. Mah * there is no listen(2) for UDP. This socket will however accept 437d6598603SBruce A. Mah * a UDP datagram from a client (indicating the client's presence). 4388a0b5a5dSsethdelliott */ 4398a0b5a5dSsethdelliott int 4408a0b5a5dSsethdelliott iperf_udp_listen(struct iperf_test *test) 4418a0b5a5dSsethdelliott { 4428a0b5a5dSsethdelliott int s; 4438a0b5a5dSsethdelliott 444f99bd3b2Ssethdelliott if ((s = netannounce(test->settings->domain, Pudp, test->bind_address, test->server_port)) < 0) { 4458a0b5a5dSsethdelliott i_errno = IESTREAMLISTEN; 446ec2d0670SJef Poskanzer return -1; 4478a0b5a5dSsethdelliott } 4488a0b5a5dSsethdelliott 449d6598603SBruce A. Mah /* 450d6598603SBruce A. Mah * The caller will put this value into test->prot_listener. 451d6598603SBruce A. Mah */ 452ec2d0670SJef Poskanzer return s; 4538a0b5a5dSsethdelliott } 4548a0b5a5dSsethdelliott 4558a0b5a5dSsethdelliott 456d6598603SBruce A. Mah /* 457d6598603SBruce A. Mah * iperf_udp_connect 4588a0b5a5dSsethdelliott * 459d6598603SBruce A. Mah * "Connect" to a UDP stream listener. 4608a0b5a5dSsethdelliott */ 4618a0b5a5dSsethdelliott int 4628a0b5a5dSsethdelliott iperf_udp_connect(struct iperf_test *test) 4638a0b5a5dSsethdelliott { 464111645edSJef Poskanzer int s, buf, sz; 465b8379188SHasso Tepper #ifdef SO_RCVTIMEO 4669b9dd030SHasso Tepper struct timeval tv; 467b8379188SHasso Tepper #endif 4680fd60a36SBruce A. Mah int rc; 4698a0b5a5dSsethdelliott 470d6598603SBruce A. Mah /* Create and bind our local socket. */ 4719d7d60acSBruce A. Mah if ((s = netdial(test->settings->domain, Pudp, test->bind_address, test->bind_port, test->server_hostname, test->server_port, -1)) < 0) { 4728a0b5a5dSsethdelliott i_errno = IESTREAMCONNECT; 473ec2d0670SJef Poskanzer return -1; 4748a0b5a5dSsethdelliott } 4758a0b5a5dSsethdelliott 4760fd60a36SBruce A. Mah /* Check and set socket buffer sizes */ 4770fd60a36SBruce A. Mah rc = iperf_udp_buffercheck(test, s); 4780fd60a36SBruce A. Mah if (rc < 0) 4790fd60a36SBruce A. Mah /* error */ 4800fd60a36SBruce A. Mah return rc; 481d6598603SBruce A. Mah /* 4820fd60a36SBruce A. Mah * If the socket buffer was too small, but it was the default 4830fd60a36SBruce A. Mah * size, then try explicitly setting it to something larger. 48472ac83e7SBruce A. Mah */ 4850fd60a36SBruce A. Mah if (rc > 0) { 4860fd60a36SBruce A. Mah if (test->settings->socket_bufsize == 0) { 4871ff12f4cSBruce A. Mah int bufsize = test->settings->blksize + UDP_BUFFER_EXTRA; 4880fd60a36SBruce A. Mah printf("Increasing socket buffer size to %d\n", 4891ff12f4cSBruce A. Mah bufsize); 4901ff12f4cSBruce A. Mah test->settings->socket_bufsize = bufsize; 4910fd60a36SBruce A. Mah rc = iperf_udp_buffercheck(test, s); 4920fd60a36SBruce A. Mah if (rc < 0) 4930fd60a36SBruce A. Mah return rc; 49472ac83e7SBruce A. Mah } 495d2202ee3SBruce A. Mah } 49672ac83e7SBruce A. Mah 4979915746aSBruce A. Mah #if defined(HAVE_SO_MAX_PACING_RATE) 4989915746aSBruce A. Mah /* If socket pacing is available and not disabled, try it. */ 499a094d9feSBruce A. Mah if (test->settings->fqrate) { 5009915746aSBruce A. Mah /* Convert bits per second to bytes per second */ 501a094d9feSBruce A. Mah unsigned int fqrate = test->settings->fqrate / 8; 502a094d9feSBruce A. Mah if (fqrate > 0) { 5039915746aSBruce A. Mah if (test->debug) { 504a094d9feSBruce A. Mah printf("Setting fair-queue socket pacing to %u\n", fqrate); 5059915746aSBruce A. Mah } 506a094d9feSBruce A. Mah if (setsockopt(s, SOL_SOCKET, SO_MAX_PACING_RATE, &fqrate, sizeof(fqrate)) < 0) { 507a094d9feSBruce A. Mah warning("Unable to set socket pacing"); 5089915746aSBruce A. Mah } 5099915746aSBruce A. Mah } 5109915746aSBruce A. Mah } 5119915746aSBruce A. Mah #endif /* HAVE_SO_MAX_PACING_RATE */ 512a094d9feSBruce A. Mah { 513a621cc26SBruce A. Mah unsigned int rate = test->settings->rate / 8; 514a621cc26SBruce A. Mah if (rate > 0) { 515a621cc26SBruce A. Mah if (test->debug) { 516a621cc26SBruce A. Mah printf("Setting application pacing to %u\n", rate); 517a621cc26SBruce A. Mah } 518a621cc26SBruce A. Mah } 519a621cc26SBruce A. Mah } 5209915746aSBruce A. Mah 521b8379188SHasso Tepper #ifdef SO_RCVTIMEO 5229b9dd030SHasso Tepper /* 30 sec timeout for a case when there is a network problem. */ 5239b9dd030SHasso Tepper tv.tv_sec = 30; 5249b9dd030SHasso Tepper tv.tv_usec = 0; 5259b9dd030SHasso Tepper setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (struct timeval *)&tv, sizeof(struct timeval)); 526b8379188SHasso Tepper #endif 5279b9dd030SHasso Tepper 52872ac83e7SBruce A. Mah /* 529d6598603SBruce A. Mah * Write a datagram to the UDP stream to let the server know we're here. 530d6598603SBruce A. Mah * The server learns our address by obtaining its peer's address. 531d6598603SBruce A. Mah */ 532d6598603SBruce A. Mah buf = 123456789; /* this can be pretty much anything */ 5338a0b5a5dSsethdelliott if (write(s, &buf, sizeof(buf)) < 0) { 5348a0b5a5dSsethdelliott // XXX: Should this be changed to IESTREAMCONNECT? 5358a0b5a5dSsethdelliott i_errno = IESTREAMWRITE; 536ec2d0670SJef Poskanzer return -1; 5378a0b5a5dSsethdelliott } 538111645edSJef Poskanzer 539d6598603SBruce A. Mah /* 540d6598603SBruce A. Mah * Wait until the server replies back to us. 541d6598603SBruce A. Mah */ 542111645edSJef Poskanzer if ((sz = recv(s, &buf, sizeof(buf), 0)) < 0) { 5438a0b5a5dSsethdelliott i_errno = IESTREAMREAD; 544ec2d0670SJef Poskanzer return -1; 5458a0b5a5dSsethdelliott } 5468a0b5a5dSsethdelliott 547ec2d0670SJef Poskanzer return s; 5488a0b5a5dSsethdelliott } 5498a0b5a5dSsethdelliott 5508a0b5a5dSsethdelliott 5518a0b5a5dSsethdelliott /* iperf_udp_init 5528a0b5a5dSsethdelliott * 5538a0b5a5dSsethdelliott * initializer for UDP streams in TEST_START 5548a0b5a5dSsethdelliott */ 5558a0b5a5dSsethdelliott int 5568a0b5a5dSsethdelliott iperf_udp_init(struct iperf_test *test) 5578a0b5a5dSsethdelliott { 558ec2d0670SJef Poskanzer return 0; 559a951c980SBrian Tierney } 560