xref: /iperf/src/iperf_udp.c (revision cde81d76)
1a951c980SBrian Tierney /*
2c0055199SBruce A. Mah  * iperf, Copyright (c) 2014-2018, 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 
51d88f4cecSPhilip Prindeville #if defined(HAVE_INTTYPES_H)
52d88f4cecSPhilip Prindeville # include <inttypes.h>
53d88f4cecSPhilip Prindeville #else
54d88f4cecSPhilip Prindeville # define PRIu64		"llu"
55d88f4cecSPhilip Prindeville #endif
56d88f4cecSPhilip 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;
69*cde81d76SBen Fox-Moore     struct iperf_time sent_time, arrival_time, temp_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 
81c0055199SBruce A. Mah     /* Only count bytes received while we're in the correct state. */
82c0055199SBruce A. Mah     if (sp->test->state == TEST_RUNNING) {
8343929b36SJef Poskanzer 	sp->result->bytes_received += r;
8443929b36SJef Poskanzer 	sp->result->bytes_received_this_interval += r;
858556db5dSsethdelliott 
86329523a5SBruce A. Mah 	if (sp->test->udp_counters_64bit) {
878556db5dSsethdelliott 	    memcpy(&sec, sp->buffer, sizeof(sec));
888556db5dSsethdelliott 	    memcpy(&usec, sp->buffer+4, sizeof(usec));
898556db5dSsethdelliott 	    memcpy(&pcount, sp->buffer+8, sizeof(pcount));
908556db5dSsethdelliott 	    sec = ntohl(sec);
918556db5dSsethdelliott 	    usec = ntohl(usec);
92329523a5SBruce A. Mah 	    pcount = be64toh(pcount);
93*cde81d76SBen Fox-Moore 	    sent_time.secs = sec;
94*cde81d76SBen Fox-Moore 	    sent_time.usecs = usec;
95329523a5SBruce A. Mah 	}
96329523a5SBruce A. Mah 	else {
97329523a5SBruce A. Mah 	    uint32_t pc;
98329523a5SBruce A. Mah 	    memcpy(&sec, sp->buffer, sizeof(sec));
99329523a5SBruce A. Mah 	    memcpy(&usec, sp->buffer+4, sizeof(usec));
100329523a5SBruce A. Mah 	    memcpy(&pc, sp->buffer+8, sizeof(pc));
101329523a5SBruce A. Mah 	    sec = ntohl(sec);
102329523a5SBruce A. Mah 	    usec = ntohl(usec);
103329523a5SBruce A. Mah 	    pcount = ntohl(pc);
104*cde81d76SBen Fox-Moore 	    sent_time.secs = sec;
105*cde81d76SBen Fox-Moore 	    sent_time.usecs = usec;
106329523a5SBruce A. Mah 	}
1078556db5dSsethdelliott 
1087f061462SBruce A. Mah 	if (sp->test->debug)
109d88f4cecSPhilip Prindeville 	    fprintf(stderr, "pcount %" PRIu64 " packet_count %d\n", pcount, sp->packet_count);
1107f061462SBruce A. Mah 
1117f061462SBruce A. Mah 	/*
1127f061462SBruce A. Mah 	 * Try to handle out of order packets.  The way we do this
1137f061462SBruce A. Mah 	 * uses a constant amount of storage but might not be
1147f061462SBruce A. Mah 	 * correct in all cases.  In particular we seem to have the
1157f061462SBruce A. Mah 	 * assumption that packets can't be duplicated in the network,
1167f061462SBruce A. Mah 	 * because duplicate packets will possibly cause some problems here.
1177f061462SBruce A. Mah 	 *
1187f061462SBruce A. Mah 	 * First figure out if the sequence numbers are going forward.
1197f061462SBruce A. Mah 	 * Note that pcount is the sequence number read from the packet,
1207f061462SBruce A. Mah 	 * and sp->packet_count is the highest sequence number seen so
1217f061462SBruce A. Mah 	 * far (so we're expecting to see the packet with sequence number
1227f061462SBruce A. Mah 	 * sp->packet_count + 1 arrive next).
1237f061462SBruce A. Mah 	 */
1248556db5dSsethdelliott 	if (pcount >= sp->packet_count + 1) {
1257f061462SBruce A. Mah 
1267f061462SBruce A. Mah 	    /* Forward, but is there a gap in sequence numbers? */
1278556db5dSsethdelliott 	    if (pcount > sp->packet_count + 1) {
1287f061462SBruce A. Mah 		/* There's a gap so count that as a loss. */
1298556db5dSsethdelliott 		sp->cnt_error += (pcount - 1) - sp->packet_count;
1308556db5dSsethdelliott 	    }
1317f061462SBruce A. Mah 	    /* Update the highest sequence number seen so far. */
1328556db5dSsethdelliott 	    sp->packet_count = pcount;
1338556db5dSsethdelliott 	} else {
1347f061462SBruce A. Mah 
1357f061462SBruce A. Mah 	    /*
1367f061462SBruce A. Mah 	     * Sequence number went backward (or was stationary?!?).
1377f061462SBruce A. Mah 	     * This counts as an out-of-order packet.
1387f061462SBruce A. Mah 	     */
1398556db5dSsethdelliott 	    sp->outoforder_packets++;
1407f061462SBruce A. Mah 
1417f061462SBruce A. Mah 	    /*
1427f061462SBruce A. Mah 	     * If we have lost packets, then the fact that we are now
1437f061462SBruce A. Mah 	     * seeing an out-of-order packet offsets a prior sequence
1447f061462SBruce A. Mah 	     * number gap that was counted as a loss.  So we can take
1457f061462SBruce A. Mah 	     * away a loss.
1467f061462SBruce A. Mah 	     */
1477f061462SBruce A. Mah 	    if (sp->cnt_error > 0)
1487f061462SBruce A. Mah 		sp->cnt_error--;
1497f061462SBruce A. Mah 
1507f061462SBruce A. Mah 	    /* Log the out-of-order packet */
1517f061462SBruce A. Mah 	    if (sp->test->debug)
152d88f4cecSPhilip Prindeville 		fprintf(stderr, "OUT OF ORDER - incoming packet sequence %" PRIu64 " but expected sequence %d on stream %d", pcount, sp->packet_count, sp->socket);
1538556db5dSsethdelliott 	}
154a951c980SBrian Tierney 
15567653543SBruce A. Mah 	/*
15667653543SBruce A. Mah 	 * jitter measurement
15767653543SBruce A. Mah 	 *
15867653543SBruce A. Mah 	 * This computation is based on RFC 1889 (specifically
15967653543SBruce A. Mah 	 * sections 6.3.1 and A.8).
16067653543SBruce A. Mah 	 *
16167653543SBruce A. Mah 	 * Note that synchronized clocks are not required since
16267653543SBruce A. Mah 	 * the source packet delta times are known.  Also this
16367653543SBruce A. Mah 	 * computation does not require knowing the round-trip
16467653543SBruce A. Mah 	 * time.
16567653543SBruce A. Mah 	 */
166*cde81d76SBen Fox-Moore 	iperf_time_now(&arrival_time);
167b60a49ddSsethdelliott 
168*cde81d76SBen Fox-Moore 	iperf_time_diff(&arrival_time, &sent_time, &temp_time);
169*cde81d76SBen Fox-Moore 	transit = iperf_time_in_secs(&temp_time);
170a951c980SBrian Tierney 	d = transit - sp->prev_transit;
171a951c980SBrian Tierney 	if (d < 0)
172a951c980SBrian Tierney 	    d = -d;
173a951c980SBrian Tierney 	sp->prev_transit = transit;
174a951c980SBrian Tierney 	sp->jitter += (d - sp->jitter) / 16.0;
175c0055199SBruce A. Mah     }
176c0055199SBruce A. Mah     else {
177c0055199SBruce A. Mah 	if (sp->test->debug)
178c0055199SBruce A. Mah 	    printf("Late receive, state = %d\n", sp->test->state);
179c0055199SBruce A. Mah     }
180a951c980SBrian Tierney 
18143929b36SJef Poskanzer     return r;
182ec2d0670SJef Poskanzer }
183ec2d0670SJef Poskanzer 
184ec2d0670SJef Poskanzer 
1858a0b5a5dSsethdelliott /* iperf_udp_send
1868a0b5a5dSsethdelliott  *
1878a0b5a5dSsethdelliott  * sends the data for UDP
1888a0b5a5dSsethdelliott  */
189a951c980SBrian Tierney int
190a951c980SBrian Tierney iperf_udp_send(struct iperf_stream *sp)
191a951c980SBrian Tierney {
19243929b36SJef Poskanzer     int r;
1938556db5dSsethdelliott     int       size = sp->settings->blksize;
194*cde81d76SBen Fox-Moore     struct iperf_time before;
19543929b36SJef Poskanzer 
196*cde81d76SBen Fox-Moore     iperf_time_now(&before);
197b60a49ddSsethdelliott 
1988556db5dSsethdelliott     ++sp->packet_count;
199329523a5SBruce A. Mah 
200329523a5SBruce A. Mah     if (sp->test->udp_counters_64bit) {
201329523a5SBruce A. Mah 
202329523a5SBruce A. Mah 	uint32_t  sec, usec;
203329523a5SBruce A. Mah 	uint64_t  pcount;
204329523a5SBruce A. Mah 
205*cde81d76SBen Fox-Moore 	sec = htonl(before.secs);
206*cde81d76SBen Fox-Moore 	usec = htonl(before.usecs);
207329523a5SBruce A. Mah 	pcount = htobe64(sp->packet_count);
208329523a5SBruce A. Mah 
209329523a5SBruce A. Mah 	memcpy(sp->buffer, &sec, sizeof(sec));
210329523a5SBruce A. Mah 	memcpy(sp->buffer+4, &usec, sizeof(usec));
211329523a5SBruce A. Mah 	memcpy(sp->buffer+8, &pcount, sizeof(pcount));
212329523a5SBruce A. Mah 
213329523a5SBruce A. Mah     }
214329523a5SBruce A. Mah     else {
215329523a5SBruce A. Mah 
216329523a5SBruce A. Mah 	uint32_t  sec, usec, pcount;
217329523a5SBruce A. Mah 
218*cde81d76SBen Fox-Moore 	sec = htonl(before.secs);
219*cde81d76SBen Fox-Moore 	usec = htonl(before.usecs);
2208556db5dSsethdelliott 	pcount = htonl(sp->packet_count);
221a951c980SBrian Tierney 
2228556db5dSsethdelliott 	memcpy(sp->buffer, &sec, sizeof(sec));
2238556db5dSsethdelliott 	memcpy(sp->buffer+4, &usec, sizeof(usec));
2248556db5dSsethdelliott 	memcpy(sp->buffer+8, &pcount, sizeof(pcount));
225a951c980SBrian Tierney 
226329523a5SBruce A. Mah     }
227329523a5SBruce A. Mah 
22843929b36SJef Poskanzer     r = Nwrite(sp->socket, sp->buffer, size, Pudp);
229a951c980SBrian Tierney 
23043929b36SJef Poskanzer     if (r < 0)
23143929b36SJef Poskanzer 	return r;
2328556db5dSsethdelliott 
23343929b36SJef Poskanzer     sp->result->bytes_sent += r;
23443929b36SJef Poskanzer     sp->result->bytes_sent_this_interval += r;
235aa89cda2Ssethdelliott 
236c458a115SBruce A. Mah     if (sp->test->debug)
237d88f4cecSPhilip Prindeville 	printf("sent %d bytes of %d, total %" PRIu64 "\n", r, sp->settings->blksize, sp->result->bytes_sent);
238c458a115SBruce A. Mah 
23943929b36SJef Poskanzer     return r;
240a951c980SBrian Tierney }
241a951c980SBrian Tierney 
242a951c980SBrian Tierney 
243a951c980SBrian Tierney /**************************************************************************/
244a951c980SBrian Tierney 
245d6598603SBruce A. Mah /*
246d6598603SBruce A. Mah  * The following functions all have to do with managing UDP data sockets.
247d6598603SBruce A. Mah  * UDP of course is connectionless, so there isn't really a concept of
248d6598603SBruce A. Mah  * setting up a connection, although connect(2) can (and is) used to
249d6598603SBruce A. Mah  * bind the remote end of sockets.  We need to simulate some of the
250d6598603SBruce A. Mah  * connection management that is built-in to TCP so that each side of the
251d6598603SBruce A. Mah  * connection knows about each other before the real data transfers begin.
252d6598603SBruce A. Mah  */
253d6598603SBruce A. Mah 
254d6598603SBruce A. Mah /*
2550fd60a36SBruce A. Mah  * Set and verify socket buffer sizes.
2560fd60a36SBruce A. Mah  * Return 0 if no error, -1 if an error, +1 if socket buffers are
2570fd60a36SBruce A. Mah  * potentially too small to hold a message.
258a951c980SBrian Tierney  */
259a951c980SBrian Tierney int
2600fd60a36SBruce A. Mah iperf_udp_buffercheck(struct iperf_test *test, int s)
261a951c980SBrian Tierney {
2620fd60a36SBruce A. Mah     int rc = 0;
263e73821cdSBruce A. Mah     int sndbuf_actual, rcvbuf_actual;
264a951c980SBrian Tierney 
265d6598603SBruce A. Mah     /*
26672ac83e7SBruce A. Mah      * Set socket buffer size if requested.  Do this for both sending and
26772ac83e7SBruce A. Mah      * receiving so that we can cover both normal and --reverse operation.
26872ac83e7SBruce A. Mah      */
26972ac83e7SBruce A. Mah     int opt;
270d2202ee3SBruce A. Mah     socklen_t optlen;
271d2202ee3SBruce A. Mah 
27272ac83e7SBruce A. Mah     if ((opt = test->settings->socket_bufsize)) {
27372ac83e7SBruce A. Mah         if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt)) < 0) {
27472ac83e7SBruce A. Mah             i_errno = IESETBUF;
27572ac83e7SBruce A. Mah             return -1;
27672ac83e7SBruce A. Mah         }
27772ac83e7SBruce A. Mah         if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &opt, sizeof(opt)) < 0) {
27872ac83e7SBruce A. Mah             i_errno = IESETBUF;
27972ac83e7SBruce A. Mah             return -1;
28072ac83e7SBruce A. Mah         }
28172ac83e7SBruce A. Mah     }
28272ac83e7SBruce A. Mah 
283d2202ee3SBruce A. Mah     /* Read back and verify the sender socket buffer size */
284e73821cdSBruce A. Mah     optlen = sizeof(sndbuf_actual);
285e73821cdSBruce A. Mah     if (getsockopt(s, SOL_SOCKET, SO_SNDBUF, &sndbuf_actual, &optlen) < 0) {
286d2202ee3SBruce A. Mah 	i_errno = IESETBUF;
287d2202ee3SBruce A. Mah 	return -1;
288d2202ee3SBruce A. Mah     }
28955598a07SBruce A. Mah     if (test->debug) {
290e73821cdSBruce A. Mah 	printf("SNDBUF is %u, expecting %u\n", sndbuf_actual, test->settings->socket_bufsize);
29155598a07SBruce A. Mah     }
292e73821cdSBruce A. Mah     if (test->settings->socket_bufsize && test->settings->socket_bufsize > sndbuf_actual) {
293d2202ee3SBruce A. Mah 	i_errno = IESETBUF2;
294d2202ee3SBruce A. Mah 	return -1;
295d2202ee3SBruce A. Mah     }
296e73821cdSBruce A. Mah     if (test->settings->blksize > sndbuf_actual) {
2970fd60a36SBruce A. Mah 	char str[80];
2980fd60a36SBruce A. Mah 	snprintf(str, sizeof(str),
2990fd60a36SBruce A. Mah 		 "Block size %d > sending socket buffer size %d",
300e73821cdSBruce A. Mah 		 test->settings->blksize, sndbuf_actual);
3010fd60a36SBruce A. Mah 	warning(str);
3020fd60a36SBruce A. Mah 	rc = 1;
3030fd60a36SBruce A. Mah     }
304d2202ee3SBruce A. Mah 
305d2202ee3SBruce A. Mah     /* Read back and verify the receiver socket buffer size */
306e73821cdSBruce A. Mah     optlen = sizeof(rcvbuf_actual);
307e73821cdSBruce A. Mah     if (getsockopt(s, SOL_SOCKET, SO_RCVBUF, &rcvbuf_actual, &optlen) < 0) {
308d2202ee3SBruce A. Mah 	i_errno = IESETBUF;
309d2202ee3SBruce A. Mah 	return -1;
310d2202ee3SBruce A. Mah     }
31155598a07SBruce A. Mah     if (test->debug) {
312e73821cdSBruce A. Mah 	printf("RCVBUF is %u, expecting %u\n", rcvbuf_actual, test->settings->socket_bufsize);
31355598a07SBruce A. Mah     }
314e73821cdSBruce A. Mah     if (test->settings->socket_bufsize && test->settings->socket_bufsize > rcvbuf_actual) {
315d2202ee3SBruce A. Mah 	i_errno = IESETBUF2;
316d2202ee3SBruce A. Mah 	return -1;
317d2202ee3SBruce A. Mah     }
318e73821cdSBruce A. Mah     if (test->settings->blksize > rcvbuf_actual) {
3190fd60a36SBruce A. Mah 	char str[80];
3200fd60a36SBruce A. Mah 	snprintf(str, sizeof(str),
3210fd60a36SBruce A. Mah 		 "Block size %d > receiving socket buffer size %d",
322e73821cdSBruce A. Mah 		 test->settings->blksize, rcvbuf_actual);
3230fd60a36SBruce A. Mah 	warning(str);
3240fd60a36SBruce A. Mah 	rc = 1;
3250fd60a36SBruce A. Mah     }
326e73821cdSBruce A. Mah 
327e73821cdSBruce A. Mah     if (test->json_output) {
328e73821cdSBruce A. Mah 	cJSON_AddNumberToObject(test->json_start, "sock_bufsize", test->settings->socket_bufsize);
329e73821cdSBruce A. Mah 	cJSON_AddNumberToObject(test->json_start, "sndbuf_actual", sndbuf_actual);
330e73821cdSBruce A. Mah 	cJSON_AddNumberToObject(test->json_start, "rcvbuf_actual", rcvbuf_actual);
331e73821cdSBruce A. Mah     }
332e73821cdSBruce A. Mah 
3330fd60a36SBruce A. Mah     return rc;
3340fd60a36SBruce A. Mah }
3350fd60a36SBruce A. Mah 
3360fd60a36SBruce A. Mah /*
3370fd60a36SBruce A. Mah  * iperf_udp_accept
3380fd60a36SBruce A. Mah  *
3390fd60a36SBruce A. Mah  * Accepts a new UDP "connection"
3400fd60a36SBruce A. Mah  */
3410fd60a36SBruce A. Mah int
3420fd60a36SBruce A. Mah iperf_udp_accept(struct iperf_test *test)
3430fd60a36SBruce A. Mah {
3440fd60a36SBruce A. Mah     struct sockaddr_storage sa_peer;
3450fd60a36SBruce A. Mah     int       buf;
3460fd60a36SBruce A. Mah     socklen_t len;
3470fd60a36SBruce A. Mah     int       sz, s;
3480fd60a36SBruce A. Mah     int	      rc;
3490fd60a36SBruce A. Mah 
3500fd60a36SBruce A. Mah     /*
3510fd60a36SBruce A. Mah      * Get the current outstanding socket.  This socket will be used to handle
3520fd60a36SBruce A. Mah      * data transfers and a new "listening" socket will be created.
3530fd60a36SBruce A. Mah      */
3540fd60a36SBruce A. Mah     s = test->prot_listener;
3550fd60a36SBruce A. Mah 
3560fd60a36SBruce A. Mah     /*
3570fd60a36SBruce A. Mah      * Grab the UDP packet sent by the client.  From that we can extract the
3580fd60a36SBruce A. Mah      * client's address, and then use that information to bind the remote side
3590fd60a36SBruce A. Mah      * of the socket to the client.
3600fd60a36SBruce A. Mah      */
3610fd60a36SBruce A. Mah     len = sizeof(sa_peer);
3620fd60a36SBruce A. Mah     if ((sz = recvfrom(test->prot_listener, &buf, sizeof(buf), 0, (struct sockaddr *) &sa_peer, &len)) < 0) {
3630fd60a36SBruce A. Mah         i_errno = IESTREAMACCEPT;
3640fd60a36SBruce A. Mah         return -1;
3650fd60a36SBruce A. Mah     }
3660fd60a36SBruce A. Mah 
3670fd60a36SBruce A. Mah     if (connect(s, (struct sockaddr *) &sa_peer, len) < 0) {
3680fd60a36SBruce A. Mah         i_errno = IESTREAMACCEPT;
3690fd60a36SBruce A. Mah         return -1;
3700fd60a36SBruce A. Mah     }
3710fd60a36SBruce A. Mah 
3720fd60a36SBruce A. Mah     /* Check and set socket buffer sizes */
3730fd60a36SBruce A. Mah     rc = iperf_udp_buffercheck(test, s);
3740fd60a36SBruce A. Mah     if (rc < 0)
3750fd60a36SBruce A. Mah 	/* error */
3760fd60a36SBruce A. Mah 	return rc;
3770fd60a36SBruce A. Mah     /*
3780fd60a36SBruce A. Mah      * If the socket buffer was too small, but it was the default
3790fd60a36SBruce A. Mah      * size, then try explicitly setting it to something larger.
3800fd60a36SBruce A. Mah      */
3810fd60a36SBruce A. Mah     if (rc > 0) {
3820fd60a36SBruce A. Mah 	if (test->settings->socket_bufsize == 0) {
3831ff12f4cSBruce A. Mah 	    int bufsize = test->settings->blksize + UDP_BUFFER_EXTRA;
3840fd60a36SBruce A. Mah 	    printf("Increasing socket buffer size to %d\n",
3851ff12f4cSBruce A. Mah 		bufsize);
3861ff12f4cSBruce A. Mah 	    test->settings->socket_bufsize = bufsize;
3870fd60a36SBruce A. Mah 	    rc = iperf_udp_buffercheck(test, s);
3880fd60a36SBruce A. Mah 	    if (rc < 0)
3890fd60a36SBruce A. Mah 		return rc;
3900fd60a36SBruce A. Mah 	}
3910fd60a36SBruce A. Mah     }
392d2202ee3SBruce A. Mah 
3939915746aSBruce A. Mah #if defined(HAVE_SO_MAX_PACING_RATE)
394a094d9feSBruce A. Mah     /* If socket pacing is specified, try it. */
395a094d9feSBruce A. Mah     if (test->settings->fqrate) {
3969915746aSBruce A. Mah 	/* Convert bits per second to bytes per second */
397a094d9feSBruce A. Mah 	unsigned int fqrate = test->settings->fqrate / 8;
398a094d9feSBruce A. Mah 	if (fqrate > 0) {
3999915746aSBruce A. Mah 	    if (test->debug) {
400a094d9feSBruce A. Mah 		printf("Setting fair-queue socket pacing to %u\n", fqrate);
4019915746aSBruce A. Mah 	    }
402a094d9feSBruce A. Mah 	    if (setsockopt(s, SOL_SOCKET, SO_MAX_PACING_RATE, &fqrate, sizeof(fqrate)) < 0) {
403a094d9feSBruce A. Mah 		warning("Unable to set socket pacing");
4049915746aSBruce A. Mah 	    }
4059915746aSBruce A. Mah 	}
4069915746aSBruce A. Mah     }
4079915746aSBruce A. Mah #endif /* HAVE_SO_MAX_PACING_RATE */
408a094d9feSBruce A. Mah     {
409a621cc26SBruce A. Mah 	unsigned int rate = test->settings->rate / 8;
410a621cc26SBruce A. Mah 	if (rate > 0) {
411a621cc26SBruce A. Mah 	    if (test->debug) {
412a621cc26SBruce A. Mah 		printf("Setting application pacing to %u\n", rate);
413a621cc26SBruce A. Mah 	    }
414a621cc26SBruce A. Mah 	}
415a621cc26SBruce A. Mah     }
4169915746aSBruce A. Mah 
41772ac83e7SBruce A. Mah     /*
418d6598603SBruce A. Mah      * Create a new "listening" socket to replace the one we were using before.
419d6598603SBruce A. Mah      */
420f99bd3b2Ssethdelliott     test->prot_listener = netannounce(test->settings->domain, Pudp, test->bind_address, test->server_port);
4218a0b5a5dSsethdelliott     if (test->prot_listener < 0) {
4228a0b5a5dSsethdelliott         i_errno = IESTREAMLISTEN;
423ec2d0670SJef Poskanzer         return -1;
424b60a49ddSsethdelliott     }
425a951c980SBrian Tierney 
4268a0b5a5dSsethdelliott     FD_SET(test->prot_listener, &test->read_set);
4278a0b5a5dSsethdelliott     test->max_fd = (test->max_fd < test->prot_listener) ? test->prot_listener : test->max_fd;
428a951c980SBrian Tierney 
4298556db5dSsethdelliott     /* Let the client know we're ready "accept" another UDP "stream" */
430d6598603SBruce A. Mah     buf = 987654321;		/* any content will work here */
4318a0b5a5dSsethdelliott     if (write(s, &buf, sizeof(buf)) < 0) {
432b60a49ddSsethdelliott         i_errno = IESTREAMWRITE;
433ec2d0670SJef Poskanzer         return -1;
434a951c980SBrian Tierney     }
435a951c980SBrian Tierney 
436ec2d0670SJef Poskanzer     return s;
4378a0b5a5dSsethdelliott }
4388a0b5a5dSsethdelliott 
4398a0b5a5dSsethdelliott 
440d6598603SBruce A. Mah /*
441d6598603SBruce A. Mah  * iperf_udp_listen
4428a0b5a5dSsethdelliott  *
443d6598603SBruce A. Mah  * Start up a listener for UDP stream connections.  Unlike for TCP,
444d6598603SBruce A. Mah  * there is no listen(2) for UDP.  This socket will however accept
445d6598603SBruce A. Mah  * a UDP datagram from a client (indicating the client's presence).
4468a0b5a5dSsethdelliott  */
4478a0b5a5dSsethdelliott int
4488a0b5a5dSsethdelliott iperf_udp_listen(struct iperf_test *test)
4498a0b5a5dSsethdelliott {
4508a0b5a5dSsethdelliott     int s;
4518a0b5a5dSsethdelliott 
452f99bd3b2Ssethdelliott     if ((s = netannounce(test->settings->domain, Pudp, test->bind_address, test->server_port)) < 0) {
4538a0b5a5dSsethdelliott         i_errno = IESTREAMLISTEN;
454ec2d0670SJef Poskanzer         return -1;
4558a0b5a5dSsethdelliott     }
4568a0b5a5dSsethdelliott 
457d6598603SBruce A. Mah     /*
458d6598603SBruce A. Mah      * The caller will put this value into test->prot_listener.
459d6598603SBruce A. Mah      */
460ec2d0670SJef Poskanzer     return s;
4618a0b5a5dSsethdelliott }
4628a0b5a5dSsethdelliott 
4638a0b5a5dSsethdelliott 
464d6598603SBruce A. Mah /*
465d6598603SBruce A. Mah  * iperf_udp_connect
4668a0b5a5dSsethdelliott  *
467d6598603SBruce A. Mah  * "Connect" to a UDP stream listener.
4688a0b5a5dSsethdelliott  */
4698a0b5a5dSsethdelliott int
4708a0b5a5dSsethdelliott iperf_udp_connect(struct iperf_test *test)
4718a0b5a5dSsethdelliott {
472111645edSJef Poskanzer     int s, buf, sz;
473b8379188SHasso Tepper #ifdef SO_RCVTIMEO
4749b9dd030SHasso Tepper     struct timeval tv;
475b8379188SHasso Tepper #endif
4760fd60a36SBruce A. Mah     int rc;
4778a0b5a5dSsethdelliott 
478d6598603SBruce A. Mah     /* Create and bind our local socket. */
4799d7d60acSBruce A. Mah     if ((s = netdial(test->settings->domain, Pudp, test->bind_address, test->bind_port, test->server_hostname, test->server_port, -1)) < 0) {
4808a0b5a5dSsethdelliott         i_errno = IESTREAMCONNECT;
481ec2d0670SJef Poskanzer         return -1;
4828a0b5a5dSsethdelliott     }
4838a0b5a5dSsethdelliott 
4840fd60a36SBruce A. Mah     /* Check and set socket buffer sizes */
4850fd60a36SBruce A. Mah     rc = iperf_udp_buffercheck(test, s);
4860fd60a36SBruce A. Mah     if (rc < 0)
4870fd60a36SBruce A. Mah 	/* error */
4880fd60a36SBruce A. Mah 	return rc;
489d6598603SBruce A. Mah     /*
4900fd60a36SBruce A. Mah      * If the socket buffer was too small, but it was the default
4910fd60a36SBruce A. Mah      * size, then try explicitly setting it to something larger.
49272ac83e7SBruce A. Mah      */
4930fd60a36SBruce A. Mah     if (rc > 0) {
4940fd60a36SBruce A. Mah 	if (test->settings->socket_bufsize == 0) {
4951ff12f4cSBruce A. Mah 	    int bufsize = test->settings->blksize + UDP_BUFFER_EXTRA;
4960fd60a36SBruce A. Mah 	    printf("Increasing socket buffer size to %d\n",
4971ff12f4cSBruce A. Mah 		bufsize);
4981ff12f4cSBruce A. Mah 	    test->settings->socket_bufsize = bufsize;
4990fd60a36SBruce A. Mah 	    rc = iperf_udp_buffercheck(test, s);
5000fd60a36SBruce A. Mah 	    if (rc < 0)
5010fd60a36SBruce A. Mah 		return rc;
50272ac83e7SBruce A. Mah 	}
503d2202ee3SBruce A. Mah     }
50472ac83e7SBruce A. Mah 
5059915746aSBruce A. Mah #if defined(HAVE_SO_MAX_PACING_RATE)
5069915746aSBruce A. Mah     /* If socket pacing is available and not disabled, try it. */
507a094d9feSBruce A. Mah     if (test->settings->fqrate) {
5089915746aSBruce A. Mah 	/* Convert bits per second to bytes per second */
509a094d9feSBruce A. Mah 	unsigned int fqrate = test->settings->fqrate / 8;
510a094d9feSBruce A. Mah 	if (fqrate > 0) {
5119915746aSBruce A. Mah 	    if (test->debug) {
512a094d9feSBruce A. Mah 		printf("Setting fair-queue socket pacing to %u\n", fqrate);
5139915746aSBruce A. Mah 	    }
514a094d9feSBruce A. Mah 	    if (setsockopt(s, SOL_SOCKET, SO_MAX_PACING_RATE, &fqrate, sizeof(fqrate)) < 0) {
515a094d9feSBruce A. Mah 		warning("Unable to set socket pacing");
5169915746aSBruce A. Mah 	    }
5179915746aSBruce A. Mah 	}
5189915746aSBruce A. Mah     }
5199915746aSBruce A. Mah #endif /* HAVE_SO_MAX_PACING_RATE */
520a094d9feSBruce A. Mah     {
521a621cc26SBruce A. Mah 	unsigned int rate = test->settings->rate / 8;
522a621cc26SBruce A. Mah 	if (rate > 0) {
523a621cc26SBruce A. Mah 	    if (test->debug) {
524a621cc26SBruce A. Mah 		printf("Setting application pacing to %u\n", rate);
525a621cc26SBruce A. Mah 	    }
526a621cc26SBruce A. Mah 	}
527a621cc26SBruce A. Mah     }
5289915746aSBruce A. Mah 
529b8379188SHasso Tepper #ifdef SO_RCVTIMEO
5309b9dd030SHasso Tepper     /* 30 sec timeout for a case when there is a network problem. */
5319b9dd030SHasso Tepper     tv.tv_sec = 30;
5329b9dd030SHasso Tepper     tv.tv_usec = 0;
5339b9dd030SHasso Tepper     setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (struct timeval *)&tv, sizeof(struct timeval));
534b8379188SHasso Tepper #endif
5359b9dd030SHasso Tepper 
53672ac83e7SBruce A. Mah     /*
537d6598603SBruce A. Mah      * Write a datagram to the UDP stream to let the server know we're here.
538d6598603SBruce A. Mah      * The server learns our address by obtaining its peer's address.
539d6598603SBruce A. Mah      */
540d6598603SBruce A. Mah     buf = 123456789;		/* this can be pretty much anything */
5418a0b5a5dSsethdelliott     if (write(s, &buf, sizeof(buf)) < 0) {
5428a0b5a5dSsethdelliott         // XXX: Should this be changed to IESTREAMCONNECT?
5438a0b5a5dSsethdelliott         i_errno = IESTREAMWRITE;
544ec2d0670SJef Poskanzer         return -1;
5458a0b5a5dSsethdelliott     }
546111645edSJef Poskanzer 
547d6598603SBruce A. Mah     /*
548d6598603SBruce A. Mah      * Wait until the server replies back to us.
549d6598603SBruce A. Mah      */
550111645edSJef Poskanzer     if ((sz = recv(s, &buf, sizeof(buf), 0)) < 0) {
5518a0b5a5dSsethdelliott         i_errno = IESTREAMREAD;
552ec2d0670SJef Poskanzer         return -1;
5538a0b5a5dSsethdelliott     }
5548a0b5a5dSsethdelliott 
555ec2d0670SJef Poskanzer     return s;
5568a0b5a5dSsethdelliott }
5578a0b5a5dSsethdelliott 
5588a0b5a5dSsethdelliott 
5598a0b5a5dSsethdelliott /* iperf_udp_init
5608a0b5a5dSsethdelliott  *
5618a0b5a5dSsethdelliott  * initializer for UDP streams in TEST_START
5628a0b5a5dSsethdelliott  */
5638a0b5a5dSsethdelliott int
5648a0b5a5dSsethdelliott iperf_udp_init(struct iperf_test *test)
5658a0b5a5dSsethdelliott {
566ec2d0670SJef Poskanzer     return 0;
567a951c980SBrian Tierney }
568