1a951c980SBrian Tierney /*
276bd67f6SSarah Larsen * iperf, Copyright (c) 2014-2022, 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>
33666040bdSXiang Xiao #include <arpa/inet.h>
34a951c980SBrian Tierney #include <sys/socket.h>
35a951c980SBrian Tierney #include <sys/types.h>
36a951c980SBrian Tierney #include <netinet/in.h>
37426221a3SBruce A. Mah #ifdef HAVE_STDINT_H
38a951c980SBrian Tierney #include <stdint.h>
39426221a3SBruce A. Mah #endif
40a951c980SBrian Tierney #include <sys/time.h>
4166ce7ad4Ssethdelliott #include <sys/select.h>
42a951c980SBrian Tierney
43a951c980SBrian Tierney #include "iperf.h"
44a951c980SBrian Tierney #include "iperf_api.h"
459673370fSJef Poskanzer #include "iperf_util.h"
46a951c980SBrian Tierney #include "iperf_udp.h"
47a951c980SBrian Tierney #include "timer.h"
48a951c980SBrian Tierney #include "net.h"
49e73821cdSBruce A. Mah #include "cjson.h"
50329523a5SBruce A. Mah #include "portable_endian.h"
51a951c980SBrian Tierney
52d88f4cecSPhilip Prindeville #if defined(HAVE_INTTYPES_H)
53d88f4cecSPhilip Prindeville # include <inttypes.h>
54d88f4cecSPhilip Prindeville #else
55223da980SBruce A. Mah # ifndef PRIu64
56223da980SBruce A. Mah # if sizeof(long) == 8
57223da980SBruce A. Mah # define PRIu64 "lu"
58223da980SBruce A. Mah # else
59d88f4cecSPhilip Prindeville # define PRIu64 "llu"
60d88f4cecSPhilip Prindeville # endif
61223da980SBruce A. Mah # endif
62223da980SBruce A. Mah #endif
63d88f4cecSPhilip Prindeville
648a0b5a5dSsethdelliott /* iperf_udp_recv
65a951c980SBrian Tierney *
668a0b5a5dSsethdelliott * receives the data for UDP
67a951c980SBrian Tierney */
68a951c980SBrian Tierney int
iperf_udp_recv(struct iperf_stream * sp)69a951c980SBrian Tierney iperf_udp_recv(struct iperf_stream *sp)
70a951c980SBrian Tierney {
71329523a5SBruce A. Mah uint32_t sec, usec;
72329523a5SBruce A. Mah uint64_t pcount;
7343929b36SJef Poskanzer int r;
74a951c980SBrian Tierney int size = sp->settings->blksize;
752609dd71SBruce A. Mah int first_packet = 0;
76a951c980SBrian Tierney double transit = 0, d = 0;
77cde81d76SBen Fox-Moore struct iperf_time sent_time, arrival_time, temp_time;
78a951c980SBrian Tierney
7943929b36SJef Poskanzer r = Nread(sp->socket, sp->buffer, size, Pudp);
80a951c980SBrian Tierney
818694d1dbSBruce A. Mah /*
828694d1dbSBruce A. Mah * If we got an error in the read, or if we didn't read anything
838694d1dbSBruce A. Mah * because the underlying read(2) got a EAGAIN, then skip packet
848694d1dbSBruce A. Mah * processing.
858694d1dbSBruce A. Mah */
868694d1dbSBruce A. Mah if (r <= 0)
8743929b36SJef Poskanzer return r;
888a0b5a5dSsethdelliott
89c0055199SBruce A. Mah /* Only count bytes received while we're in the correct state. */
90c0055199SBruce A. Mah if (sp->test->state == TEST_RUNNING) {
912609dd71SBruce A. Mah
922609dd71SBruce A. Mah /*
932609dd71SBruce A. Mah * For jitter computation below, it's important to know if this
942609dd71SBruce A. Mah * packet is the first packet received.
952609dd71SBruce A. Mah */
962609dd71SBruce A. Mah if (sp->result->bytes_received == 0) {
972609dd71SBruce A. Mah first_packet = 1;
982609dd71SBruce A. Mah }
992609dd71SBruce A. Mah
10043929b36SJef Poskanzer sp->result->bytes_received += r;
10143929b36SJef Poskanzer sp->result->bytes_received_this_interval += r;
1028556db5dSsethdelliott
1032609dd71SBruce A. Mah /* Dig the various counters out of the incoming UDP packet */
104329523a5SBruce A. Mah if (sp->test->udp_counters_64bit) {
1058556db5dSsethdelliott memcpy(&sec, sp->buffer, sizeof(sec));
1068556db5dSsethdelliott memcpy(&usec, sp->buffer+4, sizeof(usec));
1078556db5dSsethdelliott memcpy(&pcount, sp->buffer+8, sizeof(pcount));
1088556db5dSsethdelliott sec = ntohl(sec);
1098556db5dSsethdelliott usec = ntohl(usec);
110329523a5SBruce A. Mah pcount = be64toh(pcount);
111cde81d76SBen Fox-Moore sent_time.secs = sec;
112cde81d76SBen Fox-Moore sent_time.usecs = usec;
113329523a5SBruce A. Mah }
114329523a5SBruce A. Mah else {
115329523a5SBruce A. Mah uint32_t pc;
116329523a5SBruce A. Mah memcpy(&sec, sp->buffer, sizeof(sec));
117329523a5SBruce A. Mah memcpy(&usec, sp->buffer+4, sizeof(usec));
118329523a5SBruce A. Mah memcpy(&pc, sp->buffer+8, sizeof(pc));
119329523a5SBruce A. Mah sec = ntohl(sec);
120329523a5SBruce A. Mah usec = ntohl(usec);
121329523a5SBruce A. Mah pcount = ntohl(pc);
122cde81d76SBen Fox-Moore sent_time.secs = sec;
123cde81d76SBen Fox-Moore sent_time.usecs = usec;
124329523a5SBruce A. Mah }
1258556db5dSsethdelliott
126b97df9a9SDavid Bar-On if (sp->test->debug_level >= DEBUG_LEVEL_DEBUG)
127d88f4cecSPhilip Prindeville fprintf(stderr, "pcount %" PRIu64 " packet_count %d\n", pcount, sp->packet_count);
1287f061462SBruce A. Mah
1297f061462SBruce A. Mah /*
1307f061462SBruce A. Mah * Try to handle out of order packets. The way we do this
1317f061462SBruce A. Mah * uses a constant amount of storage but might not be
1327f061462SBruce A. Mah * correct in all cases. In particular we seem to have the
1337f061462SBruce A. Mah * assumption that packets can't be duplicated in the network,
1347f061462SBruce A. Mah * because duplicate packets will possibly cause some problems here.
1357f061462SBruce A. Mah *
1367f061462SBruce A. Mah * First figure out if the sequence numbers are going forward.
1377f061462SBruce A. Mah * Note that pcount is the sequence number read from the packet,
1387f061462SBruce A. Mah * and sp->packet_count is the highest sequence number seen so
1397f061462SBruce A. Mah * far (so we're expecting to see the packet with sequence number
1407f061462SBruce A. Mah * sp->packet_count + 1 arrive next).
1417f061462SBruce A. Mah */
1428556db5dSsethdelliott if (pcount >= sp->packet_count + 1) {
1437f061462SBruce A. Mah
1447f061462SBruce A. Mah /* Forward, but is there a gap in sequence numbers? */
1458556db5dSsethdelliott if (pcount > sp->packet_count + 1) {
1467f061462SBruce A. Mah /* There's a gap so count that as a loss. */
1478556db5dSsethdelliott sp->cnt_error += (pcount - 1) - sp->packet_count;
1488556db5dSsethdelliott }
1497f061462SBruce A. Mah /* Update the highest sequence number seen so far. */
1508556db5dSsethdelliott sp->packet_count = pcount;
1518556db5dSsethdelliott } else {
1527f061462SBruce A. Mah
1537f061462SBruce A. Mah /*
1547f061462SBruce A. Mah * Sequence number went backward (or was stationary?!?).
1557f061462SBruce A. Mah * This counts as an out-of-order packet.
1567f061462SBruce A. Mah */
1578556db5dSsethdelliott sp->outoforder_packets++;
1587f061462SBruce A. Mah
1597f061462SBruce A. Mah /*
1607f061462SBruce A. Mah * If we have lost packets, then the fact that we are now
1617f061462SBruce A. Mah * seeing an out-of-order packet offsets a prior sequence
1627f061462SBruce A. Mah * number gap that was counted as a loss. So we can take
1637f061462SBruce A. Mah * away a loss.
1647f061462SBruce A. Mah */
1657f061462SBruce A. Mah if (sp->cnt_error > 0)
1667f061462SBruce A. Mah sp->cnt_error--;
1677f061462SBruce A. Mah
1687f061462SBruce A. Mah /* Log the out-of-order packet */
1697f061462SBruce A. Mah if (sp->test->debug)
1701521d9b5SBruce A. Mah fprintf(stderr, "OUT OF ORDER - incoming packet sequence %" PRIu64 " but expected sequence %d on stream %d", pcount, sp->packet_count + 1, sp->socket);
1718556db5dSsethdelliott }
172a951c980SBrian Tierney
17367653543SBruce A. Mah /*
17467653543SBruce A. Mah * jitter measurement
17567653543SBruce A. Mah *
17667653543SBruce A. Mah * This computation is based on RFC 1889 (specifically
17767653543SBruce A. Mah * sections 6.3.1 and A.8).
17867653543SBruce A. Mah *
17967653543SBruce A. Mah * Note that synchronized clocks are not required since
18067653543SBruce A. Mah * the source packet delta times are known. Also this
18167653543SBruce A. Mah * computation does not require knowing the round-trip
18267653543SBruce A. Mah * time.
18367653543SBruce A. Mah */
184cde81d76SBen Fox-Moore iperf_time_now(&arrival_time);
185b60a49ddSsethdelliott
186cde81d76SBen Fox-Moore iperf_time_diff(&arrival_time, &sent_time, &temp_time);
187cde81d76SBen Fox-Moore transit = iperf_time_in_secs(&temp_time);
1882609dd71SBruce A. Mah
1892609dd71SBruce A. Mah /* Hack to handle the first packet by initializing prev_transit. */
1902609dd71SBruce A. Mah if (first_packet)
1912609dd71SBruce A. Mah sp->prev_transit = transit;
1922609dd71SBruce A. Mah
193a951c980SBrian Tierney d = transit - sp->prev_transit;
194a951c980SBrian Tierney if (d < 0)
195a951c980SBrian Tierney d = -d;
196a951c980SBrian Tierney sp->prev_transit = transit;
197a951c980SBrian Tierney sp->jitter += (d - sp->jitter) / 16.0;
198c0055199SBruce A. Mah }
199c0055199SBruce A. Mah else {
200c0055199SBruce A. Mah if (sp->test->debug)
201c0055199SBruce A. Mah printf("Late receive, state = %d\n", sp->test->state);
202c0055199SBruce A. Mah }
203a951c980SBrian Tierney
20443929b36SJef Poskanzer return r;
205ec2d0670SJef Poskanzer }
206ec2d0670SJef Poskanzer
207ec2d0670SJef Poskanzer
2088a0b5a5dSsethdelliott /* iperf_udp_send
2098a0b5a5dSsethdelliott *
2108a0b5a5dSsethdelliott * sends the data for UDP
2118a0b5a5dSsethdelliott */
212a951c980SBrian Tierney int
iperf_udp_send(struct iperf_stream * sp)213a951c980SBrian Tierney iperf_udp_send(struct iperf_stream *sp)
214a951c980SBrian Tierney {
21543929b36SJef Poskanzer int r;
2168556db5dSsethdelliott int size = sp->settings->blksize;
217cde81d76SBen Fox-Moore struct iperf_time before;
21843929b36SJef Poskanzer
219cde81d76SBen Fox-Moore iperf_time_now(&before);
220b60a49ddSsethdelliott
2218556db5dSsethdelliott ++sp->packet_count;
222329523a5SBruce A. Mah
223329523a5SBruce A. Mah if (sp->test->udp_counters_64bit) {
224329523a5SBruce A. Mah
225329523a5SBruce A. Mah uint32_t sec, usec;
226329523a5SBruce A. Mah uint64_t pcount;
227329523a5SBruce A. Mah
228cde81d76SBen Fox-Moore sec = htonl(before.secs);
229cde81d76SBen Fox-Moore usec = htonl(before.usecs);
230329523a5SBruce A. Mah pcount = htobe64(sp->packet_count);
231329523a5SBruce A. Mah
232329523a5SBruce A. Mah memcpy(sp->buffer, &sec, sizeof(sec));
233329523a5SBruce A. Mah memcpy(sp->buffer+4, &usec, sizeof(usec));
234329523a5SBruce A. Mah memcpy(sp->buffer+8, &pcount, sizeof(pcount));
235329523a5SBruce A. Mah
236329523a5SBruce A. Mah }
237329523a5SBruce A. Mah else {
238329523a5SBruce A. Mah
239329523a5SBruce A. Mah uint32_t sec, usec, pcount;
240329523a5SBruce A. Mah
241cde81d76SBen Fox-Moore sec = htonl(before.secs);
242cde81d76SBen Fox-Moore usec = htonl(before.usecs);
2438556db5dSsethdelliott pcount = htonl(sp->packet_count);
244a951c980SBrian Tierney
2458556db5dSsethdelliott memcpy(sp->buffer, &sec, sizeof(sec));
2468556db5dSsethdelliott memcpy(sp->buffer+4, &usec, sizeof(usec));
2478556db5dSsethdelliott memcpy(sp->buffer+8, &pcount, sizeof(pcount));
248a951c980SBrian Tierney
249329523a5SBruce A. Mah }
250329523a5SBruce A. Mah
25143929b36SJef Poskanzer r = Nwrite(sp->socket, sp->buffer, size, Pudp);
252a951c980SBrian Tierney
25343929b36SJef Poskanzer if (r < 0)
25443929b36SJef Poskanzer return r;
2558556db5dSsethdelliott
25643929b36SJef Poskanzer sp->result->bytes_sent += r;
25743929b36SJef Poskanzer sp->result->bytes_sent_this_interval += r;
258aa89cda2Ssethdelliott
259b97df9a9SDavid Bar-On if (sp->test->debug_level >= DEBUG_LEVEL_DEBUG)
260d88f4cecSPhilip Prindeville printf("sent %d bytes of %d, total %" PRIu64 "\n", r, sp->settings->blksize, sp->result->bytes_sent);
261c458a115SBruce A. Mah
26243929b36SJef Poskanzer return r;
263a951c980SBrian Tierney }
264a951c980SBrian Tierney
265a951c980SBrian Tierney
266a951c980SBrian Tierney /**************************************************************************/
267a951c980SBrian Tierney
268d6598603SBruce A. Mah /*
269d6598603SBruce A. Mah * The following functions all have to do with managing UDP data sockets.
270d6598603SBruce A. Mah * UDP of course is connectionless, so there isn't really a concept of
271d6598603SBruce A. Mah * setting up a connection, although connect(2) can (and is) used to
272d6598603SBruce A. Mah * bind the remote end of sockets. We need to simulate some of the
273d6598603SBruce A. Mah * connection management that is built-in to TCP so that each side of the
274d6598603SBruce A. Mah * connection knows about each other before the real data transfers begin.
275d6598603SBruce A. Mah */
276d6598603SBruce A. Mah
277d6598603SBruce A. Mah /*
2780fd60a36SBruce A. Mah * Set and verify socket buffer sizes.
2790fd60a36SBruce A. Mah * Return 0 if no error, -1 if an error, +1 if socket buffers are
2800fd60a36SBruce A. Mah * potentially too small to hold a message.
281a951c980SBrian Tierney */
282a951c980SBrian Tierney int
iperf_udp_buffercheck(struct iperf_test * test,int s)2830fd60a36SBruce A. Mah iperf_udp_buffercheck(struct iperf_test *test, int s)
284a951c980SBrian Tierney {
2850fd60a36SBruce A. Mah int rc = 0;
286e73821cdSBruce A. Mah int sndbuf_actual, rcvbuf_actual;
287a951c980SBrian Tierney
288d6598603SBruce A. Mah /*
28972ac83e7SBruce A. Mah * Set socket buffer size if requested. Do this for both sending and
29072ac83e7SBruce A. Mah * receiving so that we can cover both normal and --reverse operation.
29172ac83e7SBruce A. Mah */
29272ac83e7SBruce A. Mah int opt;
293d2202ee3SBruce A. Mah socklen_t optlen;
294d2202ee3SBruce A. Mah
29572ac83e7SBruce A. Mah if ((opt = test->settings->socket_bufsize)) {
29672ac83e7SBruce A. Mah if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt)) < 0) {
29772ac83e7SBruce A. Mah i_errno = IESETBUF;
29872ac83e7SBruce A. Mah return -1;
29972ac83e7SBruce A. Mah }
30072ac83e7SBruce A. Mah if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &opt, sizeof(opt)) < 0) {
30172ac83e7SBruce A. Mah i_errno = IESETBUF;
30272ac83e7SBruce A. Mah return -1;
30372ac83e7SBruce A. Mah }
30472ac83e7SBruce A. Mah }
30572ac83e7SBruce A. Mah
306d2202ee3SBruce A. Mah /* Read back and verify the sender socket buffer size */
307e73821cdSBruce A. Mah optlen = sizeof(sndbuf_actual);
308e73821cdSBruce A. Mah if (getsockopt(s, SOL_SOCKET, SO_SNDBUF, &sndbuf_actual, &optlen) < 0) {
309d2202ee3SBruce A. Mah i_errno = IESETBUF;
310d2202ee3SBruce A. Mah return -1;
311d2202ee3SBruce A. Mah }
31255598a07SBruce A. Mah if (test->debug) {
313e73821cdSBruce A. Mah printf("SNDBUF is %u, expecting %u\n", sndbuf_actual, test->settings->socket_bufsize);
31455598a07SBruce A. Mah }
315e73821cdSBruce A. Mah if (test->settings->socket_bufsize && test->settings->socket_bufsize > sndbuf_actual) {
316d2202ee3SBruce A. Mah i_errno = IESETBUF2;
317d2202ee3SBruce A. Mah return -1;
318d2202ee3SBruce A. Mah }
319e73821cdSBruce A. Mah if (test->settings->blksize > sndbuf_actual) {
320805a4ce1SBruce A. Mah char str[WARN_STR_LEN];
3210fd60a36SBruce A. Mah snprintf(str, sizeof(str),
3220fd60a36SBruce A. Mah "Block size %d > sending socket buffer size %d",
323e73821cdSBruce A. Mah test->settings->blksize, sndbuf_actual);
3240fd60a36SBruce A. Mah warning(str);
3250fd60a36SBruce A. Mah rc = 1;
3260fd60a36SBruce A. Mah }
327d2202ee3SBruce A. Mah
328d2202ee3SBruce A. Mah /* Read back and verify the receiver socket buffer size */
329e73821cdSBruce A. Mah optlen = sizeof(rcvbuf_actual);
330e73821cdSBruce A. Mah if (getsockopt(s, SOL_SOCKET, SO_RCVBUF, &rcvbuf_actual, &optlen) < 0) {
331d2202ee3SBruce A. Mah i_errno = IESETBUF;
332d2202ee3SBruce A. Mah return -1;
333d2202ee3SBruce A. Mah }
33455598a07SBruce A. Mah if (test->debug) {
335e73821cdSBruce A. Mah printf("RCVBUF is %u, expecting %u\n", rcvbuf_actual, test->settings->socket_bufsize);
33655598a07SBruce A. Mah }
337e73821cdSBruce A. Mah if (test->settings->socket_bufsize && test->settings->socket_bufsize > rcvbuf_actual) {
338d2202ee3SBruce A. Mah i_errno = IESETBUF2;
339d2202ee3SBruce A. Mah return -1;
340d2202ee3SBruce A. Mah }
341e73821cdSBruce A. Mah if (test->settings->blksize > rcvbuf_actual) {
342805a4ce1SBruce A. Mah char str[WARN_STR_LEN];
3430fd60a36SBruce A. Mah snprintf(str, sizeof(str),
3440fd60a36SBruce A. Mah "Block size %d > receiving socket buffer size %d",
345e73821cdSBruce A. Mah test->settings->blksize, rcvbuf_actual);
3460fd60a36SBruce A. Mah warning(str);
3470fd60a36SBruce A. Mah rc = 1;
3480fd60a36SBruce A. Mah }
349e73821cdSBruce A. Mah
350e73821cdSBruce A. Mah if (test->json_output) {
351e73821cdSBruce A. Mah cJSON_AddNumberToObject(test->json_start, "sock_bufsize", test->settings->socket_bufsize);
352e73821cdSBruce A. Mah cJSON_AddNumberToObject(test->json_start, "sndbuf_actual", sndbuf_actual);
353e73821cdSBruce A. Mah cJSON_AddNumberToObject(test->json_start, "rcvbuf_actual", rcvbuf_actual);
354e73821cdSBruce A. Mah }
355e73821cdSBruce A. Mah
3560fd60a36SBruce A. Mah return rc;
3570fd60a36SBruce A. Mah }
3580fd60a36SBruce A. Mah
3590fd60a36SBruce A. Mah /*
3600fd60a36SBruce A. Mah * iperf_udp_accept
3610fd60a36SBruce A. Mah *
3620fd60a36SBruce A. Mah * Accepts a new UDP "connection"
3630fd60a36SBruce A. Mah */
3640fd60a36SBruce A. Mah int
iperf_udp_accept(struct iperf_test * test)3650fd60a36SBruce A. Mah iperf_udp_accept(struct iperf_test *test)
3660fd60a36SBruce A. Mah {
3670fd60a36SBruce A. Mah struct sockaddr_storage sa_peer;
3682eace5a9SDavid Bar-On unsigned int buf;
3690fd60a36SBruce A. Mah socklen_t len;
3700fd60a36SBruce A. Mah int sz, s;
3710fd60a36SBruce A. Mah int rc;
3720fd60a36SBruce A. Mah
3730fd60a36SBruce A. Mah /*
3740fd60a36SBruce A. Mah * Get the current outstanding socket. This socket will be used to handle
3750fd60a36SBruce A. Mah * data transfers and a new "listening" socket will be created.
3760fd60a36SBruce A. Mah */
3770fd60a36SBruce A. Mah s = test->prot_listener;
3780fd60a36SBruce A. Mah
3790fd60a36SBruce A. Mah /*
3800fd60a36SBruce A. Mah * Grab the UDP packet sent by the client. From that we can extract the
3810fd60a36SBruce A. Mah * client's address, and then use that information to bind the remote side
3820fd60a36SBruce A. Mah * of the socket to the client.
3830fd60a36SBruce A. Mah */
3840fd60a36SBruce A. Mah len = sizeof(sa_peer);
3850fd60a36SBruce A. Mah if ((sz = recvfrom(test->prot_listener, &buf, sizeof(buf), 0, (struct sockaddr *) &sa_peer, &len)) < 0) {
3860fd60a36SBruce A. Mah i_errno = IESTREAMACCEPT;
3870fd60a36SBruce A. Mah return -1;
3880fd60a36SBruce A. Mah }
3890fd60a36SBruce A. Mah
3900fd60a36SBruce A. Mah if (connect(s, (struct sockaddr *) &sa_peer, len) < 0) {
3910fd60a36SBruce A. Mah i_errno = IESTREAMACCEPT;
3920fd60a36SBruce A. Mah return -1;
3930fd60a36SBruce A. Mah }
3940fd60a36SBruce A. Mah
3950fd60a36SBruce A. Mah /* Check and set socket buffer sizes */
3960fd60a36SBruce A. Mah rc = iperf_udp_buffercheck(test, s);
3970fd60a36SBruce A. Mah if (rc < 0)
3980fd60a36SBruce A. Mah /* error */
3990fd60a36SBruce A. Mah return rc;
4000fd60a36SBruce A. Mah /*
4010fd60a36SBruce A. Mah * If the socket buffer was too small, but it was the default
4020fd60a36SBruce A. Mah * size, then try explicitly setting it to something larger.
4030fd60a36SBruce A. Mah */
4040fd60a36SBruce A. Mah if (rc > 0) {
4050fd60a36SBruce A. Mah if (test->settings->socket_bufsize == 0) {
406a071a8c8SBruce A. Mah char str[WARN_STR_LEN];
4071ff12f4cSBruce A. Mah int bufsize = test->settings->blksize + UDP_BUFFER_EXTRA;
408a071a8c8SBruce A. Mah snprintf(str, sizeof(str), "Increasing socket buffer size to %d",
4091ff12f4cSBruce A. Mah bufsize);
410a071a8c8SBruce A. Mah warning(str);
4111ff12f4cSBruce A. Mah test->settings->socket_bufsize = bufsize;
4120fd60a36SBruce A. Mah rc = iperf_udp_buffercheck(test, s);
4130fd60a36SBruce A. Mah if (rc < 0)
4140fd60a36SBruce A. Mah return rc;
4150fd60a36SBruce A. Mah }
4160fd60a36SBruce A. Mah }
417d2202ee3SBruce A. Mah
4189915746aSBruce A. Mah #if defined(HAVE_SO_MAX_PACING_RATE)
419a094d9feSBruce A. Mah /* If socket pacing is specified, try it. */
420a094d9feSBruce A. Mah if (test->settings->fqrate) {
4219915746aSBruce A. Mah /* Convert bits per second to bytes per second */
422a094d9feSBruce A. Mah unsigned int fqrate = test->settings->fqrate / 8;
423a094d9feSBruce A. Mah if (fqrate > 0) {
4249915746aSBruce A. Mah if (test->debug) {
425a094d9feSBruce A. Mah printf("Setting fair-queue socket pacing to %u\n", fqrate);
4269915746aSBruce A. Mah }
427a094d9feSBruce A. Mah if (setsockopt(s, SOL_SOCKET, SO_MAX_PACING_RATE, &fqrate, sizeof(fqrate)) < 0) {
428a094d9feSBruce A. Mah warning("Unable to set socket pacing");
4299915746aSBruce A. Mah }
4309915746aSBruce A. Mah }
4319915746aSBruce A. Mah }
4329915746aSBruce A. Mah #endif /* HAVE_SO_MAX_PACING_RATE */
433a094d9feSBruce A. Mah {
434a621cc26SBruce A. Mah unsigned int rate = test->settings->rate / 8;
435a621cc26SBruce A. Mah if (rate > 0) {
436a621cc26SBruce A. Mah if (test->debug) {
437a621cc26SBruce A. Mah printf("Setting application pacing to %u\n", rate);
438a621cc26SBruce A. Mah }
439a621cc26SBruce A. Mah }
440a621cc26SBruce A. Mah }
4419915746aSBruce A. Mah
44272ac83e7SBruce A. Mah /*
443d6598603SBruce A. Mah * Create a new "listening" socket to replace the one we were using before.
444d6598603SBruce A. Mah */
44521581a72SBruce A. Mah test->prot_listener = netannounce(test->settings->domain, Pudp, test->bind_address, test->bind_dev, test->server_port);
4468a0b5a5dSsethdelliott if (test->prot_listener < 0) {
4478a0b5a5dSsethdelliott i_errno = IESTREAMLISTEN;
448ec2d0670SJef Poskanzer return -1;
449b60a49ddSsethdelliott }
450a951c980SBrian Tierney
4518a0b5a5dSsethdelliott FD_SET(test->prot_listener, &test->read_set);
4528a0b5a5dSsethdelliott test->max_fd = (test->max_fd < test->prot_listener) ? test->prot_listener : test->max_fd;
453a951c980SBrian Tierney
4548556db5dSsethdelliott /* Let the client know we're ready "accept" another UDP "stream" */
4552eace5a9SDavid Bar-On buf = UDP_CONNECT_REPLY;
4568a0b5a5dSsethdelliott if (write(s, &buf, sizeof(buf)) < 0) {
457b60a49ddSsethdelliott i_errno = IESTREAMWRITE;
458ec2d0670SJef Poskanzer return -1;
459a951c980SBrian Tierney }
460a951c980SBrian Tierney
461ec2d0670SJef Poskanzer return s;
4628a0b5a5dSsethdelliott }
4638a0b5a5dSsethdelliott
4648a0b5a5dSsethdelliott
465d6598603SBruce A. Mah /*
466d6598603SBruce A. Mah * iperf_udp_listen
4678a0b5a5dSsethdelliott *
468d6598603SBruce A. Mah * Start up a listener for UDP stream connections. Unlike for TCP,
469d6598603SBruce A. Mah * there is no listen(2) for UDP. This socket will however accept
470d6598603SBruce A. Mah * a UDP datagram from a client (indicating the client's presence).
4718a0b5a5dSsethdelliott */
4728a0b5a5dSsethdelliott int
iperf_udp_listen(struct iperf_test * test)4738a0b5a5dSsethdelliott iperf_udp_listen(struct iperf_test *test)
4748a0b5a5dSsethdelliott {
4758a0b5a5dSsethdelliott int s;
4768a0b5a5dSsethdelliott
47721581a72SBruce A. Mah if ((s = netannounce(test->settings->domain, Pudp, test->bind_address, test->bind_dev, test->server_port)) < 0) {
4788a0b5a5dSsethdelliott i_errno = IESTREAMLISTEN;
479ec2d0670SJef Poskanzer return -1;
4808a0b5a5dSsethdelliott }
4818a0b5a5dSsethdelliott
482d6598603SBruce A. Mah /*
483d6598603SBruce A. Mah * The caller will put this value into test->prot_listener.
484d6598603SBruce A. Mah */
485ec2d0670SJef Poskanzer return s;
4868a0b5a5dSsethdelliott }
4878a0b5a5dSsethdelliott
4888a0b5a5dSsethdelliott
489d6598603SBruce A. Mah /*
490d6598603SBruce A. Mah * iperf_udp_connect
4918a0b5a5dSsethdelliott *
492d6598603SBruce A. Mah * "Connect" to a UDP stream listener.
4938a0b5a5dSsethdelliott */
4948a0b5a5dSsethdelliott int
iperf_udp_connect(struct iperf_test * test)4958a0b5a5dSsethdelliott iperf_udp_connect(struct iperf_test *test)
4968a0b5a5dSsethdelliott {
4972eace5a9SDavid Bar-On int s, sz;
4982eace5a9SDavid Bar-On unsigned int buf;
499b8379188SHasso Tepper #ifdef SO_RCVTIMEO
5009b9dd030SHasso Tepper struct timeval tv;
501b8379188SHasso Tepper #endif
5020fd60a36SBruce A. Mah int rc;
5032eace5a9SDavid Bar-On int i, max_len_wait_for_reply;
5048a0b5a5dSsethdelliott
505d6598603SBruce A. Mah /* Create and bind our local socket. */
50621581a72SBruce A. Mah if ((s = netdial(test->settings->domain, Pudp, test->bind_address, test->bind_dev, test->bind_port, test->server_hostname, test->server_port, -1)) < 0) {
5078a0b5a5dSsethdelliott i_errno = IESTREAMCONNECT;
508ec2d0670SJef Poskanzer return -1;
5098a0b5a5dSsethdelliott }
5108a0b5a5dSsethdelliott
5110fd60a36SBruce A. Mah /* Check and set socket buffer sizes */
5120fd60a36SBruce A. Mah rc = iperf_udp_buffercheck(test, s);
5130fd60a36SBruce A. Mah if (rc < 0)
5140fd60a36SBruce A. Mah /* error */
5150fd60a36SBruce A. Mah return rc;
516d6598603SBruce A. Mah /*
5170fd60a36SBruce A. Mah * If the socket buffer was too small, but it was the default
5180fd60a36SBruce A. Mah * size, then try explicitly setting it to something larger.
51972ac83e7SBruce A. Mah */
5200fd60a36SBruce A. Mah if (rc > 0) {
5210fd60a36SBruce A. Mah if (test->settings->socket_bufsize == 0) {
522a071a8c8SBruce A. Mah char str[WARN_STR_LEN];
5231ff12f4cSBruce A. Mah int bufsize = test->settings->blksize + UDP_BUFFER_EXTRA;
524a071a8c8SBruce A. Mah snprintf(str, sizeof(str), "Increasing socket buffer size to %d",
5251ff12f4cSBruce A. Mah bufsize);
526a071a8c8SBruce A. Mah warning(str);
5271ff12f4cSBruce A. Mah test->settings->socket_bufsize = bufsize;
5280fd60a36SBruce A. Mah rc = iperf_udp_buffercheck(test, s);
5290fd60a36SBruce A. Mah if (rc < 0)
5300fd60a36SBruce A. Mah return rc;
53172ac83e7SBruce A. Mah }
532d2202ee3SBruce A. Mah }
53372ac83e7SBruce A. Mah
5349915746aSBruce A. Mah #if defined(HAVE_SO_MAX_PACING_RATE)
5359915746aSBruce A. Mah /* If socket pacing is available and not disabled, try it. */
536a094d9feSBruce A. Mah if (test->settings->fqrate) {
5379915746aSBruce A. Mah /* Convert bits per second to bytes per second */
538a094d9feSBruce A. Mah unsigned int fqrate = test->settings->fqrate / 8;
539a094d9feSBruce A. Mah if (fqrate > 0) {
5409915746aSBruce A. Mah if (test->debug) {
541a094d9feSBruce A. Mah printf("Setting fair-queue socket pacing to %u\n", fqrate);
5429915746aSBruce A. Mah }
543a094d9feSBruce A. Mah if (setsockopt(s, SOL_SOCKET, SO_MAX_PACING_RATE, &fqrate, sizeof(fqrate)) < 0) {
544a094d9feSBruce A. Mah warning("Unable to set socket pacing");
5459915746aSBruce A. Mah }
5469915746aSBruce A. Mah }
5479915746aSBruce A. Mah }
5489915746aSBruce A. Mah #endif /* HAVE_SO_MAX_PACING_RATE */
549a094d9feSBruce A. Mah {
550a621cc26SBruce A. Mah unsigned int rate = test->settings->rate / 8;
551a621cc26SBruce A. Mah if (rate > 0) {
552a621cc26SBruce A. Mah if (test->debug) {
553a621cc26SBruce A. Mah printf("Setting application pacing to %u\n", rate);
554a621cc26SBruce A. Mah }
555a621cc26SBruce A. Mah }
556a621cc26SBruce A. Mah }
5579915746aSBruce A. Mah
558b8379188SHasso Tepper #ifdef SO_RCVTIMEO
5599b9dd030SHasso Tepper /* 30 sec timeout for a case when there is a network problem. */
5609b9dd030SHasso Tepper tv.tv_sec = 30;
5619b9dd030SHasso Tepper tv.tv_usec = 0;
5629b9dd030SHasso Tepper setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (struct timeval *)&tv, sizeof(struct timeval));
563b8379188SHasso Tepper #endif
5649b9dd030SHasso Tepper
56572ac83e7SBruce A. Mah /*
566d6598603SBruce A. Mah * Write a datagram to the UDP stream to let the server know we're here.
567d6598603SBruce A. Mah * The server learns our address by obtaining its peer's address.
568d6598603SBruce A. Mah */
5692eace5a9SDavid Bar-On buf = UDP_CONNECT_MSG;
5702eace5a9SDavid Bar-On if (test->debug) {
571*c8032570SDimitri Papadopoulos printf("Sending Connect message to Socket %d\n", s);
5722eace5a9SDavid Bar-On }
5738a0b5a5dSsethdelliott if (write(s, &buf, sizeof(buf)) < 0) {
5748a0b5a5dSsethdelliott // XXX: Should this be changed to IESTREAMCONNECT?
5758a0b5a5dSsethdelliott i_errno = IESTREAMWRITE;
576ec2d0670SJef Poskanzer return -1;
5778a0b5a5dSsethdelliott }
578111645edSJef Poskanzer
579d6598603SBruce A. Mah /*
5802eace5a9SDavid Bar-On * Wait until the server replies back to us with the "accept" response.
581d6598603SBruce A. Mah */
5822eace5a9SDavid Bar-On i = 0;
5832eace5a9SDavid Bar-On max_len_wait_for_reply = sizeof(buf);
5842eace5a9SDavid Bar-On if (test->reverse) /* In reverse mode allow few packets to have the "accept" response - to handle out of order packets */
5852eace5a9SDavid Bar-On max_len_wait_for_reply += MAX_REVERSE_OUT_OF_ORDER_PACKETS * test->settings->blksize;
5862eace5a9SDavid Bar-On do {
587111645edSJef Poskanzer if ((sz = recv(s, &buf, sizeof(buf), 0)) < 0) {
5888a0b5a5dSsethdelliott i_errno = IESTREAMREAD;
589ec2d0670SJef Poskanzer return -1;
5908a0b5a5dSsethdelliott }
5912eace5a9SDavid Bar-On if (test->debug) {
5922eace5a9SDavid Bar-On printf("Connect received for Socket %d, sz=%d, buf=%x, i=%d, max_len_wait_for_reply=%d\n", s, sz, buf, i, max_len_wait_for_reply);
5932eace5a9SDavid Bar-On }
5942eace5a9SDavid Bar-On i += sz;
59515ec8e6eSDavid Bar-On } while (buf != UDP_CONNECT_REPLY && buf != LEGACY_UDP_CONNECT_REPLY && i < max_len_wait_for_reply);
5962eace5a9SDavid Bar-On
59715ec8e6eSDavid Bar-On if (buf != UDP_CONNECT_REPLY && buf != LEGACY_UDP_CONNECT_REPLY) {
5982eace5a9SDavid Bar-On i_errno = IESTREAMREAD;
5992eace5a9SDavid Bar-On return -1;
6002eace5a9SDavid Bar-On }
6018a0b5a5dSsethdelliott
602ec2d0670SJef Poskanzer return s;
6038a0b5a5dSsethdelliott }
6048a0b5a5dSsethdelliott
6058a0b5a5dSsethdelliott
6068a0b5a5dSsethdelliott /* iperf_udp_init
6078a0b5a5dSsethdelliott *
6088a0b5a5dSsethdelliott * initializer for UDP streams in TEST_START
6098a0b5a5dSsethdelliott */
6108a0b5a5dSsethdelliott int
iperf_udp_init(struct iperf_test * test)6118a0b5a5dSsethdelliott iperf_udp_init(struct iperf_test *test)
6128a0b5a5dSsethdelliott {
613ec2d0670SJef Poskanzer return 0;
614a951c980SBrian Tierney }
615