2009-11-02 22:43:19 +00:00
|
|
|
/*
|
2011-04-20 20:33:09 +00:00
|
|
|
* Copyright (c) 2009-2011, The Regents of the University of California,
|
|
|
|
* 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
|
|
|
*/
|
|
|
|
|
|
|
|
/* iperf_server_api.c: Functions to be used by an iperf server
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <netinet/tcp.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <setjmp.h>
|
|
|
|
|
|
|
|
#include "iperf.h"
|
|
|
|
#include "iperf_api.h"
|
|
|
|
#include "iperf_udp.h"
|
|
|
|
#include "iperf_tcp.h"
|
2010-07-22 18:57:08 +00:00
|
|
|
#include "iperf_util.h"
|
2009-11-02 22:43:19 +00:00
|
|
|
#include "timer.h"
|
|
|
|
#include "net.h"
|
|
|
|
#include "units.h"
|
|
|
|
#include "tcp_window_size.h"
|
2010-07-09 00:29:51 +00:00
|
|
|
#include "iperf_util.h"
|
2009-11-02 22:43:19 +00:00
|
|
|
#include "locale.h"
|
|
|
|
|
|
|
|
|
2010-06-18 21:08:50 +00:00
|
|
|
int
|
|
|
|
iperf_server_listen(struct iperf_test *test)
|
2009-11-02 22:43:19 +00:00
|
|
|
{
|
2013-08-19 10:02:01 -07:00
|
|
|
retry:
|
2010-07-27 20:27:34 +00:00
|
|
|
if((test->listener = netannounce(test->settings->domain, Ptcp, test->bind_address, test->server_port)) < 0) {
|
2013-08-19 10:02:01 -07:00
|
|
|
if (errno == EAFNOSUPPORT && (test->settings->domain == AF_INET6 || test->settings->domain == AF_UNSPEC)) {
|
|
|
|
/* If we get "Address family not supported by protocol", that
|
|
|
|
** probably means we were compiled with IPv6 but the running
|
|
|
|
** kernel does not actually do IPv6. This is not too unusual,
|
|
|
|
** v6 support is and perhaps always will be spotty.
|
|
|
|
*/
|
|
|
|
warning("this system does not seem to support IPv6 - trying IPv4");
|
|
|
|
test->settings->domain = AF_INET;
|
|
|
|
goto retry;
|
|
|
|
} else {
|
|
|
|
i_errno = IELISTEN;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-06-18 21:08:50 +00:00
|
|
|
}
|
2009-11-02 22:43:19 +00:00
|
|
|
|
2013-02-01 11:42:35 -08:00
|
|
|
if (!test->json_output) {
|
|
|
|
printf("-----------------------------------------------------------\n");
|
|
|
|
printf("Server listening on %d\n", test->server_port);
|
|
|
|
}
|
2010-06-18 21:08:50 +00:00
|
|
|
|
|
|
|
// This needs to be changed to reflect if client has different window size
|
|
|
|
// make sure we got what we asked for
|
2010-06-21 15:08:47 +00:00
|
|
|
/* XXX: This needs to be moved to the stream listener
|
2010-07-22 23:26:38 +00:00
|
|
|
if ((x = get_tcp_windowsize(test->listener, SO_RCVBUF)) < 0) {
|
2010-06-18 21:08:50 +00:00
|
|
|
// Needs to set some sort of error number/message
|
|
|
|
perror("SO_RCVBUF");
|
|
|
|
return -1;
|
|
|
|
}
|
2010-06-21 15:08:47 +00:00
|
|
|
*/
|
2010-06-18 21:08:50 +00:00
|
|
|
|
2010-06-21 15:08:47 +00:00
|
|
|
// XXX: This code needs to be moved to after parameter exhange
|
2010-07-15 23:19:42 +00:00
|
|
|
/*
|
2012-09-28 16:00:14 -07:00
|
|
|
char ubuf[UNIT_LEN];
|
|
|
|
int x;
|
|
|
|
|
2010-07-22 23:26:38 +00:00
|
|
|
if (test->protocol->id == Ptcp) {
|
2010-07-23 18:39:14 +00:00
|
|
|
if (test->settings->socket_bufsize > 0) {
|
2010-06-18 21:08:50 +00:00
|
|
|
unit_snprintf(ubuf, UNIT_LEN, (double) x, 'A');
|
2013-02-01 11:42:35 -08:00
|
|
|
if (test->json_output)
|
|
|
|
printf("TCP window size: %s\n", ubuf);
|
2010-06-18 21:08:50 +00:00
|
|
|
} else {
|
2013-02-01 11:42:35 -08:00
|
|
|
if (test->json_output)
|
|
|
|
printf("Using TCP Autotuning\n");
|
2010-06-18 21:08:50 +00:00
|
|
|
}
|
|
|
|
}
|
2010-07-15 23:19:42 +00:00
|
|
|
*/
|
2013-02-01 11:42:35 -08:00
|
|
|
if (!test->json_output)
|
|
|
|
printf("-----------------------------------------------------------\n");
|
2009-11-06 02:19:20 +00:00
|
|
|
|
2009-11-02 22:43:19 +00:00
|
|
|
FD_ZERO(&test->read_set);
|
2010-06-30 15:58:16 +00:00
|
|
|
FD_ZERO(&test->write_set);
|
2010-07-22 23:26:38 +00:00
|
|
|
FD_SET(test->listener, &test->read_set);
|
|
|
|
test->max_fd = (test->listener > test->max_fd) ? test->listener : test->max_fd;
|
2010-06-18 21:08:50 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
iperf_accept(struct iperf_test *test)
|
|
|
|
{
|
|
|
|
int s;
|
2010-06-23 19:13:37 +00:00
|
|
|
int rbuf = ACCESS_DENIED;
|
2010-06-30 22:09:45 +00:00
|
|
|
char cookie[COOKIE_SIZE];
|
2010-06-18 21:08:50 +00:00
|
|
|
socklen_t len;
|
2013-05-02 15:28:30 -07:00
|
|
|
struct sockaddr_storage addr;
|
2010-07-06 23:12:54 +00:00
|
|
|
|
|
|
|
len = sizeof(addr);
|
2010-07-22 23:26:38 +00:00
|
|
|
if ((s = accept(test->listener, (struct sockaddr *) &addr, &len)) < 0) {
|
2010-07-20 22:27:50 +00:00
|
|
|
i_errno = IEACCEPT;
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2010-06-30 22:09:45 +00:00
|
|
|
}
|
2010-06-18 21:08:50 +00:00
|
|
|
|
2010-06-23 19:13:37 +00:00
|
|
|
if (test->ctrl_sck == -1) {
|
2010-06-30 22:09:45 +00:00
|
|
|
/* Server free, accept new client */
|
2010-07-23 18:39:14 +00:00
|
|
|
if (Nread(s, test->cookie, COOKIE_SIZE, Ptcp) < 0) {
|
2010-07-20 22:27:50 +00:00
|
|
|
i_errno = IERECVCOOKIE;
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2010-06-18 21:08:50 +00:00
|
|
|
}
|
2010-06-30 22:09:45 +00:00
|
|
|
|
2010-06-23 19:13:37 +00:00
|
|
|
FD_SET(s, &test->read_set);
|
|
|
|
FD_SET(s, &test->write_set);
|
|
|
|
test->max_fd = (s > test->max_fd) ? s : test->max_fd;
|
|
|
|
test->ctrl_sck = s;
|
2010-06-18 21:08:50 +00:00
|
|
|
|
2013-07-03 12:01:57 -07:00
|
|
|
if (iperf_set_send_state(test, PARAM_EXCHANGE) != 0)
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2013-08-29 11:38:20 -07:00
|
|
|
if (iperf_exchange_parameters(test) < 0)
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2013-08-29 11:38:20 -07:00
|
|
|
if (test->on_connect)
|
2010-07-23 20:46:58 +00:00
|
|
|
test->on_connect(test);
|
2010-06-18 21:08:50 +00:00
|
|
|
} else {
|
2013-05-02 15:18:07 -07:00
|
|
|
/* XXX: Do we even need to receive cookie if we're just going to deny anyways? */
|
2010-06-30 22:09:45 +00:00
|
|
|
if (Nread(s, cookie, COOKIE_SIZE, Ptcp) < 0) {
|
2010-07-20 22:27:50 +00:00
|
|
|
i_errno = IERECVCOOKIE;
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2010-06-23 19:13:37 +00:00
|
|
|
}
|
2013-03-04 15:55:16 -08:00
|
|
|
if (Nwrite(s, (char*) &rbuf, sizeof(int), Ptcp) < 0) {
|
2010-07-20 22:27:50 +00:00
|
|
|
i_errno = IESENDMESSAGE;
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2010-06-23 19:13:37 +00:00
|
|
|
}
|
2010-07-20 22:27:50 +00:00
|
|
|
close(s);
|
2009-11-10 04:41:42 +00:00
|
|
|
}
|
2010-06-30 22:09:45 +00:00
|
|
|
|
2012-12-11 22:29:26 -08:00
|
|
|
return 0;
|
2010-06-18 21:08:50 +00:00
|
|
|
}
|
|
|
|
|
2010-07-06 23:12:54 +00:00
|
|
|
|
2010-06-18 21:08:50 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
int
|
2010-06-23 19:13:37 +00:00
|
|
|
iperf_handle_message_server(struct iperf_test *test)
|
2010-06-18 21:08:50 +00:00
|
|
|
{
|
2010-07-06 23:12:54 +00:00
|
|
|
int rval;
|
2010-06-30 15:58:16 +00:00
|
|
|
struct iperf_stream *sp;
|
|
|
|
|
2010-07-20 22:27:50 +00:00
|
|
|
// XXX: Need to rethink how this behaves to fit API
|
2013-08-16 13:19:42 -07:00
|
|
|
if ((rval = Nread(test->ctrl_sck, (char*) &test->state, sizeof(signed char), Ptcp)) <= 0) {
|
2010-07-06 23:12:54 +00:00
|
|
|
if (rval == 0) {
|
2013-02-07 12:35:17 -08:00
|
|
|
iperf_err(test, "the client has unexpectedly closed the connection");
|
2010-07-20 22:27:50 +00:00
|
|
|
i_errno = IECTRLCLOSE;
|
2010-07-06 23:12:54 +00:00
|
|
|
test->state = IPERF_DONE;
|
2012-12-11 22:29:26 -08:00
|
|
|
return 0;
|
2010-07-06 23:12:54 +00:00
|
|
|
} else {
|
2010-07-20 22:27:50 +00:00
|
|
|
i_errno = IERECVMESSAGE;
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2010-07-06 23:12:54 +00:00
|
|
|
}
|
2010-06-18 21:08:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch(test->state) {
|
2010-06-21 15:08:47 +00:00
|
|
|
case TEST_START:
|
|
|
|
break;
|
2010-06-23 19:13:37 +00:00
|
|
|
case TEST_END:
|
2011-02-24 17:08:54 +00:00
|
|
|
cpu_util(&test->cpu_util);
|
2010-07-07 21:54:24 +00:00
|
|
|
test->stats_callback(test);
|
2010-07-28 20:29:25 +00:00
|
|
|
SLIST_FOREACH(sp, &test->streams, streams) {
|
2010-06-30 15:58:16 +00:00
|
|
|
FD_CLR(sp->socket, &test->read_set);
|
|
|
|
FD_CLR(sp->socket, &test->write_set);
|
|
|
|
close(sp->socket);
|
|
|
|
}
|
2013-07-03 12:01:57 -07:00
|
|
|
if (iperf_set_send_state(test, EXCHANGE_RESULTS) != 0)
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2013-05-29 15:18:18 -07:00
|
|
|
if (iperf_sum_results(test) < 0)
|
|
|
|
return -1;
|
2010-07-20 22:27:50 +00:00
|
|
|
if (iperf_exchange_results(test) < 0)
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2013-07-03 12:01:57 -07:00
|
|
|
if (iperf_set_send_state(test, DISPLAY_RESULTS) != 0)
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2010-07-23 20:46:58 +00:00
|
|
|
if (test->on_test_finish)
|
|
|
|
test->on_test_finish(test);
|
2010-06-29 22:02:30 +00:00
|
|
|
test->reporter_callback(test);
|
2010-06-23 19:13:37 +00:00
|
|
|
break;
|
2010-06-23 20:28:32 +00:00
|
|
|
case IPERF_DONE:
|
|
|
|
break;
|
2010-06-23 19:13:37 +00:00
|
|
|
case CLIENT_TERMINATE:
|
2010-07-27 22:11:09 +00:00
|
|
|
i_errno = IECLIENTTERM;
|
|
|
|
|
|
|
|
// XXX: Remove this line below!
|
2013-02-07 12:35:17 -08:00
|
|
|
iperf_err(test, "the client has terminated");
|
2010-07-28 20:29:25 +00:00
|
|
|
SLIST_FOREACH(sp, &test->streams, streams) {
|
2010-07-08 22:41:22 +00:00
|
|
|
FD_CLR(sp->socket, &test->read_set);
|
|
|
|
FD_CLR(sp->socket, &test->write_set);
|
|
|
|
close(sp->socket);
|
|
|
|
}
|
|
|
|
test->state = IPERF_DONE;
|
|
|
|
break;
|
2010-06-21 15:08:47 +00:00
|
|
|
default:
|
2010-07-20 22:27:50 +00:00
|
|
|
i_errno = IEMESSAGE;
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2010-06-18 21:08:50 +00:00
|
|
|
}
|
|
|
|
|
2012-12-11 22:29:26 -08:00
|
|
|
return 0;
|
2010-06-18 21:08:50 +00:00
|
|
|
}
|
2009-11-02 22:43:19 +00:00
|
|
|
|
2010-08-02 22:45:53 +00:00
|
|
|
/* XXX: This function is not used anymore */
|
2010-06-30 15:58:16 +00:00
|
|
|
void
|
|
|
|
iperf_test_reset(struct iperf_test *test)
|
|
|
|
{
|
2010-08-02 22:45:53 +00:00
|
|
|
struct iperf_stream *sp;
|
2010-06-30 15:58:16 +00:00
|
|
|
|
|
|
|
close(test->ctrl_sck);
|
|
|
|
|
|
|
|
/* Free streams */
|
2010-07-28 20:29:25 +00:00
|
|
|
while (!SLIST_EMPTY(&test->streams)) {
|
|
|
|
sp = SLIST_FIRST(&test->streams);
|
|
|
|
SLIST_REMOVE_HEAD(&test->streams, streams);
|
|
|
|
iperf_free_stream(sp);
|
|
|
|
}
|
2012-12-11 22:29:26 -08:00
|
|
|
if (test->timer != NULL) {
|
|
|
|
tmr_cancel(test->timer);
|
|
|
|
test->timer = NULL;
|
|
|
|
}
|
|
|
|
if (test->stats_timer != NULL) {
|
|
|
|
tmr_cancel(test->stats_timer);
|
|
|
|
test->stats_timer = NULL;
|
|
|
|
}
|
|
|
|
if (test->reporter_timer != NULL) {
|
|
|
|
tmr_cancel(test->reporter_timer);
|
|
|
|
test->reporter_timer = NULL;
|
|
|
|
}
|
2010-06-30 15:58:16 +00:00
|
|
|
|
2010-07-28 20:29:25 +00:00
|
|
|
SLIST_INIT(&test->streams);
|
2010-06-30 19:57:17 +00:00
|
|
|
|
2010-06-30 15:58:16 +00:00
|
|
|
test->role = 's';
|
2010-07-22 23:26:38 +00:00
|
|
|
set_protocol(test, Ptcp);
|
2013-07-05 13:52:19 -07:00
|
|
|
test->omit = OMIT;
|
2010-06-30 19:57:17 +00:00
|
|
|
test->duration = DURATION;
|
|
|
|
test->state = 0;
|
|
|
|
test->server_hostname = NULL;
|
|
|
|
|
|
|
|
test->ctrl_sck = -1;
|
2010-07-22 23:26:38 +00:00
|
|
|
test->prot_listener = -1;
|
2010-06-30 19:57:17 +00:00
|
|
|
|
2010-07-07 21:54:24 +00:00
|
|
|
test->bytes_sent = 0;
|
|
|
|
|
2010-06-30 19:57:17 +00:00
|
|
|
test->reverse = 0;
|
2013-05-29 12:32:42 -07:00
|
|
|
test->sender = 0;
|
2013-05-29 15:18:18 -07:00
|
|
|
test->sender_has_retransmits = 0;
|
2010-07-06 23:12:54 +00:00
|
|
|
test->no_delay = 0;
|
2010-06-30 19:57:17 +00:00
|
|
|
|
2010-06-30 15:58:16 +00:00
|
|
|
FD_ZERO(&test->read_set);
|
|
|
|
FD_ZERO(&test->write_set);
|
2010-07-22 23:26:38 +00:00
|
|
|
FD_SET(test->listener, &test->read_set);
|
|
|
|
test->max_fd = test->listener;
|
2010-06-30 19:57:17 +00:00
|
|
|
|
|
|
|
test->num_streams = 1;
|
2010-07-23 18:39:14 +00:00
|
|
|
test->settings->socket_bufsize = 0;
|
|
|
|
test->settings->blksize = DEFAULT_TCP_BLKSIZE;
|
2013-07-12 19:56:49 -07:00
|
|
|
test->settings->rate = 0;
|
2010-07-23 18:39:14 +00:00
|
|
|
test->settings->mss = 0;
|
|
|
|
memset(test->cookie, 0, COOKIE_SIZE);
|
2010-06-30 15:58:16 +00:00
|
|
|
}
|
|
|
|
|
2013-07-03 12:01:57 -07:00
|
|
|
static void
|
2013-07-05 13:52:19 -07:00
|
|
|
server_omit_timer_proc(TimerClientData client_data, struct timeval *nowP)
|
2013-07-03 12:01:57 -07:00
|
|
|
{
|
|
|
|
struct iperf_test *test = client_data.p;
|
|
|
|
|
2013-07-05 13:52:19 -07:00
|
|
|
test->omit_timer = NULL;
|
|
|
|
test->omitting = 0;
|
2013-07-03 12:01:57 -07:00
|
|
|
iperf_reset_stats(test);
|
|
|
|
if (test->verbose && !test->json_output)
|
2013-07-05 13:52:19 -07:00
|
|
|
printf("Finished omit period, starting real test\n");
|
2013-07-03 12:01:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2013-07-05 13:52:19 -07:00
|
|
|
create_server_omit_timer(struct iperf_test * test)
|
2013-07-03 12:01:57 -07:00
|
|
|
{
|
|
|
|
struct timeval now;
|
|
|
|
TimerClientData cd;
|
|
|
|
|
|
|
|
if (gettimeofday(&now, NULL) < 0) {
|
|
|
|
i_errno = IEINITTEST;
|
|
|
|
return -1;
|
|
|
|
}
|
2013-07-05 13:52:19 -07:00
|
|
|
test->omitting = 1;
|
2013-07-03 12:01:57 -07:00
|
|
|
cd.p = test;
|
2013-07-05 13:52:19 -07:00
|
|
|
test->omit_timer = tmr_create(&now, server_omit_timer_proc, cd, test->omit * SEC_TO_US, 0);
|
|
|
|
if (test->omit_timer == NULL) {
|
2013-07-03 12:01:57 -07:00
|
|
|
i_errno = IEINITTEST;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cleanup_server(struct iperf_test *test)
|
|
|
|
{
|
|
|
|
/* Close open test sockets */
|
|
|
|
close(test->ctrl_sck);
|
|
|
|
close(test->listener);
|
|
|
|
}
|
|
|
|
|
2010-06-23 21:34:07 +00:00
|
|
|
int
|
2010-06-18 21:08:50 +00:00
|
|
|
iperf_run_server(struct iperf_test *test)
|
|
|
|
{
|
2010-07-23 18:39:14 +00:00
|
|
|
int result, s, streams_accepted;
|
2013-01-18 10:17:05 -08:00
|
|
|
fd_set read_set, write_set;
|
2010-07-22 18:57:08 +00:00
|
|
|
struct iperf_stream *sp;
|
2012-12-11 22:29:26 -08:00
|
|
|
struct timeval now;
|
2010-06-18 21:08:50 +00:00
|
|
|
|
2013-03-08 20:56:52 -08:00
|
|
|
if (test->json_output)
|
|
|
|
if (iperf_json_start(test) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (test->json_output) {
|
|
|
|
cJSON_AddItemToObject(test->json_start, "version", cJSON_CreateString(version));
|
|
|
|
cJSON_AddItemToObject(test->json_start, "system_info", cJSON_CreateString(get_system_info()));
|
|
|
|
} else if (test->verbose) {
|
|
|
|
printf("%s\n", version);
|
|
|
|
system("uname -a");
|
|
|
|
}
|
|
|
|
|
2010-06-18 21:08:50 +00:00
|
|
|
// Open socket and listen
|
|
|
|
if (iperf_server_listen(test) < 0) {
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2010-06-18 21:08:50 +00:00
|
|
|
}
|
2009-11-02 22:43:19 +00:00
|
|
|
|
2011-02-24 17:08:54 +00:00
|
|
|
// Begin calculating CPU utilization
|
|
|
|
cpu_util(NULL);
|
|
|
|
|
2010-08-02 22:45:53 +00:00
|
|
|
test->state = IPERF_START;
|
|
|
|
streams_accepted = 0;
|
2010-06-18 21:08:50 +00:00
|
|
|
|
2012-12-11 22:29:26 -08:00
|
|
|
(void) gettimeofday(&now, NULL);
|
2010-08-02 22:45:53 +00:00
|
|
|
while (test->state != IPERF_DONE) {
|
2010-06-18 21:08:50 +00:00
|
|
|
|
2013-01-18 10:17:05 -08:00
|
|
|
memcpy(&read_set, &test->read_set, sizeof(fd_set));
|
|
|
|
memcpy(&write_set, &test->write_set, sizeof(fd_set));
|
2010-06-30 22:09:45 +00:00
|
|
|
|
2013-01-18 10:17:05 -08:00
|
|
|
result = select(test->max_fd + 1, &read_set, &write_set, NULL, tmr_timeout(&now));
|
2010-08-02 22:45:53 +00:00
|
|
|
if (result < 0 && errno != EINTR) {
|
2013-07-03 12:01:57 -07:00
|
|
|
cleanup_server(test);
|
2010-08-02 22:45:53 +00:00
|
|
|
i_errno = IESELECT;
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (result > 0) {
|
2013-01-18 10:17:05 -08:00
|
|
|
if (FD_ISSET(test->listener, &read_set)) {
|
2010-08-02 22:45:53 +00:00
|
|
|
if (test->state != CREATE_STREAMS) {
|
|
|
|
if (iperf_accept(test) < 0) {
|
2013-07-03 12:01:57 -07:00
|
|
|
cleanup_server(test);
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2010-06-30 22:09:45 +00:00
|
|
|
}
|
2013-01-18 10:17:05 -08:00
|
|
|
FD_CLR(test->listener, &read_set);
|
2010-06-18 21:08:50 +00:00
|
|
|
}
|
2010-08-02 22:45:53 +00:00
|
|
|
}
|
2013-01-18 10:17:05 -08:00
|
|
|
if (FD_ISSET(test->ctrl_sck, &read_set)) {
|
2013-07-03 12:01:57 -07:00
|
|
|
if (iperf_handle_message_server(test) < 0) {
|
|
|
|
cleanup_server(test);
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2013-07-03 12:01:57 -07:00
|
|
|
}
|
2013-01-18 10:17:05 -08:00
|
|
|
FD_CLR(test->ctrl_sck, &read_set);
|
2010-08-02 22:45:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (test->state == CREATE_STREAMS) {
|
2013-01-18 10:17:05 -08:00
|
|
|
if (FD_ISSET(test->prot_listener, &read_set)) {
|
2010-08-02 22:45:53 +00:00
|
|
|
|
2013-07-03 12:01:57 -07:00
|
|
|
if ((s = test->protocol->accept(test)) < 0) {
|
|
|
|
cleanup_server(test);
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2013-07-03 12:01:57 -07:00
|
|
|
}
|
2010-06-30 22:09:45 +00:00
|
|
|
|
2010-08-02 22:45:53 +00:00
|
|
|
if (!is_closed(s)) {
|
|
|
|
sp = iperf_new_stream(test, s);
|
2013-07-03 12:01:57 -07:00
|
|
|
if (!sp) {
|
|
|
|
cleanup_server(test);
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2013-07-03 12:01:57 -07:00
|
|
|
}
|
2010-07-22 18:57:08 +00:00
|
|
|
|
2010-08-02 22:45:53 +00:00
|
|
|
FD_SET(s, &test->read_set);
|
|
|
|
FD_SET(s, &test->write_set);
|
|
|
|
test->max_fd = (s > test->max_fd) ? s : test->max_fd;
|
2010-07-22 23:26:38 +00:00
|
|
|
|
2010-08-02 22:45:53 +00:00
|
|
|
streams_accepted++;
|
|
|
|
if (test->on_new_stream)
|
|
|
|
test->on_new_stream(sp);
|
2010-06-30 22:09:45 +00:00
|
|
|
}
|
2013-01-18 10:17:05 -08:00
|
|
|
FD_CLR(test->prot_listener, &read_set);
|
2010-08-02 22:45:53 +00:00
|
|
|
}
|
2010-07-22 18:57:08 +00:00
|
|
|
|
2010-08-02 22:45:53 +00:00
|
|
|
if (streams_accepted == test->num_streams) {
|
|
|
|
if (test->protocol->id != Ptcp) {
|
|
|
|
FD_CLR(test->prot_listener, &test->read_set);
|
|
|
|
close(test->prot_listener);
|
|
|
|
} else {
|
|
|
|
if (test->no_delay || test->settings->mss || test->settings->socket_bufsize) {
|
|
|
|
FD_CLR(test->listener, &test->read_set);
|
|
|
|
close(test->listener);
|
|
|
|
if ((s = netannounce(test->settings->domain, Ptcp, test->bind_address, test->server_port)) < 0) {
|
2013-07-03 12:01:57 -07:00
|
|
|
cleanup_server(test);
|
2010-08-02 22:45:53 +00:00
|
|
|
i_errno = IELISTEN;
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2010-07-15 23:19:42 +00:00
|
|
|
}
|
2010-08-02 22:45:53 +00:00
|
|
|
test->listener = s;
|
|
|
|
test->max_fd = (s > test->max_fd ? s : test->max_fd);
|
|
|
|
FD_SET(test->listener, &test->read_set);
|
2010-07-14 23:24:58 +00:00
|
|
|
}
|
2010-08-02 22:45:53 +00:00
|
|
|
}
|
|
|
|
test->prot_listener = -1;
|
2013-07-03 12:01:57 -07:00
|
|
|
if (iperf_set_send_state(test, TEST_START) != 0) {
|
|
|
|
cleanup_server(test);
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2013-07-03 12:01:57 -07:00
|
|
|
}
|
|
|
|
if (iperf_init_test(test) < 0) {
|
|
|
|
cleanup_server(test);
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2013-07-03 12:01:57 -07:00
|
|
|
}
|
2013-07-05 13:52:19 -07:00
|
|
|
if (create_server_omit_timer(test) < 0) {
|
2013-07-03 12:01:57 -07:00
|
|
|
cleanup_server(test);
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2013-07-03 12:01:57 -07:00
|
|
|
}
|
2013-08-29 11:38:20 -07:00
|
|
|
if (test->reverse)
|
|
|
|
if (iperf_create_send_timers(test) < 0) {
|
|
|
|
cleanup_server(test);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-07-03 12:01:57 -07:00
|
|
|
if (iperf_set_send_state(test, TEST_RUNNING) != 0) {
|
|
|
|
cleanup_server(test);
|
|
|
|
return -1;
|
|
|
|
}
|
2010-06-18 21:08:50 +00:00
|
|
|
}
|
2010-08-02 22:45:53 +00:00
|
|
|
}
|
2010-06-30 22:09:45 +00:00
|
|
|
|
2010-08-02 22:45:53 +00:00
|
|
|
if (test->state == TEST_RUNNING) {
|
|
|
|
if (test->reverse) {
|
|
|
|
// Reverse mode. Server sends.
|
2013-07-03 12:01:57 -07:00
|
|
|
if (iperf_send(test, &write_set) < 0) {
|
|
|
|
cleanup_server(test);
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2013-07-03 12:01:57 -07:00
|
|
|
}
|
2010-08-02 22:45:53 +00:00
|
|
|
} else {
|
|
|
|
// Regular mode. Server receives.
|
2013-07-03 12:01:57 -07:00
|
|
|
if (iperf_recv(test, &read_set) < 0) {
|
|
|
|
cleanup_server(test);
|
2012-12-11 22:29:26 -08:00
|
|
|
return -1;
|
2013-07-03 12:01:57 -07:00
|
|
|
}
|
2010-08-02 22:45:53 +00:00
|
|
|
}
|
2010-07-07 21:54:24 +00:00
|
|
|
|
2012-12-11 22:29:26 -08:00
|
|
|
/* Run the timers. */
|
|
|
|
(void) gettimeofday(&now, NULL);
|
|
|
|
tmr_run(&now);
|
2010-06-18 21:08:50 +00:00
|
|
|
}
|
|
|
|
}
|
2010-08-02 22:45:53 +00:00
|
|
|
}
|
2009-11-06 07:25:10 +00:00
|
|
|
|
2013-07-03 12:01:57 -07:00
|
|
|
cleanup_server(test);
|
2010-06-30 15:58:16 +00:00
|
|
|
|
2013-03-08 20:56:52 -08:00
|
|
|
if (test->json_output) {
|
|
|
|
if (iperf_json_finish(test) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-12-11 22:29:26 -08:00
|
|
|
return 0;
|
2009-11-06 07:25:10 +00:00
|
|
|
}
|