2009-11-02 22:43:19 +00:00
|
|
|
/*
|
2017-04-11 15:11:17 -07:00
|
|
|
* iperf, Copyright (c) 2014, 2015, 2016, 2017, The Regents of the University of
|
2014-09-29 14:00:46 -07:00
|
|
|
* California, through Lawrence Berkeley National Laboratory (subject
|
|
|
|
* to receipt of any required approvals from the U.S. Dept. of
|
|
|
|
* Energy). All rights reserved.
|
2011-04-20 20:33:09 +00:00
|
|
|
*
|
2014-09-29 14:00:46 -07:00
|
|
|
* If you have questions about your rights to use or distribute this
|
|
|
|
* software, please contact Berkeley Lab's Technology Transfer
|
|
|
|
* Department at TTD@lbl.gov.
|
|
|
|
*
|
|
|
|
* NOTICE. This software is owned by the U.S. Department of Energy.
|
|
|
|
* As such, the U.S. Government has been granted for itself and others
|
|
|
|
* acting on its behalf a paid-up, nonexclusive, irrevocable,
|
|
|
|
* worldwide license in the Software to reproduce, prepare derivative
|
|
|
|
* works, and perform publicly and display publicly. Beginning five
|
|
|
|
* (5) years after the date permission to assert copyright is obtained
|
|
|
|
* from the U.S. Department of Energy, and subject to any subsequent
|
|
|
|
* five (5) year renewals, the U.S. Government is granted for itself
|
|
|
|
* and others acting on its behalf a paid-up, nonexclusive,
|
|
|
|
* irrevocable, worldwide license in the Software to reproduce,
|
|
|
|
* prepare derivative works, distribute copies to the public, perform
|
|
|
|
* publicly and display publicly, and to permit others to do so.
|
|
|
|
*
|
|
|
|
* 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>
|
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>
|
|
|
|
#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"
|
2010-07-09 00:29:51 +00:00
|
|
|
#include "iperf_util.h"
|
2014-09-15 10:42:36 -07:00
|
|
|
#include "iperf_locale.h"
|
2009-11-02 22:43:19 +00:00
|
|
|
|
2016-09-22 13:34:52 -07:00
|
|
|
#if defined(HAVE_TCP_CONGESTION)
|
|
|
|
#if !defined(TCP_CA_NAME_MAX)
|
|
|
|
#define TCP_CA_NAME_MAX 16
|
|
|
|
#endif /* TCP_CA_NAME_MAX */
|
|
|
|
#endif /* HAVE_TCP_CONGESTION */
|
2009-11-02 22:43:19 +00:00
|
|
|
|
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) {
|
2016-10-17 16:25:07 -04:00
|
|
|
iperf_printf(test, "-----------------------------------------------------------\n");
|
|
|
|
iperf_printf(test, "Server listening on %d\n", test->server_port);
|
2013-02-01 11:42:35 -08:00
|
|
|
}
|
2010-06-18 21:08:50 +00:00
|
|
|
|
2013-02-01 11:42:35 -08:00
|
|
|
if (!test->json_output)
|
2016-10-17 16:25:07 -04:00
|
|
|
iperf_printf(test, "-----------------------------------------------------------\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);
|
2013-11-08 15:59:17 -08:00
|
|
|
if (test->listener > test->max_fd) test->max_fd = test->listener;
|
2010-06-18 21:08:50 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
iperf_accept(struct iperf_test *test)
|
|
|
|
{
|
|
|
|
int s;
|
2013-11-06 11:05:46 -08:00
|
|
|
signed char rbuf = ACCESS_DENIED;
|
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 */
|
2013-11-08 15:59:17 -08:00
|
|
|
test->ctrl_sck = s;
|
|
|
|
if (Nread(test->ctrl_sck, 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
|
|
|
}
|
2013-11-08 15:59:17 -08:00
|
|
|
FD_SET(test->ctrl_sck, &test->read_set);
|
|
|
|
if (test->ctrl_sck > test->max_fd) test->max_fd = test->ctrl_sck;
|
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-10-25 17:00:52 -07:00
|
|
|
if (test->server_affinity != -1)
|
2014-01-13 10:58:47 -08:00
|
|
|
if (iperf_setaffinity(test, test->server_affinity) != 0)
|
2013-10-25 17:00:52 -07: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 {
|
2014-08-27 14:48:09 -07:00
|
|
|
/*
|
|
|
|
* Don't try to read from the socket. It could block an ongoing test.
|
|
|
|
* Just send ACCESS_DENIED.
|
|
|
|
*/
|
2013-11-06 11:05:46 -08:00
|
|
|
if (Nwrite(s, (char*) &rbuf, sizeof(rbuf), 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:
|
2013-12-05 08:49:27 -08:00
|
|
|
test->done = 1;
|
2013-11-26 14:47:15 -08: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);
|
|
|
|
}
|
2015-05-11 10:44:06 -07:00
|
|
|
test->reporter_callback(test);
|
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;
|
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-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;
|
|
|
|
|
2014-01-07 16:06:27 -08:00
|
|
|
// Temporarily be in DISPLAY_RESULTS phase so we can get
|
|
|
|
// ending summary statistics.
|
|
|
|
signed char oldstate = test->state;
|
|
|
|
cpu_util(test->cpu_util);
|
|
|
|
test->state = DISPLAY_RESULTS;
|
|
|
|
test->reporter_callback(test);
|
|
|
|
test->state = oldstate;
|
|
|
|
|
2010-07-27 22:11:09 +00:00
|
|
|
// 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
|
|
|
|
2016-08-05 16:41:52 -04:00
|
|
|
static void
|
|
|
|
server_timer_proc(TimerClientData client_data, struct timeval *nowP)
|
|
|
|
{
|
|
|
|
struct iperf_test *test = client_data.p;
|
|
|
|
struct iperf_stream *sp;
|
|
|
|
|
|
|
|
test->timer = NULL;
|
|
|
|
if (test->done)
|
|
|
|
return;
|
|
|
|
test->done = 1;
|
|
|
|
/* Free streams */
|
|
|
|
while (!SLIST_EMPTY(&test->streams)) {
|
|
|
|
sp = SLIST_FIRST(&test->streams);
|
|
|
|
SLIST_REMOVE_HEAD(&test->streams, streams);
|
|
|
|
close(sp->socket);
|
|
|
|
iperf_free_stream(sp);
|
|
|
|
}
|
|
|
|
close(test->ctrl_sck);
|
|
|
|
}
|
|
|
|
|
2013-11-01 13:59:33 -07:00
|
|
|
static void
|
|
|
|
server_stats_timer_proc(TimerClientData client_data, struct timeval *nowP)
|
|
|
|
{
|
|
|
|
struct iperf_test *test = client_data.p;
|
|
|
|
|
|
|
|
if (test->done)
|
|
|
|
return;
|
|
|
|
if (test->stats_callback)
|
|
|
|
test->stats_callback(test);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
server_reporter_timer_proc(TimerClientData client_data, struct timeval *nowP)
|
|
|
|
{
|
|
|
|
struct iperf_test *test = client_data.p;
|
|
|
|
|
|
|
|
if (test->done)
|
|
|
|
return;
|
|
|
|
if (test->reporter_callback)
|
|
|
|
test->reporter_callback(test);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
create_server_timers(struct iperf_test * test)
|
|
|
|
{
|
|
|
|
struct timeval now;
|
|
|
|
TimerClientData cd;
|
|
|
|
|
|
|
|
if (gettimeofday(&now, NULL) < 0) {
|
|
|
|
i_errno = IEINITTEST;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
cd.p = test;
|
2016-08-05 16:41:52 -04:00
|
|
|
test->timer = test->stats_timer = test->reporter_timer = NULL;
|
|
|
|
if (test->duration != 0 ) {
|
|
|
|
test->done = 0;
|
|
|
|
test->timer = tmr_create(&now, server_timer_proc, cd, (test->duration + test->omit + 5) * SEC_TO_US, 0);
|
|
|
|
if (test->timer == NULL) {
|
|
|
|
i_errno = IEINITTEST;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-01 13:59:33 -07:00
|
|
|
test->stats_timer = test->reporter_timer = NULL;
|
|
|
|
if (test->stats_interval != 0) {
|
|
|
|
test->stats_timer = tmr_create(&now, server_stats_timer_proc, cd, test->stats_interval * SEC_TO_US, 1);
|
|
|
|
if (test->stats_timer == NULL) {
|
|
|
|
i_errno = IEINITTEST;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (test->reporter_interval != 0) {
|
|
|
|
test->reporter_timer = tmr_create(&now, server_reporter_timer_proc, cd, test->reporter_interval * SEC_TO_US, 1);
|
|
|
|
if (test->reporter_timer == NULL) {
|
|
|
|
i_errno = IEINITTEST;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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);
|
2013-10-24 14:08:04 -07:00
|
|
|
if (test->verbose && !test->json_output && test->reporter_interval == 0)
|
2016-10-17 16:25:07 -04:00
|
|
|
iperf_printf(test, "%s", report_omit_done);
|
2013-11-01 13:59:33 -07:00
|
|
|
|
|
|
|
/* Reset the timers. */
|
|
|
|
if (test->stats_timer != NULL)
|
|
|
|
tmr_reset(nowP, test->stats_timer);
|
|
|
|
if (test->reporter_timer != NULL)
|
|
|
|
tmr_reset(nowP, test->reporter_timer);
|
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;
|
|
|
|
|
2013-10-24 14:08:04 -07:00
|
|
|
if (test->omit == 0) {
|
|
|
|
test->omit_timer = NULL;
|
|
|
|
test->omitting = 0;
|
|
|
|
} else {
|
|
|
|
if (gettimeofday(&now, NULL) < 0) {
|
|
|
|
i_errno = IEINITTEST;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
test->omitting = 1;
|
|
|
|
cd.p = test;
|
|
|
|
test->omit_timer = tmr_create(&now, server_omit_timer_proc, cd, test->omit * SEC_TO_US, 0);
|
|
|
|
if (test->omit_timer == NULL) {
|
|
|
|
i_errno = IEINITTEST;
|
|
|
|
return -1;
|
|
|
|
}
|
2013-07-03 12:01:57 -07:00
|
|
|
}
|
2013-10-24 14:08:04 -07:00
|
|
|
|
2013-07-03 12:01:57 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cleanup_server(struct iperf_test *test)
|
|
|
|
{
|
|
|
|
/* Close open test sockets */
|
2016-09-21 11:25:18 -07:00
|
|
|
if (test->ctrl_sck) {
|
|
|
|
close(test->ctrl_sck);
|
|
|
|
}
|
|
|
|
if (test->listener) {
|
|
|
|
close(test->listener);
|
|
|
|
}
|
2013-11-01 13:59:33 -07:00
|
|
|
|
|
|
|
/* Cancel any remaining timers. */
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
if (test->omit_timer != NULL) {
|
|
|
|
tmr_cancel(test->omit_timer);
|
|
|
|
test->omit_timer = NULL;
|
|
|
|
}
|
2017-04-20 22:33:15 +02:00
|
|
|
if (test->congestion_used != NULL) {
|
|
|
|
free(test->congestion_used);
|
2017-05-04 14:16:42 -07:00
|
|
|
test->congestion_used = NULL;
|
2017-04-20 22:33:15 +02:00
|
|
|
}
|
2016-08-05 16:41:52 -04:00
|
|
|
if (test->timer != NULL) {
|
|
|
|
tmr_cancel(test->timer);
|
|
|
|
test->timer = NULL;
|
|
|
|
}
|
2013-07-03 12:01:57 -07:00
|
|
|
}
|
|
|
|
|
2013-10-29 15:03:17 -07:00
|
|
|
|
2010-06-23 21:34:07 +00:00
|
|
|
int
|
2010-06-18 21:08:50 +00:00
|
|
|
iperf_run_server(struct iperf_test *test)
|
|
|
|
{
|
2017-11-14 15:29:41 -07:00
|
|
|
int result, s, streams_accepted, saved_errno;
|
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;
|
2013-11-15 09:54:21 -08:00
|
|
|
struct timeval* timeout;
|
2010-06-18 21:08:50 +00:00
|
|
|
|
2013-10-25 17:00:52 -07:00
|
|
|
if (test->affinity != -1)
|
2014-01-13 10:58:47 -08:00
|
|
|
if (iperf_setaffinity(test, test->affinity) != 0)
|
2016-06-03 09:26:05 -07:00
|
|
|
return -2;
|
2013-10-25 17:00:52 -07:00
|
|
|
|
2013-03-08 20:56:52 -08:00
|
|
|
if (test->json_output)
|
|
|
|
if (iperf_json_start(test) < 0)
|
2016-06-03 09:26:05 -07:00
|
|
|
return -2;
|
2013-03-08 20:56:52 -08:00
|
|
|
|
|
|
|
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) {
|
2016-10-17 16:25:07 -04:00
|
|
|
iperf_printf(test, "%s\n", version);
|
|
|
|
iperf_printf(test, "%s", "");
|
|
|
|
iperf_printf(test, "%s\n", get_system_info());
|
2014-03-14 15:48:31 -07:00
|
|
|
iflush(test);
|
2013-03-08 20:56:52 -08:00
|
|
|
}
|
|
|
|
|
2010-06-18 21:08:50 +00:00
|
|
|
// Open socket and listen
|
|
|
|
if (iperf_server_listen(test) < 0) {
|
2016-06-03 09:26:05 -07:00
|
|
|
return -2;
|
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
|
|
|
|
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-11-15 09:54:21 -08:00
|
|
|
(void) gettimeofday(&now, NULL);
|
|
|
|
timeout = tmr_timeout(&now);
|
|
|
|
result = select(test->max_fd + 1, &read_set, &write_set, NULL, timeout);
|
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
|
|
|
|
2016-09-22 13:34:52 -07:00
|
|
|
#if defined(HAVE_TCP_CONGESTION)
|
|
|
|
if (test->protocol->id == Ptcp) {
|
|
|
|
if (test->congestion) {
|
|
|
|
if (setsockopt(s, IPPROTO_TCP, TCP_CONGESTION, test->congestion, strlen(test->congestion)) < 0) {
|
2017-04-11 15:11:17 -07:00
|
|
|
/*
|
|
|
|
* ENOENT means we tried to set the
|
|
|
|
* congestion algorithm but the algorithm
|
|
|
|
* specified doesn't exist. This can happen
|
|
|
|
* if the client and server have different
|
|
|
|
* congestion algorithms available. In this
|
|
|
|
* case, print a warning, but otherwise
|
|
|
|
* continue.
|
|
|
|
*/
|
|
|
|
if (errno == ENOENT) {
|
|
|
|
warning("TCP congestion control algorithm not supported");
|
|
|
|
}
|
|
|
|
else {
|
2017-11-14 15:29:41 -07:00
|
|
|
saved_errno = errno;
|
2017-04-11 15:11:17 -07:00
|
|
|
close(s);
|
|
|
|
cleanup_server(test);
|
2017-11-14 15:29:41 -07:00
|
|
|
errno = saved_errno;
|
2017-04-11 15:11:17 -07:00
|
|
|
i_errno = IESETCONGESTION;
|
|
|
|
return -1;
|
|
|
|
}
|
2016-09-22 13:34:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
2017-03-22 20:00:08 +01:00
|
|
|
socklen_t len = TCP_CA_NAME_MAX;
|
2016-09-22 13:34:52 -07:00
|
|
|
char ca[TCP_CA_NAME_MAX + 1];
|
|
|
|
if (getsockopt(s, IPPROTO_TCP, TCP_CONGESTION, ca, &len) < 0) {
|
2017-11-14 15:29:41 -07:00
|
|
|
saved_errno = errno;
|
2016-09-22 13:34:52 -07:00
|
|
|
close(s);
|
|
|
|
cleanup_server(test);
|
2017-11-14 15:29:41 -07:00
|
|
|
errno = saved_errno;
|
2016-09-22 13:34:52 -07:00
|
|
|
i_errno = IESETCONGESTION;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
test->congestion_used = strdup(ca);
|
|
|
|
if (test->debug) {
|
|
|
|
printf("Congestion algorithm is %s\n", test->congestion_used);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* HAVE_TCP_CONGESTION */
|
|
|
|
|
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
|
|
|
|
2013-11-25 11:13:28 -08:00
|
|
|
if (test->sender)
|
|
|
|
FD_SET(s, &test->write_set);
|
|
|
|
else
|
|
|
|
FD_SET(s, &test->read_set);
|
2013-11-08 15:59:17 -08:00
|
|
|
if (s > test->max_fd) test->max_fd = s;
|
2010-07-22 23:26:38 +00:00
|
|
|
|
2014-10-13 04:28:58 -07:00
|
|
|
/*
|
|
|
|
* If the protocol isn't UDP, or even if it is but
|
|
|
|
* we're the receiver, set nonblocking sockets.
|
|
|
|
* We need this to allow a server receiver to
|
|
|
|
* maintain interactivity with the control channel.
|
|
|
|
*/
|
|
|
|
if (test->protocol->id != Pudp ||
|
|
|
|
!test->sender) {
|
2014-04-23 15:01:27 -07:00
|
|
|
setnonblocking(s, 1);
|
|
|
|
}
|
2014-04-16 16:23:13 -07: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);
|
2016-09-21 11:25:18 -07:00
|
|
|
test->listener = 0;
|
2010-08-02 22:45:53 +00:00
|
|
|
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;
|
|
|
|
FD_SET(test->listener, &test->read_set);
|
2013-11-08 15:59:17 -08:00
|
|
|
if (test->listener > test->max_fd) test->max_fd = test->listener;
|
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-11-01 13:59:33 -07:00
|
|
|
if (create_server_timers(test) < 0) {
|
|
|
|
cleanup_server(test);
|
|
|
|
return -1;
|
|
|
|
}
|
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-06-18 21:08:50 +00:00
|
|
|
}
|
|
|
|
}
|
2013-11-26 15:14:07 -08:00
|
|
|
|
|
|
|
if (result == 0 ||
|
|
|
|
(timeout != NULL && timeout->tv_sec == 0 && timeout->tv_usec == 0)) {
|
|
|
|
/* Run the timers. */
|
|
|
|
(void) gettimeofday(&now, NULL);
|
|
|
|
tmr_run(&now);
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2014-03-14 14:23:38 -07:00
|
|
|
iflush(test);
|
|
|
|
|
2013-10-25 17:00:52 -07:00
|
|
|
if (test->server_affinity != -1)
|
2014-01-13 10:58:47 -08:00
|
|
|
if (iperf_clearaffinity(test) != 0)
|
2013-10-25 17:00:52 -07:00
|
|
|
return -1;
|
|
|
|
|
2012-12-11 22:29:26 -08:00
|
|
|
return 0;
|
2009-11-06 07:25:10 +00:00
|
|
|
}
|