2009-11-02 22:43:19 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2009, 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* 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 <signal.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_server_api.h"
|
|
|
|
#include "iperf_api.h"
|
|
|
|
#include "iperf_udp.h"
|
|
|
|
#include "iperf_tcp.h"
|
|
|
|
#include "timer.h"
|
|
|
|
#include "net.h"
|
|
|
|
#include "units.h"
|
|
|
|
#include "tcp_window_size.h"
|
|
|
|
#include "uuid.h"
|
|
|
|
#include "locale.h"
|
|
|
|
|
2010-06-23 19:13:37 +00:00
|
|
|
jmp_buf env;
|
2009-11-02 22:43:19 +00:00
|
|
|
|
2010-06-23 19:13:37 +00:00
|
|
|
/* send_result_to_client - sends result string from server to client */
|
2009-11-02 22:43:19 +00:00
|
|
|
void
|
|
|
|
send_result_to_client(struct iperf_stream * sp)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
int size = sp->settings->blksize;
|
|
|
|
|
|
|
|
char *buf = (char *) malloc(size);
|
|
|
|
|
|
|
|
if (!buf)
|
|
|
|
{
|
|
|
|
perror("malloc: unable to allocate transmit buffer");
|
|
|
|
}
|
|
|
|
/* adding the string terminator to the message */
|
|
|
|
buf[strlen((char *) sp->data)] = '\0';
|
|
|
|
|
|
|
|
memcpy(buf, sp->data, strlen((char *) sp->data));
|
|
|
|
|
|
|
|
printf("send_result_to_client: sending %d bytes \n", (int) strlen((char *) sp->data));
|
|
|
|
result = send(sp->socket, buf, size, 0);
|
|
|
|
printf("RESULT SENT TO CLIENT\n");
|
|
|
|
|
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
|
2010-06-18 21:08:50 +00:00
|
|
|
int
|
|
|
|
iperf_server_listen(struct iperf_test *test)
|
2009-11-02 22:43:19 +00:00
|
|
|
{
|
2010-06-18 21:08:50 +00:00
|
|
|
char ubuf[UNIT_LEN];
|
|
|
|
int x;
|
|
|
|
|
|
|
|
if((test->listener = netannounce(Ptcp, NULL, test->server_port)) < 0) {
|
|
|
|
// Needs to set some sort of error number/message
|
2010-06-23 19:13:37 +00:00
|
|
|
perror("netannounce test->listener");
|
2010-06-18 21:08:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
setnonblocking(test->listener);
|
2009-11-02 22:43:19 +00:00
|
|
|
|
2010-06-18 21:08:50 +00:00
|
|
|
printf("-----------------------------------------------------------\n");
|
|
|
|
printf("Server listening on %d\n", test->server_port);
|
|
|
|
|
|
|
|
// 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-06-18 21:08:50 +00:00
|
|
|
if ((x = get_tcp_windowsize(test->listener_sock_tcp, SO_RCVBUF)) < 0) {
|
|
|
|
// 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
|
|
|
|
// XXX: Last thing I was working on
|
2010-06-18 21:08:50 +00:00
|
|
|
if (test->protocol == Ptcp) {
|
|
|
|
if (test->default_settings->socket_bufsize > 0) {
|
|
|
|
unit_snprintf(ubuf, UNIT_LEN, (double) x, 'A');
|
|
|
|
printf("TCP window size: %s\n", ubuf);
|
|
|
|
} else {
|
|
|
|
printf("Using TCP Autotuning\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("-----------------------------------------------------------\n");
|
2009-11-06 02:19:20 +00:00
|
|
|
|
2009-11-02 22:43:19 +00:00
|
|
|
FD_ZERO(&test->read_set);
|
2009-11-10 04:41:42 +00:00
|
|
|
FD_ZERO(&test->temp_set);
|
2010-06-18 21:08:50 +00:00
|
|
|
FD_SET(test->listener, &test->read_set);
|
2010-06-23 19:13:37 +00:00
|
|
|
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-18 21:08:50 +00:00
|
|
|
char ipl[512], ipr[512];
|
|
|
|
socklen_t len;
|
|
|
|
struct sockaddr_in addr;
|
2010-06-23 19:13:37 +00:00
|
|
|
struct sockaddr_in temp1, temp2;
|
2010-06-18 21:08:50 +00:00
|
|
|
|
2010-06-23 19:13:37 +00:00
|
|
|
if (test->ctrl_sck == -1) {
|
2010-06-18 21:08:50 +00:00
|
|
|
if ((s = accept(test->listener, (struct sockaddr *) &addr, &len)) < 0) {
|
|
|
|
perror("accept");
|
|
|
|
return -1;
|
|
|
|
}
|
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
|
|
|
|
2010-06-23 19:13:37 +00:00
|
|
|
len = sizeof(struct sockaddr_in);
|
|
|
|
if (getsockname(s, (struct sockaddr *) &temp1, &len) < 0)
|
|
|
|
perror("getsockname");
|
|
|
|
if (getpeername(s, (struct sockaddr *) &temp2, &len) < 0)
|
|
|
|
perror("getpeername");
|
|
|
|
|
|
|
|
inet_ntop(AF_INET, (void *) &temp1.sin_addr, ipl, sizeof(ipl));
|
|
|
|
inet_ntop(AF_INET, (void *) &temp2.sin_addr, ipr, sizeof(ipr));
|
|
|
|
printf(report_peer, s, ipl, ntohs(temp1.sin_port), ipr, ntohs(temp2.sin_port));
|
|
|
|
|
|
|
|
return 0;
|
2010-06-18 21:08:50 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
// This message needs to be sent to the client
|
2010-06-23 19:13:37 +00:00
|
|
|
if ((s = accept(test->listener, (struct sockaddr *) &addr, &len)) < 0) {
|
|
|
|
perror("accept");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (write(s, &rbuf, sizeof(int)) < 0) {
|
|
|
|
perror("write");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
close(s);
|
2010-06-18 21:08:50 +00:00
|
|
|
return 0;
|
2009-11-10 04:41:42 +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
|
|
|
{
|
|
|
|
if (read(test->ctrl_sck, &test->state, sizeof(int)) < 0) {
|
2010-06-21 15:08:47 +00:00
|
|
|
// XXX: Needs to indicate read error
|
2010-06-18 21:08:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(test->state) {
|
|
|
|
case PARAM_EXCHANGE:
|
|
|
|
iperf_exchange_parameters(test);
|
|
|
|
break;
|
2010-06-21 15:08:47 +00:00
|
|
|
case TEST_START:
|
|
|
|
break;
|
2010-06-23 19:13:37 +00:00
|
|
|
case TEST_RUNNING:
|
|
|
|
break;
|
|
|
|
case TEST_END:
|
2010-06-23 20:28:32 +00:00
|
|
|
test->state = DISPLAY_RESULTS;
|
2010-06-23 19:13:37 +00:00
|
|
|
if (write(test->ctrl_sck, &test->state, sizeof(int)) < 0) {
|
2010-06-23 20:28:32 +00:00
|
|
|
perror("write DISPLAY_RESULTS");
|
2010-06-23 19:13:37 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
break;
|
2010-06-23 20:28:32 +00:00
|
|
|
case IPERF_DONE:
|
|
|
|
break;
|
2010-06-23 19:13:37 +00:00
|
|
|
case CLIENT_TERMINATE:
|
|
|
|
fprintf(stderr, "The client has terminated. Exiting...\n");
|
2010-06-21 15:08:47 +00:00
|
|
|
exit(1);
|
|
|
|
default:
|
|
|
|
// XXX: This needs to be replaced by actual error handling
|
|
|
|
fprintf("How did you get here? test->state = %d\n", test->state);
|
|
|
|
return -1;
|
2010-06-18 21:08:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2009-11-02 22:43:19 +00:00
|
|
|
|
2010-06-18 21:08:50 +00:00
|
|
|
void
|
|
|
|
iperf_run_server(struct iperf_test *test)
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
struct iperf_stream *sp;
|
|
|
|
struct timer *stats_interval, *reporter_interval;
|
|
|
|
char *result_string = NULL;
|
|
|
|
int j = 0, result = 0, message = 0;
|
|
|
|
int nfd = 0;
|
2010-06-23 19:13:37 +00:00
|
|
|
int streams_accepted = 0;
|
2010-06-18 21:08:50 +00:00
|
|
|
|
|
|
|
// Open socket and listen
|
|
|
|
if (iperf_server_listen(test) < 0) {
|
|
|
|
// This needs to be replaced by more formal error handling
|
|
|
|
fprintf(stderr, "An error occurred. Exiting.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2009-11-02 22:43:19 +00:00
|
|
|
|
2010-06-23 19:13:37 +00:00
|
|
|
signal(SIGINT, sig_handler);
|
|
|
|
if (setjmp(env)) {
|
|
|
|
fprintf(stderr, "Interrupt received. Exiting...\n");
|
|
|
|
test->state = SERVER_TERMINATE;
|
|
|
|
if (test->ctrl_sck >= 0) {
|
|
|
|
if (write(test->ctrl_sck, &test->state, sizeof(int)) < 0) {
|
|
|
|
fprintf(stderr, "Unable to send SERVER_TERMINATE message to client\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2009-11-02 22:43:19 +00:00
|
|
|
test->default_settings->state = TEST_RUNNING;
|
|
|
|
|
|
|
|
printf("iperf_run_server: Waiting for client connect.... \n");
|
2009-11-06 02:19:20 +00:00
|
|
|
|
2010-06-23 20:28:32 +00:00
|
|
|
while (test->state != IPERF_DONE) {
|
2010-06-18 21:08:50 +00:00
|
|
|
|
|
|
|
FD_COPY(&test->read_set, &test->temp_set);
|
|
|
|
tv.tv_sec = 15;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
|
|
|
|
result = select(test->max_fd + 1, &test->temp_set, NULL, NULL, &tv);
|
|
|
|
if (result < 0 && errno != EINTR) {
|
|
|
|
// Change the way this handles errors
|
|
|
|
perror("select");
|
|
|
|
exit(1);
|
|
|
|
} else if (result > 0) {
|
|
|
|
if (FD_ISSET(test->listener, &test->temp_set)) {
|
2010-06-23 19:13:37 +00:00
|
|
|
if (iperf_accept(test) < 0) {
|
|
|
|
fprintf(stderr, "iperf_accept: error accepting control socket. exiting...\n");
|
2010-06-18 21:08:50 +00:00
|
|
|
exit(1);
|
2010-06-23 19:13:37 +00:00
|
|
|
}
|
2010-06-18 21:08:50 +00:00
|
|
|
FD_CLR(test->listener, &test->temp_set);
|
|
|
|
}
|
|
|
|
if (FD_ISSET(test->ctrl_sck, &test->temp_set)) {
|
|
|
|
// Handle control messages
|
2010-06-23 19:13:37 +00:00
|
|
|
iperf_handle_message_server(test);
|
2010-06-18 21:08:50 +00:00
|
|
|
FD_CLR(test->ctrl_sck, &test->temp_set);
|
|
|
|
}
|
2010-06-23 19:13:37 +00:00
|
|
|
|
|
|
|
if (test->state == CREATE_STREAMS) {
|
|
|
|
if (FD_ISSET(test->prot_listener, &test->temp_set)) {
|
|
|
|
// Spawn new streams
|
|
|
|
// XXX: This works, but should it check cookies for authenticity
|
|
|
|
// Also, currently it uses 5202 for stream connections.
|
|
|
|
// Should this be fixed to use 5201 instead?
|
|
|
|
iperf_tcp_accept(test);
|
|
|
|
++streams_accepted;
|
|
|
|
FD_CLR(test->prot_listener, &test->temp_set);
|
2010-06-18 21:08:50 +00:00
|
|
|
}
|
2010-06-23 19:13:37 +00:00
|
|
|
if (streams_accepted == test->num_streams) {
|
|
|
|
FD_CLR(test->prot_listener, &test->read_set);
|
|
|
|
close(test->prot_listener);
|
|
|
|
test->state = TEST_START;
|
|
|
|
if (write(test->ctrl_sck, &test->state, sizeof(int)) < 0) {
|
|
|
|
perror("write TEST_START");
|
|
|
|
return -1;
|
|
|
|
}
|
2010-06-18 21:08:50 +00:00
|
|
|
}
|
|
|
|
}
|
2010-06-23 19:13:37 +00:00
|
|
|
|
|
|
|
if (test->reverse) {
|
|
|
|
// Reverse mode. Server sends.
|
|
|
|
iperf_send(test);
|
|
|
|
} else {
|
|
|
|
// Regular mode. Server receives.
|
|
|
|
iperf_recv(test);
|
2010-06-18 21:08:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-11-06 07:25:10 +00:00
|
|
|
|
2009-11-03 05:37:06 +00:00
|
|
|
printf("Test Complete. \n\n");
|
2009-11-10 05:21:17 +00:00
|
|
|
|
2009-11-06 07:25:10 +00:00
|
|
|
/* reset cookie when client is finished */
|
2009-11-16 02:50:24 +00:00
|
|
|
/* XXX: which cookie to reset, and why is it stored to 2 places? */
|
|
|
|
//memset(test->streams->settings->cookie, '\0', COOKIE_SIZE);
|
2010-06-23 19:13:37 +00:00
|
|
|
/* All memory for the previous run needs to be freed here */
|
2009-11-16 02:50:24 +00:00
|
|
|
memset(test->default_settings->cookie, '\0', COOKIE_SIZE);
|
2009-11-03 05:37:06 +00:00
|
|
|
return;
|
2009-11-06 07:25:10 +00:00
|
|
|
}
|
|
|
|
|