]> sjero.net Git - iperf/blob - src/main.cpp
DCCP support for iperf
[iperf] / src / main.cpp
1 /*--------------------------------------------------------------- 
2  * Copyright (c) 1999,2000,2001,2002,2003                              
3  * The Board of Trustees of the University of Illinois            
4  * All Rights Reserved.                                           
5  *--------------------------------------------------------------- 
6  * Permission is hereby granted, free of charge, to any person    
7  * obtaining a copy of this software (Iperf) and associated       
8  * documentation files (the "Software"), to deal in the Software  
9  * without restriction, including without limitation the          
10  * rights to use, copy, modify, merge, publish, distribute,        
11  * sublicense, and/or sell copies of the Software, and to permit     
12  * persons to whom the Software is furnished to do
13  * so, subject to the following conditions: 
14  *
15  *     
16  * Redistributions of source code must retain the above 
17  * copyright notice, this list of conditions and 
18  * the following disclaimers. 
19  *
20  *     
21  * Redistributions in binary form must reproduce the above 
22  * copyright notice, this list of conditions and the following 
23  * disclaimers in the documentation and/or other materials 
24  * provided with the distribution. 
25  * 
26  *     
27  * Neither the names of the University of Illinois, NCSA, 
28  * nor the names of its contributors may be used to endorse 
29  * or promote products derived from this Software without
30  * specific prior written permission. 
31  * 
32  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
33  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
34  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
35  * NONINFRINGEMENT. IN NO EVENT SHALL THE CONTIBUTORS OR COPYRIGHT 
36  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
37  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
38  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE
39  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
40  * ________________________________________________________________
41  * National Laboratory for Applied Network Research 
42  * National Center for Supercomputing Applications 
43  * University of Illinois at Urbana-Champaign 
44  * http://www.ncsa.uiuc.edu
45  * ________________________________________________________________ 
46  * main.cpp
47  * by Mark Gates <mgates@nlanr.net>
48  * &  Ajay Tirumala <tirumala@ncsa.uiuc.edu>
49  * -------------------------------------------------------------------
50  * main does initialization and creates the various objects that will
51  * actually run the iperf program, then waits in the Joinall().
52  * -------------------------------------------------------------------
53  * headers
54  * uses
55  *   <stdlib.h>
56  *   <string.h>
57  *
58  *   <signal.h>
59  * ------------------------------------------------------------------- */
60
61 #define HEADERS()
62
63 #include "headers.h"
64
65 #include "Settings.hpp"
66 #include "PerfSocket.hpp"
67 #include "Locale.h"
68 #include "Condition.h"
69 #include "Timestamp.hpp"
70 #include "Listener.hpp"
71 #include "List.h"
72 #include "util.h"
73
74 /* -------------------------------------------------------------------
75  * prototypes
76  * ------------------------------------------------------------------- */
77 // Function called at exit to clean up as much as possible
78 void cleanup( void );
79
80 /* -------------------------------------------------------------------
81  * global variables
82  * ------------------------------------------------------------------- */
83 extern "C" {
84     // Global flag to signal a user interrupt
85     int sInterupted = 0;
86     // Global ID that we increment to be used 
87     // as identifier for SUM reports
88     int groupID = 0;
89     // Mutex to protect access to the above ID
90     Mutex groupCond;
91     // Condition used to signify advances of the current
92     // records being accessed in a report and also to
93     // serialize modification of the report list
94     Condition ReportCond;
95     Condition ReportDoneCond;
96 }
97
98 // global variables only accessed within this file
99
100 // Thread that received the SIGTERM or SIGINT signal
101 // Used to ensure that if multiple threads receive the
102 // signal we do not prematurely exit
103 nthread_t sThread;
104 // The main thread uses this function to wait 
105 // for all other threads to complete
106 void waitUntilQuit( void );
107
108 /* -------------------------------------------------------------------
109  * main()
110  *      Entry point into Iperf
111  *
112  * sets up signal handlers
113  * initialize global locks and conditions
114  * parses settings from environment and command line
115  * starts up server or client thread
116  * waits for all threads to complete
117  * ------------------------------------------------------------------- */
118 int main( int argc, char **argv ) {
119
120     // Set SIGTERM and SIGINT to call our user interrupt function
121     my_signal( SIGTERM, Sig_Interupt );
122     my_signal( SIGINT,  Sig_Interupt );
123
124
125     // Initialize global mutexes and conditions
126     Condition_Initialize ( &ReportCond );
127     Condition_Initialize ( &ReportDoneCond );
128     Mutex_Initialize( &groupCond );
129     Mutex_Initialize( &clients_mutex );
130
131     // Initialize the thread subsystem
132     thread_init( );
133
134     // Initialize the interrupt handling thread to 0
135     sThread = thread_zeroid();
136
137     // perform any cleanup when quitting Iperf
138     atexit( cleanup );
139
140     // Allocate the "global" settings
141     thread_Settings* ext_gSettings = new thread_Settings;
142
143     // Initialize settings to defaults
144     Settings_Initialize( ext_gSettings );
145     // read settings from environment variables
146     Settings_ParseEnvironment( ext_gSettings );
147     // read settings from command-line parameters
148     Settings_ParseCommandLine( argc, argv, ext_gSettings );
149
150     if (isPacketOriented(ext_gSettings) &&
151         !(ext_gSettings->mProtocol == kProto_UDP ||
152           ext_gSettings->mProtocol == kProto_DCCP       ))
153             die("Can't use packet-oriented mode with these settings.");
154
155     if (isSingleUDP(ext_gSettings) && ext_gSettings->mProtocol != kProto_UDP) {
156         fprintf(stderr, "WARNING: option -U applies to UDP only, ignored!\n");
157         unsetSingleUDP(ext_gSettings);
158     }
159
160     if (!isModeTime(ext_gSettings) &&
161         (isConnectionLess(ext_gSettings) || isPacketOriented(ext_gSettings)))
162             die("Amount-oriented (-n) works only in non-packet-oriented mode.");
163
164     // Check for either having specified client or server
165     if ( ext_gSettings->mThreadMode == kMode_Client 
166          || ext_gSettings->mThreadMode == kMode_Listener ) {
167         // initialize client(s)
168         if ( ext_gSettings->mThreadMode == kMode_Client ) {
169             client_init( ext_gSettings );
170         }
171
172 #ifdef HAVE_THREAD
173         // start up the reporter and client(s) or listener
174         {
175             thread_Settings *into = NULL;
176             // Create the settings structure for the reporter thread
177             Settings_Copy( ext_gSettings, &into );
178             into->mThreadMode = kMode_Reporter;
179
180             // Have the reporter launch the client or listener
181             into->runNow = ext_gSettings;
182             
183             // Start all the threads that are ready to go
184             thread_start( into );
185         }
186 #else
187         // No need to make a reporter thread because we don't have threads
188         thread_start( ext_gSettings );
189 #endif
190     } else {
191         // neither server nor client mode was specified
192         // print usage and exit
193
194         fprintf( stderr, usage_short, argv[0], argv[0] );
195
196         return 0;
197     }
198
199     // wait for other (client, server) threads to complete
200     thread_joinall();
201     
202     // all done!
203     return 0;
204 } // end main
205
206 /* -------------------------------------------------------------------
207  * Signal handler sets the sInterupted flag, so the object can
208  * respond appropriately.. [static]
209  * ------------------------------------------------------------------- */
210
211 void Sig_Interupt( int inSigno ) {
212 #ifdef HAVE_THREAD
213     // We try to not allow a single interrupt handled by multiple threads
214     // to completely kill the app so we save off the first thread ID
215     // then that is the only thread that can supply the next interrupt
216     if ( thread_equalid( sThread, thread_zeroid() ) ) {
217         sThread = thread_getid();
218     } else if ( thread_equalid( sThread, thread_getid() ) ) {
219         sig_exit( inSigno );
220     }
221
222     // global variable used by threads to see if they were interrupted
223     sInterupted = 1;
224
225     // with threads, stop waiting for non-terminating threads
226     // (ie Listener Thread)
227     thread_release_nonterm( 1 );
228
229 #else
230     // without threads, just exit quietly, same as sig_exit()
231     sig_exit( inSigno );
232 #endif
233 }
234
235 /* -------------------------------------------------------------------
236  * Any necesary cleanup before Iperf quits. Called at program exit,
237  * either by exit() or terminating main().
238  * ------------------------------------------------------------------- */
239
240 void cleanup( void ) {
241     // clean up the list of clients
242     Iperf_destroy ( &clients );
243
244     // shutdown the thread subsystem
245     thread_destroy( );
246 } // end cleanup
247