1 /*
2 * iperf, Copyright (c) 2014-2022, The Regents of the University of
3 * California, through Lawrence Berkeley National Laboratory (subject
4 * to receipt of any required approvals from the U.S. Dept. of
5 * Energy). All rights reserved.
6 *
7 * If you have questions about your rights to use or distribute this
8 * software, please contact Berkeley Lab's Technology Transfer
9 * Department at [email protected].
10 *
11 * NOTICE. This software is owned by the U.S. Department of Energy.
12 * As such, the U.S. Government has been granted for itself and others
13 * acting on its behalf a paid-up, nonexclusive, irrevocable,
14 * worldwide license in the Software to reproduce, prepare derivative
15 * works, and perform publicly and display publicly. Beginning five
16 * (5) years after the date permission to assert copyright is obtained
17 * from the U.S. Department of Energy, and subject to any subsequent
18 * five (5) year renewals, the U.S. Government is granted for itself
19 * and others acting on its behalf a paid-up, nonexclusive,
20 * irrevocable, worldwide license in the Software to reproduce,
21 * prepare derivative works, distribute copies to the public, perform
22 * publicly and display publicly, and to permit others to do so.
23 *
24 * This code is distributed under a BSD style license, see the LICENSE
25 * file for complete information.
26 */
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <errno.h>
31 #include <unistd.h>
32 #include <assert.h>
33 #include <arpa/inet.h>
34 #include <sys/socket.h>
35 #include <sys/types.h>
36 #include <netinet/in.h>
37 #ifdef HAVE_STDINT_H
38 #include <stdint.h>
39 #endif
40 #include <sys/time.h>
41 #include <sys/select.h>
42
43 #include "iperf.h"
44 #include "iperf_api.h"
45 #include "iperf_util.h"
46 #include "iperf_udp.h"
47 #include "timer.h"
48 #include "net.h"
49 #include "cjson.h"
50 #include "portable_endian.h"
51
52 #if defined(HAVE_INTTYPES_H)
53 # include <inttypes.h>
54 #else
55 # ifndef PRIu64
56 # if sizeof(long) == 8
57 # define PRIu64 "lu"
58 # else
59 # define PRIu64 "llu"
60 # endif
61 # endif
62 #endif
63
64 /* iperf_udp_recv
65 *
66 * receives the data for UDP
67 */
68 int
iperf_udp_recv(struct iperf_stream * sp)69 iperf_udp_recv(struct iperf_stream *sp)
70 {
71 uint32_t sec, usec;
72 uint64_t pcount;
73 int r;
74 int size = sp->settings->blksize;
75 int first_packet = 0;
76 double transit = 0, d = 0;
77 struct iperf_time sent_time, arrival_time, temp_time;
78
79 r = Nread(sp->socket, sp->buffer, size, Pudp);
80
81 /*
82 * If we got an error in the read, or if we didn't read anything
83 * because the underlying read(2) got a EAGAIN, then skip packet
84 * processing.
85 */
86 if (r <= 0)
87 return r;
88
89 /* Only count bytes received while we're in the correct state. */
90 if (sp->test->state == TEST_RUNNING) {
91
92 /*
93 * For jitter computation below, it's important to know if this
94 * packet is the first packet received.
95 */
96 if (sp->result->bytes_received == 0) {
97 first_packet = 1;
98 }
99
100 sp->result->bytes_received += r;
101 sp->result->bytes_received_this_interval += r;
102
103 /* Dig the various counters out of the incoming UDP packet */
104 if (sp->test->udp_counters_64bit) {
105 memcpy(&sec, sp->buffer, sizeof(sec));
106 memcpy(&usec, sp->buffer+4, sizeof(usec));
107 memcpy(&pcount, sp->buffer+8, sizeof(pcount));
108 sec = ntohl(sec);
109 usec = ntohl(usec);
110 pcount = be64toh(pcount);
111 sent_time.secs = sec;
112 sent_time.usecs = usec;
113 }
114 else {
115 uint32_t pc;
116 memcpy(&sec, sp->buffer, sizeof(sec));
117 memcpy(&usec, sp->buffer+4, sizeof(usec));
118 memcpy(&pc, sp->buffer+8, sizeof(pc));
119 sec = ntohl(sec);
120 usec = ntohl(usec);
121 pcount = ntohl(pc);
122 sent_time.secs = sec;
123 sent_time.usecs = usec;
124 }
125
126 if (sp->test->debug_level >= DEBUG_LEVEL_DEBUG)
127 fprintf(stderr, "pcount %" PRIu64 " packet_count %d\n", pcount, sp->packet_count);
128
129 /*
130 * Try to handle out of order packets. The way we do this
131 * uses a constant amount of storage but might not be
132 * correct in all cases. In particular we seem to have the
133 * assumption that packets can't be duplicated in the network,
134 * because duplicate packets will possibly cause some problems here.
135 *
136 * First figure out if the sequence numbers are going forward.
137 * Note that pcount is the sequence number read from the packet,
138 * and sp->packet_count is the highest sequence number seen so
139 * far (so we're expecting to see the packet with sequence number
140 * sp->packet_count + 1 arrive next).
141 */
142 if (pcount >= sp->packet_count + 1) {
143
144 /* Forward, but is there a gap in sequence numbers? */
145 if (pcount > sp->packet_count + 1) {
146 /* There's a gap so count that as a loss. */
147 sp->cnt_error += (pcount - 1) - sp->packet_count;
148 }
149 /* Update the highest sequence number seen so far. */
150 sp->packet_count = pcount;
151 } else {
152
153 /*
154 * Sequence number went backward (or was stationary?!?).
155 * This counts as an out-of-order packet.
156 */
157 sp->outoforder_packets++;
158
159 /*
160 * If we have lost packets, then the fact that we are now
161 * seeing an out-of-order packet offsets a prior sequence
162 * number gap that was counted as a loss. So we can take
163 * away a loss.
164 */
165 if (sp->cnt_error > 0)
166 sp->cnt_error--;
167
168 /* Log the out-of-order packet */
169 if (sp->test->debug)
170 fprintf(stderr, "OUT OF ORDER - incoming packet sequence %" PRIu64 " but expected sequence %d on stream %d", pcount, sp->packet_count + 1, sp->socket);
171 }
172
173 /*
174 * jitter measurement
175 *
176 * This computation is based on RFC 1889 (specifically
177 * sections 6.3.1 and A.8).
178 *
179 * Note that synchronized clocks are not required since
180 * the source packet delta times are known. Also this
181 * computation does not require knowing the round-trip
182 * time.
183 */
184 iperf_time_now(&arrival_time);
185
186 iperf_time_diff(&arrival_time, &sent_time, &temp_time);
187 transit = iperf_time_in_secs(&temp_time);
188
189 /* Hack to handle the first packet by initializing prev_transit. */
190 if (first_packet)
191 sp->prev_transit = transit;
192
193 d = transit - sp->prev_transit;
194 if (d < 0)
195 d = -d;
196 sp->prev_transit = transit;
197 sp->jitter += (d - sp->jitter) / 16.0;
198 }
199 else {
200 if (sp->test->debug)
201 printf("Late receive, state = %d\n", sp->test->state);
202 }
203
204 return r;
205 }
206
207
208 /* iperf_udp_send
209 *
210 * sends the data for UDP
211 */
212 int
iperf_udp_send(struct iperf_stream * sp)213 iperf_udp_send(struct iperf_stream *sp)
214 {
215 int r;
216 int size = sp->settings->blksize;
217 struct iperf_time before;
218
219 iperf_time_now(&before);
220
221 ++sp->packet_count;
222
223 if (sp->test->udp_counters_64bit) {
224
225 uint32_t sec, usec;
226 uint64_t pcount;
227
228 sec = htonl(before.secs);
229 usec = htonl(before.usecs);
230 pcount = htobe64(sp->packet_count);
231
232 memcpy(sp->buffer, &sec, sizeof(sec));
233 memcpy(sp->buffer+4, &usec, sizeof(usec));
234 memcpy(sp->buffer+8, &pcount, sizeof(pcount));
235
236 }
237 else {
238
239 uint32_t sec, usec, pcount;
240
241 sec = htonl(before.secs);
242 usec = htonl(before.usecs);
243 pcount = htonl(sp->packet_count);
244
245 memcpy(sp->buffer, &sec, sizeof(sec));
246 memcpy(sp->buffer+4, &usec, sizeof(usec));
247 memcpy(sp->buffer+8, &pcount, sizeof(pcount));
248
249 }
250
251 r = Nwrite(sp->socket, sp->buffer, size, Pudp);
252
253 if (r < 0)
254 return r;
255
256 sp->result->bytes_sent += r;
257 sp->result->bytes_sent_this_interval += r;
258
259 if (sp->test->debug_level >= DEBUG_LEVEL_DEBUG)
260 printf("sent %d bytes of %d, total %" PRIu64 "\n", r, sp->settings->blksize, sp->result->bytes_sent);
261
262 return r;
263 }
264
265
266 /**************************************************************************/
267
268 /*
269 * The following functions all have to do with managing UDP data sockets.
270 * UDP of course is connectionless, so there isn't really a concept of
271 * setting up a connection, although connect(2) can (and is) used to
272 * bind the remote end of sockets. We need to simulate some of the
273 * connection management that is built-in to TCP so that each side of the
274 * connection knows about each other before the real data transfers begin.
275 */
276
277 /*
278 * Set and verify socket buffer sizes.
279 * Return 0 if no error, -1 if an error, +1 if socket buffers are
280 * potentially too small to hold a message.
281 */
282 int
iperf_udp_buffercheck(struct iperf_test * test,int s)283 iperf_udp_buffercheck(struct iperf_test *test, int s)
284 {
285 int rc = 0;
286 int sndbuf_actual, rcvbuf_actual;
287
288 /*
289 * Set socket buffer size if requested. Do this for both sending and
290 * receiving so that we can cover both normal and --reverse operation.
291 */
292 int opt;
293 socklen_t optlen;
294
295 if ((opt = test->settings->socket_bufsize)) {
296 if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt)) < 0) {
297 i_errno = IESETBUF;
298 return -1;
299 }
300 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &opt, sizeof(opt)) < 0) {
301 i_errno = IESETBUF;
302 return -1;
303 }
304 }
305
306 /* Read back and verify the sender socket buffer size */
307 optlen = sizeof(sndbuf_actual);
308 if (getsockopt(s, SOL_SOCKET, SO_SNDBUF, &sndbuf_actual, &optlen) < 0) {
309 i_errno = IESETBUF;
310 return -1;
311 }
312 if (test->debug) {
313 printf("SNDBUF is %u, expecting %u\n", sndbuf_actual, test->settings->socket_bufsize);
314 }
315 if (test->settings->socket_bufsize && test->settings->socket_bufsize > sndbuf_actual) {
316 i_errno = IESETBUF2;
317 return -1;
318 }
319 if (test->settings->blksize > sndbuf_actual) {
320 char str[WARN_STR_LEN];
321 snprintf(str, sizeof(str),
322 "Block size %d > sending socket buffer size %d",
323 test->settings->blksize, sndbuf_actual);
324 warning(str);
325 rc = 1;
326 }
327
328 /* Read back and verify the receiver socket buffer size */
329 optlen = sizeof(rcvbuf_actual);
330 if (getsockopt(s, SOL_SOCKET, SO_RCVBUF, &rcvbuf_actual, &optlen) < 0) {
331 i_errno = IESETBUF;
332 return -1;
333 }
334 if (test->debug) {
335 printf("RCVBUF is %u, expecting %u\n", rcvbuf_actual, test->settings->socket_bufsize);
336 }
337 if (test->settings->socket_bufsize && test->settings->socket_bufsize > rcvbuf_actual) {
338 i_errno = IESETBUF2;
339 return -1;
340 }
341 if (test->settings->blksize > rcvbuf_actual) {
342 char str[WARN_STR_LEN];
343 snprintf(str, sizeof(str),
344 "Block size %d > receiving socket buffer size %d",
345 test->settings->blksize, rcvbuf_actual);
346 warning(str);
347 rc = 1;
348 }
349
350 if (test->json_output) {
351 cJSON_AddNumberToObject(test->json_start, "sock_bufsize", test->settings->socket_bufsize);
352 cJSON_AddNumberToObject(test->json_start, "sndbuf_actual", sndbuf_actual);
353 cJSON_AddNumberToObject(test->json_start, "rcvbuf_actual", rcvbuf_actual);
354 }
355
356 return rc;
357 }
358
359 /*
360 * iperf_udp_accept
361 *
362 * Accepts a new UDP "connection"
363 */
364 int
iperf_udp_accept(struct iperf_test * test)365 iperf_udp_accept(struct iperf_test *test)
366 {
367 struct sockaddr_storage sa_peer;
368 unsigned int buf;
369 socklen_t len;
370 int sz, s;
371 int rc;
372
373 /*
374 * Get the current outstanding socket. This socket will be used to handle
375 * data transfers and a new "listening" socket will be created.
376 */
377 s = test->prot_listener;
378
379 /*
380 * Grab the UDP packet sent by the client. From that we can extract the
381 * client's address, and then use that information to bind the remote side
382 * of the socket to the client.
383 */
384 len = sizeof(sa_peer);
385 if ((sz = recvfrom(test->prot_listener, &buf, sizeof(buf), 0, (struct sockaddr *) &sa_peer, &len)) < 0) {
386 i_errno = IESTREAMACCEPT;
387 return -1;
388 }
389
390 if (connect(s, (struct sockaddr *) &sa_peer, len) < 0) {
391 i_errno = IESTREAMACCEPT;
392 return -1;
393 }
394
395 /* Check and set socket buffer sizes */
396 rc = iperf_udp_buffercheck(test, s);
397 if (rc < 0)
398 /* error */
399 return rc;
400 /*
401 * If the socket buffer was too small, but it was the default
402 * size, then try explicitly setting it to something larger.
403 */
404 if (rc > 0) {
405 if (test->settings->socket_bufsize == 0) {
406 char str[WARN_STR_LEN];
407 int bufsize = test->settings->blksize + UDP_BUFFER_EXTRA;
408 snprintf(str, sizeof(str), "Increasing socket buffer size to %d",
409 bufsize);
410 warning(str);
411 test->settings->socket_bufsize = bufsize;
412 rc = iperf_udp_buffercheck(test, s);
413 if (rc < 0)
414 return rc;
415 }
416 }
417
418 #if defined(HAVE_SO_MAX_PACING_RATE)
419 /* If socket pacing is specified, try it. */
420 if (test->settings->fqrate) {
421 /* Convert bits per second to bytes per second */
422 unsigned int fqrate = test->settings->fqrate / 8;
423 if (fqrate > 0) {
424 if (test->debug) {
425 printf("Setting fair-queue socket pacing to %u\n", fqrate);
426 }
427 if (setsockopt(s, SOL_SOCKET, SO_MAX_PACING_RATE, &fqrate, sizeof(fqrate)) < 0) {
428 warning("Unable to set socket pacing");
429 }
430 }
431 }
432 #endif /* HAVE_SO_MAX_PACING_RATE */
433 {
434 unsigned int rate = test->settings->rate / 8;
435 if (rate > 0) {
436 if (test->debug) {
437 printf("Setting application pacing to %u\n", rate);
438 }
439 }
440 }
441
442 /*
443 * Create a new "listening" socket to replace the one we were using before.
444 */
445 test->prot_listener = netannounce(test->settings->domain, Pudp, test->bind_address, test->bind_dev, test->server_port);
446 if (test->prot_listener < 0) {
447 i_errno = IESTREAMLISTEN;
448 return -1;
449 }
450
451 FD_SET(test->prot_listener, &test->read_set);
452 test->max_fd = (test->max_fd < test->prot_listener) ? test->prot_listener : test->max_fd;
453
454 /* Let the client know we're ready "accept" another UDP "stream" */
455 buf = UDP_CONNECT_REPLY;
456 if (write(s, &buf, sizeof(buf)) < 0) {
457 i_errno = IESTREAMWRITE;
458 return -1;
459 }
460
461 return s;
462 }
463
464
465 /*
466 * iperf_udp_listen
467 *
468 * Start up a listener for UDP stream connections. Unlike for TCP,
469 * there is no listen(2) for UDP. This socket will however accept
470 * a UDP datagram from a client (indicating the client's presence).
471 */
472 int
iperf_udp_listen(struct iperf_test * test)473 iperf_udp_listen(struct iperf_test *test)
474 {
475 int s;
476
477 if ((s = netannounce(test->settings->domain, Pudp, test->bind_address, test->bind_dev, test->server_port)) < 0) {
478 i_errno = IESTREAMLISTEN;
479 return -1;
480 }
481
482 /*
483 * The caller will put this value into test->prot_listener.
484 */
485 return s;
486 }
487
488
489 /*
490 * iperf_udp_connect
491 *
492 * "Connect" to a UDP stream listener.
493 */
494 int
iperf_udp_connect(struct iperf_test * test)495 iperf_udp_connect(struct iperf_test *test)
496 {
497 int s, sz;
498 unsigned int buf;
499 #ifdef SO_RCVTIMEO
500 struct timeval tv;
501 #endif
502 int rc;
503 int i, max_len_wait_for_reply;
504
505 /* Create and bind our local socket. */
506 if ((s = netdial(test->settings->domain, Pudp, test->bind_address, test->bind_dev, test->bind_port, test->server_hostname, test->server_port, -1)) < 0) {
507 i_errno = IESTREAMCONNECT;
508 return -1;
509 }
510
511 /* Check and set socket buffer sizes */
512 rc = iperf_udp_buffercheck(test, s);
513 if (rc < 0)
514 /* error */
515 return rc;
516 /*
517 * If the socket buffer was too small, but it was the default
518 * size, then try explicitly setting it to something larger.
519 */
520 if (rc > 0) {
521 if (test->settings->socket_bufsize == 0) {
522 char str[WARN_STR_LEN];
523 int bufsize = test->settings->blksize + UDP_BUFFER_EXTRA;
524 snprintf(str, sizeof(str), "Increasing socket buffer size to %d",
525 bufsize);
526 warning(str);
527 test->settings->socket_bufsize = bufsize;
528 rc = iperf_udp_buffercheck(test, s);
529 if (rc < 0)
530 return rc;
531 }
532 }
533
534 #if defined(HAVE_SO_MAX_PACING_RATE)
535 /* If socket pacing is available and not disabled, try it. */
536 if (test->settings->fqrate) {
537 /* Convert bits per second to bytes per second */
538 unsigned int fqrate = test->settings->fqrate / 8;
539 if (fqrate > 0) {
540 if (test->debug) {
541 printf("Setting fair-queue socket pacing to %u\n", fqrate);
542 }
543 if (setsockopt(s, SOL_SOCKET, SO_MAX_PACING_RATE, &fqrate, sizeof(fqrate)) < 0) {
544 warning("Unable to set socket pacing");
545 }
546 }
547 }
548 #endif /* HAVE_SO_MAX_PACING_RATE */
549 {
550 unsigned int rate = test->settings->rate / 8;
551 if (rate > 0) {
552 if (test->debug) {
553 printf("Setting application pacing to %u\n", rate);
554 }
555 }
556 }
557
558 #ifdef SO_RCVTIMEO
559 /* 30 sec timeout for a case when there is a network problem. */
560 tv.tv_sec = 30;
561 tv.tv_usec = 0;
562 setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (struct timeval *)&tv, sizeof(struct timeval));
563 #endif
564
565 /*
566 * Write a datagram to the UDP stream to let the server know we're here.
567 * The server learns our address by obtaining its peer's address.
568 */
569 buf = UDP_CONNECT_MSG;
570 if (test->debug) {
571 printf("Sending Connect message to Socket %d\n", s);
572 }
573 if (write(s, &buf, sizeof(buf)) < 0) {
574 // XXX: Should this be changed to IESTREAMCONNECT?
575 i_errno = IESTREAMWRITE;
576 return -1;
577 }
578
579 /*
580 * Wait until the server replies back to us with the "accept" response.
581 */
582 i = 0;
583 max_len_wait_for_reply = sizeof(buf);
584 if (test->reverse) /* In reverse mode allow few packets to have the "accept" response - to handle out of order packets */
585 max_len_wait_for_reply += MAX_REVERSE_OUT_OF_ORDER_PACKETS * test->settings->blksize;
586 do {
587 if ((sz = recv(s, &buf, sizeof(buf), 0)) < 0) {
588 i_errno = IESTREAMREAD;
589 return -1;
590 }
591 if (test->debug) {
592 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);
593 }
594 i += sz;
595 } while (buf != UDP_CONNECT_REPLY && buf != LEGACY_UDP_CONNECT_REPLY && i < max_len_wait_for_reply);
596
597 if (buf != UDP_CONNECT_REPLY && buf != LEGACY_UDP_CONNECT_REPLY) {
598 i_errno = IESTREAMREAD;
599 return -1;
600 }
601
602 return s;
603 }
604
605
606 /* iperf_udp_init
607 *
608 * initializer for UDP streams in TEST_START
609 */
610 int
iperf_udp_init(struct iperf_test * test)611 iperf_udp_init(struct iperf_test *test)
612 {
613 return 0;
614 }
615