2009-11-02 22:43:19 +00:00
|
|
|
/*
|
2014-08-28 08:59:32 -07:00
|
|
|
* Copyright (c) 2009-2011, 2014, The Regents of the University of California,
|
2011-04-20 20:33:09 +00:00
|
|
|
* through Lawrence Berkeley National Laboratory (subject to receipt of any
|
|
|
|
* required approvals from the U.S. Dept. of Energy). All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is distributed under a BSD style license, see the LICENSE file
|
|
|
|
* for complete information.
|
2009-11-02 22:43:19 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <netinet/in.h>
|
2014-05-19 10:15:36 -07:00
|
|
|
#ifdef HAVE_STDINT_H
|
2009-11-02 22:43:19 +00:00
|
|
|
#include <stdint.h>
|
2014-05-19 10:15:36 -07:00
|
|
|
#endif
|
2009-11-02 22:43:19 +00:00
|
|
|
#include <sys/time.h>
|
2010-07-22 23:26:38 +00:00
|
|
|
#include <sys/select.h>
|
2009-11-02 22:43:19 +00:00
|
|
|
|
|
|
|
#include "iperf.h"
|
|
|
|
#include "iperf_api.h"
|
2012-12-05 12:34:58 -08:00
|
|
|
#include "iperf_util.h"
|
2009-11-02 22:43:19 +00:00
|
|
|
#include "iperf_udp.h"
|
|
|
|
#include "timer.h"
|
|
|
|
#include "net.h"
|
2014-08-28 08:59:32 -07:00
|
|
|
#include "portable_endian.h"
|
2009-11-02 22:43:19 +00:00
|
|
|
|
2010-07-22 18:57:08 +00:00
|
|
|
/* iperf_udp_recv
|
2009-11-02 22:43:19 +00:00
|
|
|
*
|
2010-07-22 18:57:08 +00:00
|
|
|
* receives the data for UDP
|
2009-11-02 22:43:19 +00:00
|
|
|
*/
|
|
|
|
int
|
2010-07-14 23:24:58 +00:00
|
|
|
iperf_udp_recv(struct iperf_stream *sp)
|
2009-11-02 22:43:19 +00:00
|
|
|
{
|
2014-08-28 08:59:32 -07:00
|
|
|
uint32_t sec, usec;
|
|
|
|
uint64_t pcount;
|
2013-02-22 15:54:05 -08:00
|
|
|
int r;
|
2009-11-02 22:43:19 +00:00
|
|
|
int size = sp->settings->blksize;
|
|
|
|
double transit = 0, d = 0;
|
2010-07-14 23:24:58 +00:00
|
|
|
struct timeval sent_time, arrival_time;
|
|
|
|
|
2013-02-22 15:54:05 -08:00
|
|
|
r = Nread(sp->socket, sp->buffer, size, Pudp);
|
2009-11-02 22:43:19 +00:00
|
|
|
|
2013-02-22 15:54:05 -08:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2010-07-22 18:57:08 +00:00
|
|
|
|
2013-02-22 15:54:05 -08:00
|
|
|
sp->result->bytes_received += r;
|
|
|
|
sp->result->bytes_received_this_interval += r;
|
2010-07-14 23:24:58 +00:00
|
|
|
|
2014-08-28 08:59:32 -07:00
|
|
|
if (sp->test->udp_counters_64bit) {
|
|
|
|
memcpy(&sec, sp->buffer, sizeof(sec));
|
|
|
|
memcpy(&usec, sp->buffer+4, sizeof(usec));
|
|
|
|
memcpy(&pcount, sp->buffer+8, sizeof(pcount));
|
|
|
|
sec = ntohl(sec);
|
|
|
|
usec = ntohl(usec);
|
|
|
|
pcount = be64toh(pcount);
|
|
|
|
sent_time.tv_sec = sec;
|
|
|
|
sent_time.tv_usec = usec;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
uint32_t pc;
|
|
|
|
memcpy(&sec, sp->buffer, sizeof(sec));
|
|
|
|
memcpy(&usec, sp->buffer+4, sizeof(usec));
|
|
|
|
memcpy(&pc, sp->buffer+8, sizeof(pc));
|
|
|
|
sec = ntohl(sec);
|
|
|
|
usec = ntohl(usec);
|
|
|
|
pcount = ntohl(pc);
|
|
|
|
sent_time.tv_sec = sec;
|
|
|
|
sent_time.tv_usec = usec;
|
|
|
|
}
|
2010-07-14 23:24:58 +00:00
|
|
|
|
|
|
|
/* Out of order packets */
|
|
|
|
if (pcount >= sp->packet_count + 1) {
|
|
|
|
if (pcount > sp->packet_count + 1) {
|
|
|
|
sp->cnt_error += (pcount - 1) - sp->packet_count;
|
|
|
|
}
|
|
|
|
sp->packet_count = pcount;
|
|
|
|
} else {
|
|
|
|
sp->outoforder_packets++;
|
2014-09-22 15:06:27 -07:00
|
|
|
iperf_err(sp->test, "OUT OF ORDER - incoming packet = %zu and received packet = %d AND SP = %d", pcount, sp->packet_count, sp->socket);
|
2009-11-02 22:43:19 +00:00
|
|
|
}
|
|
|
|
|
2010-07-14 23:24:58 +00:00
|
|
|
/* jitter measurement */
|
2010-07-20 22:27:50 +00:00
|
|
|
gettimeofday(&arrival_time, NULL);
|
|
|
|
|
2010-07-14 23:24:58 +00:00
|
|
|
transit = timeval_diff(&sent_time, &arrival_time);
|
|
|
|
d = transit - sp->prev_transit;
|
|
|
|
if (d < 0)
|
|
|
|
d = -d;
|
|
|
|
sp->prev_transit = transit;
|
|
|
|
// XXX: This is NOT the way to calculate jitter
|
|
|
|
// J = |(R1 - S1) - (R0 - S0)| [/ number of packets, for average]
|
|
|
|
sp->jitter += (d - sp->jitter) / 16.0;
|
2009-11-02 22:43:19 +00:00
|
|
|
|
2014-08-28 08:59:32 -07:00
|
|
|
if (sp->test->debug) {
|
2014-09-22 15:06:27 -07:00
|
|
|
fprintf(stderr, "packet_count %d\n", sp->packet_count);
|
2014-08-28 08:59:32 -07:00
|
|
|
}
|
|
|
|
|
2013-02-22 15:54:05 -08:00
|
|
|
return r;
|
2012-12-11 22:29:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-22 18:57:08 +00:00
|
|
|
/* iperf_udp_send
|
|
|
|
*
|
|
|
|
* sends the data for UDP
|
|
|
|
*/
|
2009-11-02 22:43:19 +00:00
|
|
|
int
|
2010-07-14 23:24:58 +00:00
|
|
|
iperf_udp_send(struct iperf_stream *sp)
|
2009-11-02 22:43:19 +00:00
|
|
|
{
|
2013-02-22 15:54:05 -08:00
|
|
|
int r;
|
2010-07-14 23:24:58 +00:00
|
|
|
int size = sp->settings->blksize;
|
2013-06-24 07:14:14 -07:00
|
|
|
struct timeval before;
|
2013-02-22 15:54:05 -08:00
|
|
|
|
|
|
|
gettimeofday(&before, 0);
|
|
|
|
|
|
|
|
++sp->packet_count;
|
|
|
|
|
2014-08-28 08:59:32 -07:00
|
|
|
if (sp->test->udp_counters_64bit) {
|
|
|
|
|
|
|
|
uint32_t sec, usec;
|
|
|
|
uint64_t pcount;
|
|
|
|
|
|
|
|
sec = htonl(before.tv_sec);
|
|
|
|
usec = htonl(before.tv_usec);
|
|
|
|
pcount = htobe64(sp->packet_count);
|
|
|
|
|
|
|
|
memcpy(sp->buffer, &sec, sizeof(sec));
|
|
|
|
memcpy(sp->buffer+4, &usec, sizeof(usec));
|
|
|
|
memcpy(sp->buffer+8, &pcount, sizeof(pcount));
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
uint32_t sec, usec, pcount;
|
|
|
|
|
|
|
|
sec = htonl(before.tv_sec);
|
|
|
|
usec = htonl(before.tv_usec);
|
|
|
|
pcount = htonl(sp->packet_count);
|
|
|
|
|
|
|
|
memcpy(sp->buffer, &sec, sizeof(sec));
|
|
|
|
memcpy(sp->buffer+4, &usec, sizeof(usec));
|
|
|
|
memcpy(sp->buffer+8, &pcount, sizeof(pcount));
|
|
|
|
|
|
|
|
}
|
2013-02-22 15:54:05 -08:00
|
|
|
|
|
|
|
r = Nwrite(sp->socket, sp->buffer, size, Pudp);
|
|
|
|
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
sp->result->bytes_sent += r;
|
|
|
|
sp->result->bytes_sent_this_interval += r;
|
|
|
|
|
|
|
|
return r;
|
2009-11-02 22:43:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**************************************************************************/
|
|
|
|
|
2010-07-22 18:57:08 +00:00
|
|
|
/* iperf_udp_accept
|
2009-11-02 22:43:19 +00:00
|
|
|
*
|
2010-07-22 18:57:08 +00:00
|
|
|
* accepts a new UDP connection
|
2009-11-02 22:43:19 +00:00
|
|
|
*/
|
|
|
|
int
|
2010-07-14 23:24:58 +00:00
|
|
|
iperf_udp_accept(struct iperf_test *test)
|
2009-11-02 22:43:19 +00:00
|
|
|
{
|
2013-05-02 15:18:07 -07:00
|
|
|
struct sockaddr_storage sa_peer;
|
2010-07-14 23:24:58 +00:00
|
|
|
int buf;
|
2009-11-02 22:43:19 +00:00
|
|
|
socklen_t len;
|
2010-07-14 23:24:58 +00:00
|
|
|
int sz, s;
|
2009-11-02 22:43:19 +00:00
|
|
|
|
2010-07-22 18:57:08 +00:00
|
|
|
s = test->prot_listener;
|
2009-11-02 22:43:19 +00:00
|
|
|
|
2013-05-02 15:18:07 -07:00
|
|
|
len = sizeof(sa_peer);
|
2010-07-22 18:57:08 +00:00
|
|
|
if ((sz = recvfrom(test->prot_listener, &buf, sizeof(buf), 0, (struct sockaddr *) &sa_peer, &len)) < 0) {
|
2010-07-20 22:27:50 +00:00
|
|
|
i_errno = IESTREAMACCEPT;
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2009-11-02 22:43:19 +00:00
|
|
|
}
|
|
|
|
|
2010-07-14 23:24:58 +00:00
|
|
|
if (connect(s, (struct sockaddr *) &sa_peer, len) < 0) {
|
2010-07-20 22:27:50 +00:00
|
|
|
i_errno = IESTREAMACCEPT;
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2010-07-14 23:24:58 +00:00
|
|
|
}
|
2009-11-02 22:43:19 +00:00
|
|
|
|
2010-07-27 20:27:34 +00:00
|
|
|
test->prot_listener = netannounce(test->settings->domain, Pudp, test->bind_address, test->server_port);
|
2010-07-22 18:57:08 +00:00
|
|
|
if (test->prot_listener < 0) {
|
|
|
|
i_errno = IESTREAMLISTEN;
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2010-07-20 22:27:50 +00:00
|
|
|
}
|
2009-11-02 22:43:19 +00:00
|
|
|
|
2010-07-22 18:57:08 +00:00
|
|
|
FD_SET(test->prot_listener, &test->read_set);
|
|
|
|
test->max_fd = (test->max_fd < test->prot_listener) ? test->prot_listener : test->max_fd;
|
2009-11-02 22:43:19 +00:00
|
|
|
|
2010-07-14 23:24:58 +00:00
|
|
|
/* Let the client know we're ready "accept" another UDP "stream" */
|
2013-05-02 15:18:07 -07:00
|
|
|
buf = 987654321;
|
2010-07-22 18:57:08 +00:00
|
|
|
if (write(s, &buf, sizeof(buf)) < 0) {
|
2010-07-20 22:27:50 +00:00
|
|
|
i_errno = IESTREAMWRITE;
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2009-11-02 22:43:19 +00:00
|
|
|
}
|
|
|
|
|
2012-12-11 22:29:26 -08:00
|
|
|
return s;
|
2010-07-22 18:57:08 +00:00
|
|
|
}
|
2009-11-02 22:43:19 +00:00
|
|
|
|
2010-07-22 18:57:08 +00:00
|
|
|
|
|
|
|
/* iperf_udp_listen
|
|
|
|
*
|
|
|
|
* start up a listener for UDP stream connections
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
iperf_udp_listen(struct iperf_test *test)
|
|
|
|
{
|
|
|
|
int s;
|
|
|
|
|
2010-07-27 20:27:34 +00:00
|
|
|
if ((s = netannounce(test->settings->domain, Pudp, test->bind_address, test->server_port)) < 0) {
|
2010-07-22 18:57:08 +00:00
|
|
|
i_errno = IESTREAMLISTEN;
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2010-07-22 18:57:08 +00:00
|
|
|
}
|
|
|
|
|
2012-12-11 22:29:26 -08:00
|
|
|
return s;
|
2010-07-22 18:57:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* iperf_udp_connect
|
|
|
|
*
|
|
|
|
* connect to a TCP stream listener
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
iperf_udp_connect(struct iperf_test *test)
|
|
|
|
{
|
2013-05-02 15:18:07 -07:00
|
|
|
int s, buf, sz;
|
2010-07-22 18:57:08 +00:00
|
|
|
|
2014-09-15 10:57:43 -07:00
|
|
|
if ((s = netdial(test->settings->domain, Pudp, test->bind_address, test->bind_port, test->server_hostname, test->server_port)) < 0) {
|
2010-07-22 18:57:08 +00:00
|
|
|
i_errno = IESTREAMCONNECT;
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2010-07-22 18:57:08 +00:00
|
|
|
}
|
|
|
|
|
2013-05-02 15:18:07 -07:00
|
|
|
/* Write to the UDP stream to let the server know we're here. */
|
|
|
|
buf = 123456789;
|
2010-07-22 18:57:08 +00:00
|
|
|
if (write(s, &buf, sizeof(buf)) < 0) {
|
|
|
|
// XXX: Should this be changed to IESTREAMCONNECT?
|
|
|
|
i_errno = IESTREAMWRITE;
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2010-07-22 18:57:08 +00:00
|
|
|
}
|
2013-05-02 15:18:07 -07:00
|
|
|
|
2010-07-22 18:57:08 +00:00
|
|
|
/* Wait until the server confirms the client UDP write */
|
|
|
|
// XXX: Should this read be TCP instead?
|
2013-05-02 15:18:07 -07:00
|
|
|
if ((sz = recv(s, &buf, sizeof(buf), 0)) < 0) {
|
2010-07-22 18:57:08 +00:00
|
|
|
i_errno = IESTREAMREAD;
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2010-07-22 18:57:08 +00:00
|
|
|
}
|
|
|
|
|
2012-12-11 22:29:26 -08:00
|
|
|
return s;
|
2009-11-02 22:43:19 +00:00
|
|
|
}
|
|
|
|
|
2010-07-22 18:57:08 +00:00
|
|
|
|
|
|
|
/* iperf_udp_init
|
|
|
|
*
|
|
|
|
* initializer for UDP streams in TEST_START
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
iperf_udp_init(struct iperf_test *test)
|
|
|
|
{
|
2012-12-11 22:29:26 -08:00
|
|
|
return 0;
|
2010-07-22 18:57:08 +00:00
|
|
|
}
|