1
1
iperf3/src/main.c

165 строки
4.6 KiB
C
Исходник Обычный вид История

/*
* iperf, Copyright (c) 2014, 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.
*
* 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.
*/
#include "iperf_config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <errno.h>
#include <signal.h>
#include <unistd.h>
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif
#include <netinet/tcp.h>
2009-11-03 01:43:19 +03:00
#include "iperf.h"
#include "iperf_api.h"
#include "units.h"
#include "iperf_locale.h"
2010-07-29 23:18:29 +04:00
#include "net.h"
static int run(struct iperf_test *test);
/**************************************************************************/
int
main(int argc, char **argv)
{
struct iperf_test *test;
// XXX: Setting the process affinity requires root on most systems.
// Is this a feature we really need?
#ifdef TEST_PROC_AFFINITY
/* didnt seem to work.... */
/*
* increasing the priority of the process to minimise packet generation
* delay
*/
int rc = setpriority(PRIO_PROCESS, 0, -15);
if (rc < 0) {
perror("setpriority:");
fprintf(stderr, "setting priority to valid level\n");
rc = setpriority(PRIO_PROCESS, 0, 0);
}
/* setting the affinity of the process */
cpu_set_t cpu_set;
int affinity = -1;
int ncores = 1;
sched_getaffinity(0, sizeof(cpu_set_t), &cpu_set);
if (errno)
perror("couldn't get affinity:");
if ((ncores = sysconf(_SC_NPROCESSORS_CONF)) <= 0)
err("sysconf: couldn't get _SC_NPROCESSORS_CONF");
CPU_ZERO(&cpu_set);
CPU_SET(affinity, &cpu_set);
if (sched_setaffinity(0, sizeof(cpu_set_t), &cpu_set) != 0)
err("couldn't change CPU affinity");
#endif
test = iperf_new_test();
if (!test)
iperf_errexit(NULL, "create new test error - %s", iperf_strerror(i_errno));
iperf_defaults(test); /* sets defaults */
if (iperf_parse_arguments(test, argc, argv) < 0) {
iperf_err(test, "parameter error - %s", iperf_strerror(i_errno));
fprintf(stderr, "\n");
usage_long();
exit(1);
}
if (run(test) < 0)
iperf_errexit(test, "error - %s", iperf_strerror(i_errno));
iperf_free_test(test);
return 0;
}
2009-11-03 01:43:19 +03:00
/**************************************************************************/
static int
run(struct iperf_test *test)
2009-11-03 01:43:19 +03:00
{
int consecutive_errors;
switch (test->role) {
case 's':
if (test->daemon) {
int rc = daemon(0, 0);
if (rc < 0) {
i_errno = IEDAEMON;
iperf_errexit(test, "error - %s", iperf_strerror(i_errno));
}
}
consecutive_errors = 0;
if (iperf_create_pidfile(test) < 0) {
i_errno = IEPIDFILE;
iperf_errexit(test, "error - %s", iperf_strerror(i_errno));
}
for (;;) {
Resolves #147. Squashed commit of the following: commit 23ef0d047fb5396df671be9245f7872153fc299c Author: Bruce A. Mah <bmah@es.net> Date: Mon Apr 7 13:35:29 2014 -0700 Add a few API calls to the client-side example program so we can exercise recently-added JSON-related functionality. commit 5f8301e8d0380133d533da9b2e39ca4ac522e1c3 Author: Bruce A. Mah <bmah@es.net> Date: Mon Apr 7 13:16:39 2014 -0700 Revert part of earlier change. We still want to save the JSON for libiperf consumers that might want it, but preserve the prior behavior of writing that JSON to stdout. This maintains (roughly) the behavior of older libiperf, in which libiperf consumers (such as the iperf3 executable) do not need to explicitly print the JSON if that's all they're doing with it. commit 173dcdb05867af00103205bfe39d1b71e18689e9 Author: Bruce A. Mah <bmah@es.net> Date: Tue Mar 25 13:55:45 2014 -0700 Update manpage for newly-added library calls. Bump document date while here. Part of Issue #147. commit 51a275de9463febc440d41cee9d971fcd381e01c Author: Bruce A. Mah <bmah@es.net> Date: Tue Mar 25 13:30:09 2014 -0700 Allow consumers of libiperf3 to get the JSON output for a just-completed test. This changes the behavior of iperf_json_finish() so that it no longer outputs JSON output, but saves the rendered output in a NUL-terminated string buffer. After calling iperf_run_server() or iperf_run_client(), the client application should check iperf_get_test_json_output() to see if it returns a non-NULL pointer. If so, there is JSON data available for it to print or otherwise consume. The buffer is automatically deallocated when the containing iperf_test structure is deallocated with iperf_free_test(). Also adds a new API call iperf_get_test_outfile() to find the output FILE* structure. Modifies the iperf3 application to use the new API. Users of iperf3 will not notice any functional change. No effect in "normal" output mode (non-JSON).
2014-04-08 01:12:47 +04:00
if (iperf_run_server(test) < 0) {
iperf_err(test, "error - %s", iperf_strerror(i_errno));
fprintf(stderr, "\n");
++consecutive_errors;
if (consecutive_errors >= 5) {
fprintf(stderr, "too many errors, exiting\n");
break;
}
} else
consecutive_errors = 0;
iperf_reset_test(test);
}
iperf_delete_pidfile(test);
break;
Resolves #147. Squashed commit of the following: commit 23ef0d047fb5396df671be9245f7872153fc299c Author: Bruce A. Mah <bmah@es.net> Date: Mon Apr 7 13:35:29 2014 -0700 Add a few API calls to the client-side example program so we can exercise recently-added JSON-related functionality. commit 5f8301e8d0380133d533da9b2e39ca4ac522e1c3 Author: Bruce A. Mah <bmah@es.net> Date: Mon Apr 7 13:16:39 2014 -0700 Revert part of earlier change. We still want to save the JSON for libiperf consumers that might want it, but preserve the prior behavior of writing that JSON to stdout. This maintains (roughly) the behavior of older libiperf, in which libiperf consumers (such as the iperf3 executable) do not need to explicitly print the JSON if that's all they're doing with it. commit 173dcdb05867af00103205bfe39d1b71e18689e9 Author: Bruce A. Mah <bmah@es.net> Date: Tue Mar 25 13:55:45 2014 -0700 Update manpage for newly-added library calls. Bump document date while here. Part of Issue #147. commit 51a275de9463febc440d41cee9d971fcd381e01c Author: Bruce A. Mah <bmah@es.net> Date: Tue Mar 25 13:30:09 2014 -0700 Allow consumers of libiperf3 to get the JSON output for a just-completed test. This changes the behavior of iperf_json_finish() so that it no longer outputs JSON output, but saves the rendered output in a NUL-terminated string buffer. After calling iperf_run_server() or iperf_run_client(), the client application should check iperf_get_test_json_output() to see if it returns a non-NULL pointer. If so, there is JSON data available for it to print or otherwise consume. The buffer is automatically deallocated when the containing iperf_test structure is deallocated with iperf_free_test(). Also adds a new API call iperf_get_test_outfile() to find the output FILE* structure. Modifies the iperf3 application to use the new API. Users of iperf3 will not notice any functional change. No effect in "normal" output mode (non-JSON).
2014-04-08 01:12:47 +04:00
case 'c':
if (iperf_run_client(test) < 0)
iperf_errexit(test, "error - %s", iperf_strerror(i_errno));
break;
default:
usage();
break;
2009-11-03 01:43:19 +03:00
}
return 0;
2009-11-03 01:43:19 +03:00
}