xref: /iperf/src/iperf_udp.c (revision 1ff12f4c)
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"
48329523a5SBruce A. Mah #include "portable_endian.h"
49a951c980SBrian Tierney 
508a0b5a5dSsethdelliott /* iperf_udp_recv
51a951c980SBrian Tierney  *
528a0b5a5dSsethdelliott  * receives the data for UDP
53a951c980SBrian Tierney  */
54a951c980SBrian Tierney int
55a951c980SBrian Tierney iperf_udp_recv(struct iperf_stream *sp)
56a951c980SBrian Tierney {
57329523a5SBruce A. Mah     uint32_t  sec, usec;
58329523a5SBruce A. Mah     uint64_t  pcount;
5943929b36SJef Poskanzer     int       r;
60a951c980SBrian Tierney     int       size = sp->settings->blksize;
61a951c980SBrian Tierney     double    transit = 0, d = 0;
628556db5dSsethdelliott     struct timeval sent_time, arrival_time;
63a951c980SBrian Tierney 
6443929b36SJef Poskanzer     r = Nread(sp->socket, sp->buffer, size, Pudp);
65a951c980SBrian Tierney 
668694d1dbSBruce A. Mah     /*
678694d1dbSBruce A. Mah      * If we got an error in the read, or if we didn't read anything
688694d1dbSBruce A. Mah      * because the underlying read(2) got a EAGAIN, then skip packet
698694d1dbSBruce A. Mah      * processing.
708694d1dbSBruce A. Mah      */
718694d1dbSBruce A. Mah     if (r <= 0)
7243929b36SJef Poskanzer         return r;
738a0b5a5dSsethdelliott 
7443929b36SJef Poskanzer     sp->result->bytes_received += r;
7543929b36SJef Poskanzer     sp->result->bytes_received_this_interval += r;
768556db5dSsethdelliott 
77329523a5SBruce A. Mah     if (sp->test->udp_counters_64bit) {
788556db5dSsethdelliott 	memcpy(&sec, sp->buffer, sizeof(sec));
798556db5dSsethdelliott 	memcpy(&usec, sp->buffer+4, sizeof(usec));
808556db5dSsethdelliott 	memcpy(&pcount, sp->buffer+8, sizeof(pcount));
818556db5dSsethdelliott 	sec = ntohl(sec);
828556db5dSsethdelliott 	usec = ntohl(usec);
83329523a5SBruce A. Mah 	pcount = be64toh(pcount);
848556db5dSsethdelliott 	sent_time.tv_sec = sec;
858556db5dSsethdelliott 	sent_time.tv_usec = usec;
86329523a5SBruce A. Mah     }
87329523a5SBruce A. Mah     else {
88329523a5SBruce A. Mah 	uint32_t pc;
89329523a5SBruce A. Mah 	memcpy(&sec, sp->buffer, sizeof(sec));
90329523a5SBruce A. Mah 	memcpy(&usec, sp->buffer+4, sizeof(usec));
91329523a5SBruce A. Mah 	memcpy(&pc, sp->buffer+8, sizeof(pc));
92329523a5SBruce A. Mah 	sec = ntohl(sec);
93329523a5SBruce A. Mah 	usec = ntohl(usec);
94329523a5SBruce A. Mah 	pcount = ntohl(pc);
95329523a5SBruce A. Mah 	sent_time.tv_sec = sec;
96329523a5SBruce A. Mah 	sent_time.tv_usec = usec;
97329523a5SBruce A. Mah     }
988556db5dSsethdelliott 
998556db5dSsethdelliott     /* Out of order packets */
1008556db5dSsethdelliott     if (pcount >= sp->packet_count + 1) {
1018556db5dSsethdelliott         if (pcount > sp->packet_count + 1) {
1028556db5dSsethdelliott             sp->cnt_error += (pcount - 1) - sp->packet_count;
1038556db5dSsethdelliott         }
1048556db5dSsethdelliott         sp->packet_count = pcount;
1058556db5dSsethdelliott     } else {
1068556db5dSsethdelliott         sp->outoforder_packets++;
1079fc0149eSBruce A. Mah 	iperf_err(sp->test, "OUT OF ORDER - incoming packet = %zu and received packet = %d AND SP = %d", pcount, sp->packet_count, sp->socket);
1088556db5dSsethdelliott     }
109a951c980SBrian Tierney 
110a951c980SBrian Tierney     /* jitter measurement */
111b60a49ddSsethdelliott     gettimeofday(&arrival_time, NULL);
112b60a49ddSsethdelliott 
1138556db5dSsethdelliott     transit = timeval_diff(&sent_time, &arrival_time);
114a951c980SBrian Tierney     d = transit - sp->prev_transit;
115a951c980SBrian Tierney     if (d < 0)
116a951c980SBrian Tierney         d = -d;
117a951c980SBrian Tierney     sp->prev_transit = transit;
1188556db5dSsethdelliott     // XXX: This is NOT the way to calculate jitter
1198556db5dSsethdelliott     //      J = |(R1 - S1) - (R0 - S0)| [/ number of packets, for average]
120a951c980SBrian Tierney     sp->jitter += (d - sp->jitter) / 16.0;
121a951c980SBrian Tierney 
122329523a5SBruce A. Mah     if (sp->test->debug) {
1239fc0149eSBruce A. Mah 	fprintf(stderr, "packet_count %d\n", sp->packet_count);
124329523a5SBruce A. Mah     }
125329523a5SBruce A. Mah 
12643929b36SJef Poskanzer     return r;
127ec2d0670SJef Poskanzer }
128ec2d0670SJef Poskanzer 
129ec2d0670SJef Poskanzer 
1308a0b5a5dSsethdelliott /* iperf_udp_send
1318a0b5a5dSsethdelliott  *
1328a0b5a5dSsethdelliott  * sends the data for UDP
1338a0b5a5dSsethdelliott  */
134a951c980SBrian Tierney int
135a951c980SBrian Tierney iperf_udp_send(struct iperf_stream *sp)
136a951c980SBrian Tierney {
13743929b36SJef Poskanzer     int r;
1388556db5dSsethdelliott     int       size = sp->settings->blksize;
139647d3b95SJef Poskanzer     struct timeval before;
14043929b36SJef Poskanzer 
141b60a49ddSsethdelliott     gettimeofday(&before, 0);
142b60a49ddSsethdelliott 
1438556db5dSsethdelliott     ++sp->packet_count;
144329523a5SBruce A. Mah 
145329523a5SBruce A. Mah     if (sp->test->udp_counters_64bit) {
146329523a5SBruce A. Mah 
147329523a5SBruce A. Mah 	uint32_t  sec, usec;
148329523a5SBruce A. Mah 	uint64_t  pcount;
149329523a5SBruce A. Mah 
150329523a5SBruce A. Mah 	sec = htonl(before.tv_sec);
151329523a5SBruce A. Mah 	usec = htonl(before.tv_usec);
152329523a5SBruce A. Mah 	pcount = htobe64(sp->packet_count);
153329523a5SBruce A. Mah 
154329523a5SBruce A. Mah 	memcpy(sp->buffer, &sec, sizeof(sec));
155329523a5SBruce A. Mah 	memcpy(sp->buffer+4, &usec, sizeof(usec));
156329523a5SBruce A. Mah 	memcpy(sp->buffer+8, &pcount, sizeof(pcount));
157329523a5SBruce A. Mah 
158329523a5SBruce A. Mah     }
159329523a5SBruce A. Mah     else {
160329523a5SBruce A. Mah 
161329523a5SBruce A. Mah 	uint32_t  sec, usec, pcount;
162329523a5SBruce A. Mah 
1638556db5dSsethdelliott 	sec = htonl(before.tv_sec);
1648556db5dSsethdelliott 	usec = htonl(before.tv_usec);
1658556db5dSsethdelliott 	pcount = htonl(sp->packet_count);
166a951c980SBrian Tierney 
1678556db5dSsethdelliott 	memcpy(sp->buffer, &sec, sizeof(sec));
1688556db5dSsethdelliott 	memcpy(sp->buffer+4, &usec, sizeof(usec));
1698556db5dSsethdelliott 	memcpy(sp->buffer+8, &pcount, sizeof(pcount));
170a951c980SBrian Tierney 
171329523a5SBruce A. Mah     }
172329523a5SBruce A. Mah 
17343929b36SJef Poskanzer     r = Nwrite(sp->socket, sp->buffer, size, Pudp);
174a951c980SBrian Tierney 
17543929b36SJef Poskanzer     if (r < 0)
17643929b36SJef Poskanzer 	return r;
1778556db5dSsethdelliott 
17843929b36SJef Poskanzer     sp->result->bytes_sent += r;
17943929b36SJef Poskanzer     sp->result->bytes_sent_this_interval += r;
180aa89cda2Ssethdelliott 
18143929b36SJef Poskanzer     return r;
182a951c980SBrian Tierney }
183a951c980SBrian Tierney 
184a951c980SBrian Tierney 
185a951c980SBrian Tierney /**************************************************************************/
186a951c980SBrian Tierney 
187d6598603SBruce A. Mah /*
188d6598603SBruce A. Mah  * The following functions all have to do with managing UDP data sockets.
189d6598603SBruce A. Mah  * UDP of course is connectionless, so there isn't really a concept of
190d6598603SBruce A. Mah  * setting up a connection, although connect(2) can (and is) used to
191d6598603SBruce A. Mah  * bind the remote end of sockets.  We need to simulate some of the
192d6598603SBruce A. Mah  * connection management that is built-in to TCP so that each side of the
193d6598603SBruce A. Mah  * connection knows about each other before the real data transfers begin.
194d6598603SBruce A. Mah  */
195d6598603SBruce A. Mah 
196d6598603SBruce A. Mah /*
1970fd60a36SBruce A. Mah  * Set and verify socket buffer sizes.
1980fd60a36SBruce A. Mah  * Return 0 if no error, -1 if an error, +1 if socket buffers are
1990fd60a36SBruce A. Mah  * potentially too small to hold a message.
200a951c980SBrian Tierney  */
201a951c980SBrian Tierney int
2020fd60a36SBruce A. Mah iperf_udp_buffercheck(struct iperf_test *test, int s)
203a951c980SBrian Tierney {
2040fd60a36SBruce A. Mah     int rc = 0;
205a951c980SBrian Tierney 
206d6598603SBruce A. Mah     /*
20772ac83e7SBruce A. Mah      * Set socket buffer size if requested.  Do this for both sending and
20872ac83e7SBruce A. Mah      * receiving so that we can cover both normal and --reverse operation.
20972ac83e7SBruce A. Mah      */
21072ac83e7SBruce A. Mah     int opt;
211d2202ee3SBruce A. Mah     socklen_t optlen;
212d2202ee3SBruce A. Mah 
21372ac83e7SBruce A. Mah     if ((opt = test->settings->socket_bufsize)) {
21472ac83e7SBruce A. Mah         if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt)) < 0) {
21572ac83e7SBruce A. Mah             i_errno = IESETBUF;
21672ac83e7SBruce A. Mah             return -1;
21772ac83e7SBruce A. Mah         }
21872ac83e7SBruce A. Mah         if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &opt, sizeof(opt)) < 0) {
21972ac83e7SBruce A. Mah             i_errno = IESETBUF;
22072ac83e7SBruce A. Mah             return -1;
22172ac83e7SBruce A. Mah         }
22272ac83e7SBruce A. Mah     }
22372ac83e7SBruce A. Mah 
224d2202ee3SBruce A. Mah     /* Read back and verify the sender socket buffer size */
225d2202ee3SBruce A. Mah     optlen = sizeof(opt);
226d2202ee3SBruce A. Mah     if (getsockopt(s, SOL_SOCKET, SO_SNDBUF, &opt, &optlen) < 0) {
227d2202ee3SBruce A. Mah 	i_errno = IESETBUF;
228d2202ee3SBruce A. Mah 	return -1;
229d2202ee3SBruce A. Mah     }
23055598a07SBruce A. Mah     if (test->debug) {
23155598a07SBruce A. Mah 	printf("SNDBUF is %u, expecting %u\n", opt, test->settings->socket_bufsize);
23255598a07SBruce A. Mah     }
23355598a07SBruce A. Mah     if (test->settings->socket_bufsize && test->settings->socket_bufsize > opt) {
234d2202ee3SBruce A. Mah 	i_errno = IESETBUF2;
235d2202ee3SBruce A. Mah 	return -1;
236d2202ee3SBruce A. Mah     }
2370fd60a36SBruce A. Mah     if (test->settings->blksize > opt) {
2380fd60a36SBruce A. Mah 	char str[80];
2390fd60a36SBruce A. Mah 	snprintf(str, sizeof(str),
2400fd60a36SBruce A. Mah 		 "Block size %d > sending socket buffer size %d",
2410fd60a36SBruce A. Mah 		 test->settings->blksize, opt);
2420fd60a36SBruce A. Mah 	warning(str);
2430fd60a36SBruce A. Mah 	rc = 1;
2440fd60a36SBruce A. Mah     }
245d2202ee3SBruce A. Mah 
246d2202ee3SBruce A. Mah     /* Read back and verify the receiver socket buffer size */
247d2202ee3SBruce A. Mah     optlen = sizeof(opt);
248d2202ee3SBruce A. Mah     if (getsockopt(s, SOL_SOCKET, SO_RCVBUF, &opt, &optlen) < 0) {
249d2202ee3SBruce A. Mah 	i_errno = IESETBUF;
250d2202ee3SBruce A. Mah 	return -1;
251d2202ee3SBruce A. Mah     }
25255598a07SBruce A. Mah     if (test->debug) {
25355598a07SBruce A. Mah 	printf("RCVBUF is %u, expecting %u\n", opt, test->settings->socket_bufsize);
25455598a07SBruce A. Mah     }
25555598a07SBruce A. Mah     if (test->settings->socket_bufsize && test->settings->socket_bufsize > opt) {
256d2202ee3SBruce A. Mah 	i_errno = IESETBUF2;
257d2202ee3SBruce A. Mah 	return -1;
258d2202ee3SBruce A. Mah     }
2590fd60a36SBruce A. Mah     if (test->settings->blksize > opt) {
2600fd60a36SBruce A. Mah 	char str[80];
2610fd60a36SBruce A. Mah 	snprintf(str, sizeof(str),
2620fd60a36SBruce A. Mah 		 "Block size %d > receiving socket buffer size %d",
2630fd60a36SBruce A. Mah 		 test->settings->blksize, opt);
2640fd60a36SBruce A. Mah 	warning(str);
2650fd60a36SBruce A. Mah 	rc = 1;
2660fd60a36SBruce A. Mah     }
2670fd60a36SBruce A. Mah     return rc;
2680fd60a36SBruce A. Mah }
2690fd60a36SBruce A. Mah 
2700fd60a36SBruce A. Mah /*
2710fd60a36SBruce A. Mah  * iperf_udp_accept
2720fd60a36SBruce A. Mah  *
2730fd60a36SBruce A. Mah  * Accepts a new UDP "connection"
2740fd60a36SBruce A. Mah  */
2750fd60a36SBruce A. Mah int
2760fd60a36SBruce A. Mah iperf_udp_accept(struct iperf_test *test)
2770fd60a36SBruce A. Mah {
2780fd60a36SBruce A. Mah     struct sockaddr_storage sa_peer;
2790fd60a36SBruce A. Mah     int       buf;
2800fd60a36SBruce A. Mah     socklen_t len;
2810fd60a36SBruce A. Mah     int       sz, s;
2820fd60a36SBruce A. Mah     int	      rc;
2830fd60a36SBruce A. Mah 
2840fd60a36SBruce A. Mah     /*
2850fd60a36SBruce A. Mah      * Get the current outstanding socket.  This socket will be used to handle
2860fd60a36SBruce A. Mah      * data transfers and a new "listening" socket will be created.
2870fd60a36SBruce A. Mah      */
2880fd60a36SBruce A. Mah     s = test->prot_listener;
2890fd60a36SBruce A. Mah 
2900fd60a36SBruce A. Mah     /*
2910fd60a36SBruce A. Mah      * Grab the UDP packet sent by the client.  From that we can extract the
2920fd60a36SBruce A. Mah      * client's address, and then use that information to bind the remote side
2930fd60a36SBruce A. Mah      * of the socket to the client.
2940fd60a36SBruce A. Mah      */
2950fd60a36SBruce A. Mah     len = sizeof(sa_peer);
2960fd60a36SBruce A. Mah     if ((sz = recvfrom(test->prot_listener, &buf, sizeof(buf), 0, (struct sockaddr *) &sa_peer, &len)) < 0) {
2970fd60a36SBruce A. Mah         i_errno = IESTREAMACCEPT;
2980fd60a36SBruce A. Mah         return -1;
2990fd60a36SBruce A. Mah     }
3000fd60a36SBruce A. Mah 
3010fd60a36SBruce A. Mah     if (connect(s, (struct sockaddr *) &sa_peer, len) < 0) {
3020fd60a36SBruce A. Mah         i_errno = IESTREAMACCEPT;
3030fd60a36SBruce A. Mah         return -1;
3040fd60a36SBruce A. Mah     }
3050fd60a36SBruce A. Mah 
3060fd60a36SBruce A. Mah     /* Check and set socket buffer sizes */
3070fd60a36SBruce A. Mah     rc = iperf_udp_buffercheck(test, s);
3080fd60a36SBruce A. Mah     if (rc < 0)
3090fd60a36SBruce A. Mah 	/* error */
3100fd60a36SBruce A. Mah 	return rc;
3110fd60a36SBruce A. Mah     /*
3120fd60a36SBruce A. Mah      * If the socket buffer was too small, but it was the default
3130fd60a36SBruce A. Mah      * size, then try explicitly setting it to something larger.
3140fd60a36SBruce A. Mah      */
3150fd60a36SBruce A. Mah     if (rc > 0) {
3160fd60a36SBruce A. Mah 	if (test->settings->socket_bufsize == 0) {
317*1ff12f4cSBruce A. Mah 	    int bufsize = test->settings->blksize + UDP_BUFFER_EXTRA;
3180fd60a36SBruce A. Mah 	    printf("Increasing socket buffer size to %d\n",
319*1ff12f4cSBruce A. Mah 		bufsize);
320*1ff12f4cSBruce A. Mah 	    test->settings->socket_bufsize = bufsize;
3210fd60a36SBruce A. Mah 	    rc = iperf_udp_buffercheck(test, s);
3220fd60a36SBruce A. Mah 	    if (rc < 0)
3230fd60a36SBruce A. Mah 		return rc;
3240fd60a36SBruce A. Mah 	}
3250fd60a36SBruce A. Mah     }
326d2202ee3SBruce A. Mah 
3279915746aSBruce A. Mah #if defined(HAVE_SO_MAX_PACING_RATE)
328a094d9feSBruce A. Mah     /* If socket pacing is specified, try it. */
329a094d9feSBruce A. Mah     if (test->settings->fqrate) {
3309915746aSBruce A. Mah 	/* Convert bits per second to bytes per second */
331a094d9feSBruce A. Mah 	unsigned int fqrate = test->settings->fqrate / 8;
332a094d9feSBruce A. Mah 	if (fqrate > 0) {
3339915746aSBruce A. Mah 	    if (test->debug) {
334a094d9feSBruce A. Mah 		printf("Setting fair-queue socket pacing to %u\n", fqrate);
3359915746aSBruce A. Mah 	    }
336a094d9feSBruce A. Mah 	    if (setsockopt(s, SOL_SOCKET, SO_MAX_PACING_RATE, &fqrate, sizeof(fqrate)) < 0) {
337a094d9feSBruce A. Mah 		warning("Unable to set socket pacing");
3389915746aSBruce A. Mah 	    }
3399915746aSBruce A. Mah 	}
3409915746aSBruce A. Mah     }
3419915746aSBruce A. Mah #endif /* HAVE_SO_MAX_PACING_RATE */
342a094d9feSBruce A. Mah     {
343a621cc26SBruce A. Mah 	unsigned int rate = test->settings->rate / 8;
344a621cc26SBruce A. Mah 	if (rate > 0) {
345a621cc26SBruce A. Mah 	    if (test->debug) {
346a621cc26SBruce A. Mah 		printf("Setting application pacing to %u\n", rate);
347a621cc26SBruce A. Mah 	    }
348a621cc26SBruce A. Mah 	}
349a621cc26SBruce A. Mah     }
3509915746aSBruce A. Mah 
35172ac83e7SBruce A. Mah     /*
352d6598603SBruce A. Mah      * Create a new "listening" socket to replace the one we were using before.
353d6598603SBruce A. Mah      */
354f99bd3b2Ssethdelliott     test->prot_listener = netannounce(test->settings->domain, Pudp, test->bind_address, test->server_port);
3558a0b5a5dSsethdelliott     if (test->prot_listener < 0) {
3568a0b5a5dSsethdelliott         i_errno = IESTREAMLISTEN;
357ec2d0670SJef Poskanzer         return -1;
358b60a49ddSsethdelliott     }
359a951c980SBrian Tierney 
3608a0b5a5dSsethdelliott     FD_SET(test->prot_listener, &test->read_set);
3618a0b5a5dSsethdelliott     test->max_fd = (test->max_fd < test->prot_listener) ? test->prot_listener : test->max_fd;
362a951c980SBrian Tierney 
3638556db5dSsethdelliott     /* Let the client know we're ready "accept" another UDP "stream" */
364d6598603SBruce A. Mah     buf = 987654321;		/* any content will work here */
3658a0b5a5dSsethdelliott     if (write(s, &buf, sizeof(buf)) < 0) {
366b60a49ddSsethdelliott         i_errno = IESTREAMWRITE;
367ec2d0670SJef Poskanzer         return -1;
368a951c980SBrian Tierney     }
369a951c980SBrian Tierney 
370ec2d0670SJef Poskanzer     return s;
3718a0b5a5dSsethdelliott }
3728a0b5a5dSsethdelliott 
3738a0b5a5dSsethdelliott 
374d6598603SBruce A. Mah /*
375d6598603SBruce A. Mah  * iperf_udp_listen
3768a0b5a5dSsethdelliott  *
377d6598603SBruce A. Mah  * Start up a listener for UDP stream connections.  Unlike for TCP,
378d6598603SBruce A. Mah  * there is no listen(2) for UDP.  This socket will however accept
379d6598603SBruce A. Mah  * a UDP datagram from a client (indicating the client's presence).
3808a0b5a5dSsethdelliott  */
3818a0b5a5dSsethdelliott int
3828a0b5a5dSsethdelliott iperf_udp_listen(struct iperf_test *test)
3838a0b5a5dSsethdelliott {
3848a0b5a5dSsethdelliott     int s;
3858a0b5a5dSsethdelliott 
386f99bd3b2Ssethdelliott     if ((s = netannounce(test->settings->domain, Pudp, test->bind_address, test->server_port)) < 0) {
3878a0b5a5dSsethdelliott         i_errno = IESTREAMLISTEN;
388ec2d0670SJef Poskanzer         return -1;
3898a0b5a5dSsethdelliott     }
3908a0b5a5dSsethdelliott 
391d6598603SBruce A. Mah     /*
392d6598603SBruce A. Mah      * The caller will put this value into test->prot_listener.
393d6598603SBruce A. Mah      */
394ec2d0670SJef Poskanzer     return s;
3958a0b5a5dSsethdelliott }
3968a0b5a5dSsethdelliott 
3978a0b5a5dSsethdelliott 
398d6598603SBruce A. Mah /*
399d6598603SBruce A. Mah  * iperf_udp_connect
4008a0b5a5dSsethdelliott  *
401d6598603SBruce A. Mah  * "Connect" to a UDP stream listener.
4028a0b5a5dSsethdelliott  */
4038a0b5a5dSsethdelliott int
4048a0b5a5dSsethdelliott iperf_udp_connect(struct iperf_test *test)
4058a0b5a5dSsethdelliott {
406111645edSJef Poskanzer     int s, buf, sz;
407b8379188SHasso Tepper #ifdef SO_RCVTIMEO
4089b9dd030SHasso Tepper     struct timeval tv;
409b8379188SHasso Tepper #endif
4100fd60a36SBruce A. Mah     int rc;
4118a0b5a5dSsethdelliott 
412d6598603SBruce A. Mah     /* Create and bind our local socket. */
413023fb45eSKevin Constantine     if ((s = netdial(test->settings->domain, Pudp, test->bind_address, test->bind_port, test->server_hostname, test->server_port)) < 0) {
4148a0b5a5dSsethdelliott         i_errno = IESTREAMCONNECT;
415ec2d0670SJef Poskanzer         return -1;
4168a0b5a5dSsethdelliott     }
4178a0b5a5dSsethdelliott 
4180fd60a36SBruce A. Mah     /* Check and set socket buffer sizes */
4190fd60a36SBruce A. Mah     rc = iperf_udp_buffercheck(test, s);
4200fd60a36SBruce A. Mah     if (rc < 0)
4210fd60a36SBruce A. Mah 	/* error */
4220fd60a36SBruce A. Mah 	return rc;
423d6598603SBruce A. Mah     /*
4240fd60a36SBruce A. Mah      * If the socket buffer was too small, but it was the default
4250fd60a36SBruce A. Mah      * size, then try explicitly setting it to something larger.
42672ac83e7SBruce A. Mah      */
4270fd60a36SBruce A. Mah     if (rc > 0) {
4280fd60a36SBruce A. Mah 	if (test->settings->socket_bufsize == 0) {
429*1ff12f4cSBruce A. Mah 	    int bufsize = test->settings->blksize + UDP_BUFFER_EXTRA;
4300fd60a36SBruce A. Mah 	    printf("Increasing socket buffer size to %d\n",
431*1ff12f4cSBruce A. Mah 		bufsize);
432*1ff12f4cSBruce A. Mah 	    test->settings->socket_bufsize = bufsize;
4330fd60a36SBruce A. Mah 	    rc = iperf_udp_buffercheck(test, s);
4340fd60a36SBruce A. Mah 	    if (rc < 0)
4350fd60a36SBruce A. Mah 		return rc;
43672ac83e7SBruce A. Mah 	}
437d2202ee3SBruce A. Mah     }
43872ac83e7SBruce A. Mah 
4399915746aSBruce A. Mah #if defined(HAVE_SO_MAX_PACING_RATE)
4409915746aSBruce A. Mah     /* If socket pacing is available and not disabled, try it. */
441a094d9feSBruce A. Mah     if (test->settings->fqrate) {
4429915746aSBruce A. Mah 	/* Convert bits per second to bytes per second */
443a094d9feSBruce A. Mah 	unsigned int fqrate = test->settings->fqrate / 8;
444a094d9feSBruce A. Mah 	if (fqrate > 0) {
4459915746aSBruce A. Mah 	    if (test->debug) {
446a094d9feSBruce A. Mah 		printf("Setting fair-queue socket pacing to %u\n", fqrate);
4479915746aSBruce A. Mah 	    }
448a094d9feSBruce A. Mah 	    if (setsockopt(s, SOL_SOCKET, SO_MAX_PACING_RATE, &fqrate, sizeof(fqrate)) < 0) {
449a094d9feSBruce A. Mah 		warning("Unable to set socket pacing");
4509915746aSBruce A. Mah 	    }
4519915746aSBruce A. Mah 	}
4529915746aSBruce A. Mah     }
4539915746aSBruce A. Mah #endif /* HAVE_SO_MAX_PACING_RATE */
454a094d9feSBruce A. Mah     {
455a621cc26SBruce A. Mah 	unsigned int rate = test->settings->rate / 8;
456a621cc26SBruce A. Mah 	if (rate > 0) {
457a621cc26SBruce A. Mah 	    if (test->debug) {
458a621cc26SBruce A. Mah 		printf("Setting application pacing to %u\n", rate);
459a621cc26SBruce A. Mah 	    }
460a621cc26SBruce A. Mah 	}
461a621cc26SBruce A. Mah     }
4629915746aSBruce A. Mah 
463b8379188SHasso Tepper #ifdef SO_RCVTIMEO
4649b9dd030SHasso Tepper     /* 30 sec timeout for a case when there is a network problem. */
4659b9dd030SHasso Tepper     tv.tv_sec = 30;
4669b9dd030SHasso Tepper     tv.tv_usec = 0;
4679b9dd030SHasso Tepper     setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (struct timeval *)&tv, sizeof(struct timeval));
468b8379188SHasso Tepper #endif
4699b9dd030SHasso Tepper 
47072ac83e7SBruce A. Mah     /*
471d6598603SBruce A. Mah      * Write a datagram to the UDP stream to let the server know we're here.
472d6598603SBruce A. Mah      * The server learns our address by obtaining its peer's address.
473d6598603SBruce A. Mah      */
474d6598603SBruce A. Mah     buf = 123456789;		/* this can be pretty much anything */
4758a0b5a5dSsethdelliott     if (write(s, &buf, sizeof(buf)) < 0) {
4768a0b5a5dSsethdelliott         // XXX: Should this be changed to IESTREAMCONNECT?
4778a0b5a5dSsethdelliott         i_errno = IESTREAMWRITE;
478ec2d0670SJef Poskanzer         return -1;
4798a0b5a5dSsethdelliott     }
480111645edSJef Poskanzer 
481d6598603SBruce A. Mah     /*
482d6598603SBruce A. Mah      * Wait until the server replies back to us.
483d6598603SBruce A. Mah      */
484111645edSJef Poskanzer     if ((sz = recv(s, &buf, sizeof(buf), 0)) < 0) {
4858a0b5a5dSsethdelliott         i_errno = IESTREAMREAD;
486ec2d0670SJef Poskanzer         return -1;
4878a0b5a5dSsethdelliott     }
4888a0b5a5dSsethdelliott 
489ec2d0670SJef Poskanzer     return s;
4908a0b5a5dSsethdelliott }
4918a0b5a5dSsethdelliott 
4928a0b5a5dSsethdelliott 
4938a0b5a5dSsethdelliott /* iperf_udp_init
4948a0b5a5dSsethdelliott  *
4958a0b5a5dSsethdelliott  * initializer for UDP streams in TEST_START
4968a0b5a5dSsethdelliott  */
4978a0b5a5dSsethdelliott int
4988a0b5a5dSsethdelliott iperf_udp_init(struct iperf_test *test)
4998a0b5a5dSsethdelliott {
500ec2d0670SJef Poskanzer     return 0;
501a951c980SBrian Tierney }
502