Annotation of embedaddon/iperf/src/main.c, revision 1.1.1.3

1.1       misho       1: /*
1.1.1.3 ! misho       2:  * iperf, Copyright (c) 2014-2022, The Regents of the University of
1.1       misho       3:  * California, through Lawrence Berkeley National Laboratory (subject
                      4:  * to receipt of any required approvals from the U.S. Dept. of
                      5:  * Energy).  All rights reserved.
                      6:  *
                      7:  * If you have questions about your rights to use or distribute this
                      8:  * software, please contact Berkeley Lab's Technology Transfer
                      9:  * Department at TTD@lbl.gov.
                     10:  *
                     11:  * NOTICE.  This software is owned by the U.S. Department of Energy.
                     12:  * As such, the U.S. Government has been granted for itself and others
                     13:  * acting on its behalf a paid-up, nonexclusive, irrevocable,
                     14:  * worldwide license in the Software to reproduce, prepare derivative
                     15:  * works, and perform publicly and display publicly.  Beginning five
                     16:  * (5) years after the date permission to assert copyright is obtained
                     17:  * from the U.S. Department of Energy, and subject to any subsequent
                     18:  * five (5) year renewals, the U.S. Government is granted for itself
                     19:  * and others acting on its behalf a paid-up, nonexclusive,
                     20:  * irrevocable, worldwide license in the Software to reproduce,
                     21:  * prepare derivative works, distribute copies to the public, perform
                     22:  * publicly and display publicly, and to permit others to do so.
                     23:  *
                     24:  * This code is distributed under a BSD style license, see the LICENSE
                     25:  * file for complete information.
                     26:  */
                     27: #include "iperf_config.h"
                     28: 
                     29: #include <stdio.h>
                     30: #include <stdlib.h>
                     31: #include <string.h>
                     32: #include <getopt.h>
                     33: #include <errno.h>
                     34: #include <signal.h>
                     35: #include <unistd.h>
                     36: #ifdef HAVE_STDINT_H
                     37: #include <stdint.h>
                     38: #endif
                     39: #include <sys/socket.h>
                     40: #include <sys/types.h>
                     41: #include <netinet/in.h>
                     42: #include <arpa/inet.h>
                     43: #include <netdb.h>
                     44: 
                     45: #include "iperf.h"
                     46: #include "iperf_api.h"
1.1.1.2   misho      47: #include "iperf_util.h"
1.1       misho      48: #include "iperf_locale.h"
                     49: #include "net.h"
1.1.1.2   misho      50: #include "units.h"
1.1       misho      51: 
                     52: 
                     53: static int run(struct iperf_test *test);
                     54: 
                     55: 
                     56: /**************************************************************************/
                     57: int
                     58: main(int argc, char **argv)
                     59: {
                     60:     struct iperf_test *test;
                     61: 
                     62:     // XXX: Setting the process affinity requires root on most systems.
                     63:     //      Is this a feature we really need?
                     64: #ifdef TEST_PROC_AFFINITY
1.1.1.3 ! misho      65:     /* didn't seem to work.... */
1.1       misho      66:     /*
                     67:      * increasing the priority of the process to minimise packet generation
                     68:      * delay
                     69:      */
                     70:     int rc = setpriority(PRIO_PROCESS, 0, -15);
                     71: 
                     72:     if (rc < 0) {
                     73:         perror("setpriority:");
                     74:         fprintf(stderr, "setting priority to valid level\n");
                     75:         rc = setpriority(PRIO_PROCESS, 0, 0);
                     76:     }
1.1.1.3 ! misho      77: 
1.1       misho      78:     /* setting the affinity of the process  */
                     79:     cpu_set_t cpu_set;
                     80:     int affinity = -1;
                     81:     int ncores = 1;
                     82: 
                     83:     sched_getaffinity(0, sizeof(cpu_set_t), &cpu_set);
                     84:     if (errno)
                     85:         perror("couldn't get affinity:");
                     86: 
                     87:     if ((ncores = sysconf(_SC_NPROCESSORS_CONF)) <= 0)
                     88:         err("sysconf: couldn't get _SC_NPROCESSORS_CONF");
                     89: 
                     90:     CPU_ZERO(&cpu_set);
                     91:     CPU_SET(affinity, &cpu_set);
                     92:     if (sched_setaffinity(0, sizeof(cpu_set_t), &cpu_set) != 0)
                     93:         err("couldn't change CPU affinity");
                     94: #endif
                     95: 
                     96:     test = iperf_new_test();
                     97:     if (!test)
                     98:         iperf_errexit(NULL, "create new test error - %s", iperf_strerror(i_errno));
                     99:     iperf_defaults(test);      /* sets defaults */
                    100: 
                    101:     if (iperf_parse_arguments(test, argc, argv) < 0) {
                    102:         iperf_err(test, "parameter error - %s", iperf_strerror(i_errno));
                    103:         fprintf(stderr, "\n");
1.1.1.3 ! misho     104:         usage();
1.1       misho     105:         exit(1);
                    106:     }
                    107: 
                    108:     if (run(test) < 0)
                    109:         iperf_errexit(test, "error - %s", iperf_strerror(i_errno));
                    110: 
                    111:     iperf_free_test(test);
                    112: 
                    113:     return 0;
                    114: }
                    115: 
                    116: 
                    117: static jmp_buf sigend_jmp_buf;
                    118: 
1.1.1.2   misho     119: static void __attribute__ ((noreturn))
1.1       misho     120: sigend_handler(int sig)
                    121: {
                    122:     longjmp(sigend_jmp_buf, 1);
                    123: }
                    124: 
                    125: /**************************************************************************/
                    126: static int
                    127: run(struct iperf_test *test)
                    128: {
                    129:     /* Termination signals. */
                    130:     iperf_catch_sigend(sigend_handler);
                    131:     if (setjmp(sigend_jmp_buf))
                    132:        iperf_got_sigend(test);
                    133: 
1.1.1.2   misho     134:     /* Ignore SIGPIPE to simplify error handling */
                    135:     signal(SIGPIPE, SIG_IGN);
                    136: 
1.1       misho     137:     switch (test->role) {
                    138:         case 's':
                    139:            if (test->daemon) {
1.1.1.2   misho     140:                int rc;
                    141:                rc = daemon(0, 0);
1.1       misho     142:                if (rc < 0) {
                    143:                    i_errno = IEDAEMON;
                    144:                    iperf_errexit(test, "error - %s", iperf_strerror(i_errno));
                    145:                }
                    146:            }
                    147:            if (iperf_create_pidfile(test) < 0) {
                    148:                i_errno = IEPIDFILE;
                    149:                iperf_errexit(test, "error - %s", iperf_strerror(i_errno));
                    150:            }
                    151:             for (;;) {
                    152:                int rc;
                    153:                rc = iperf_run_server(test);
1.1.1.3 ! misho     154:                 test->server_last_run_rc = rc;
1.1       misho     155:                if (rc < 0) {
                    156:                    iperf_err(test, "error - %s", iperf_strerror(i_errno));
1.1.1.3 ! misho     157:                     if (test->json_output) {
        !           158:                         if (iperf_json_finish(test) < 0)
        !           159:                             return -1;
        !           160:                     }
        !           161:                     iflush(test);
        !           162: 
1.1       misho     163:                    if (rc < -1) {
                    164:                        iperf_errexit(test, "exiting");
                    165:                    }
                    166:                 }
                    167:                 iperf_reset_test(test);
1.1.1.3 ! misho     168:                 if (iperf_get_test_one_off(test) && rc != 2) {
1.1.1.2   misho     169:                    /* Authentication failure doesn't count for 1-off test */
                    170:                    if (rc < 0 && i_errno == IEAUTHTEST) {
                    171:                        continue;
                    172:                    }
                    173:                    break;
                    174:                }
1.1       misho     175:             }
                    176:            iperf_delete_pidfile(test);
                    177:             break;
                    178:        case 'c':
1.1.1.3 ! misho     179:            if (iperf_create_pidfile(test) < 0) {
        !           180:                i_errno = IEPIDFILE;
        !           181:                iperf_errexit(test, "error - %s", iperf_strerror(i_errno));
        !           182:            }
1.1       misho     183:            if (iperf_run_client(test) < 0)
                    184:                iperf_errexit(test, "error - %s", iperf_strerror(i_errno));
1.1.1.3 ! misho     185:            iperf_delete_pidfile(test);
1.1       misho     186:             break;
                    187:         default:
                    188:             usage();
                    189:             break;
                    190:     }
                    191: 
                    192:     iperf_catch_sigend(SIG_DFL);
1.1.1.2   misho     193:     signal(SIGPIPE, SIG_DFL);
1.1       misho     194: 
                    195:     return 0;
                    196: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>