]> sjero.net Git - iperf/blob - include/Settings.hpp
6383f48a02a510b9643e8601ddac2a57fb863559
[iperf] / include / Settings.hpp
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  *
47  * Settings.hpp
48  * by Mark Gates <mgates@nlanr.net>
49  * &  Ajay Tirumala <tirumala@ncsa.uiuc.edu>
50  * -------------------------------------------------------------------
51  * Stores and parses the initial values for all the global variables.
52  * -------------------------------------------------------------------
53  * headers
54  * uses
55  *   <stdlib.h>
56  *   <assert.h>
57  * ------------------------------------------------------------------- */
58
59 #ifndef SETTINGS_H
60 #define SETTINGS_H
61
62 #include "headers.h"
63 #include "Thread.h"
64
65 /* -------------------------------------------------------------------
66  * constants
67  * ------------------------------------------------------------------- */
68 #ifdef __cplusplus
69 extern "C" {
70 #endif
71
72 // Convention: odd=connection-oriented, even=connection-less
73 typedef enum Protocol {
74         kProto_TCP     = IPPROTO_TCP,
75         kProto_DCCP    = IPPROTO_DCCP,
76         kProto_UDP     = IPPROTO_UDP,
77         kProto_UDPLITE = IPPROTO_UDPLITE,
78 } Protocol;
79
80 static inline const char *protoName(const unsigned proto)
81 {
82         switch(proto) {
83         case kProto_TCP:     return "TCP";
84         case kProto_DCCP:    return "DCCP";
85         case kProto_UDP:     return "UDP";
86         case kProto_UDPLITE: return "UDP-Lite";
87         default:             return "(unknown)";
88         }
89 }
90
91 static inline unsigned sockType(const Protocol p)
92 {
93         switch (p) {
94         case kProto_TCP:        return SOCK_STREAM;
95         case kProto_UDPLITE:    /* fall through */
96         case kProto_UDP:        return SOCK_DGRAM;
97         case kProto_DCCP:       return SOCK_DCCP;
98         }
99 }
100
101 static inline bool is_connectionless(const Protocol p)
102 {
103         return p == kProto_UDP || p == kProto_UDPLITE;
104 }
105
106 // server/client mode
107 typedef enum ThreadMode {
108     kMode_Unknown = 0,
109     kMode_Server,
110     kMode_Client,
111     kMode_Reporter,
112     kMode_Listener
113 } ThreadMode;
114
115 // report mode
116 typedef enum ReportMode {
117     kReport_Default = 0,
118     kReport_CSV,
119     //kReport_XML,
120     kReport_MAXIMUM
121 } ReportMode;
122
123 // test mode
124 typedef enum TestMode {
125     kTest_Normal = 0,
126     kTest_DualTest,
127     kTest_TradeOff,
128     kTest_Unknown
129 } TestMode;
130
131 #include "Reporter.h"
132 /*
133  * The thread_Settings is a structure that holds all
134  * options for a given execution of either a client
135  * or server. By using this structure rather than
136  * a global structure or class we can have multiple
137  * clients or servers running with different settings.
138  * In version 2.0 and above this structure contains 
139  * all the information needed for a thread to execute
140  * and contains only C elements so it can be manipulated
141  * by either C or C++.
142  */
143 typedef struct thread_Settings {
144     // Pointers
145     char*  mFileName;               // -F
146     char*  mHost;                   // -c
147     char*  mLocalhost;              // -B
148     char*  mOutputFileName;         // -o
149     FILE*  Extractor_file;
150     ReportHeader*  reporthdr;
151     MultiHeader*   multihdr;
152     struct thread_Settings *runNow;
153     struct thread_Settings *runNext;
154     // int's
155     int mThreads;                   // -P
156     int mTOS;                       // -S
157     int mSock;                      // socket descriptor
158     int mSockAF;                    // type of @mSock
159     int Extractor_size;
160     int mBufLen;                    // -l
161     int mMSS;                       // -M
162     int mWinSize;                    // -w
163     int mCCID;                          // -Z
164     /*   flags is a BitMask of old bools
165         bool   mBufLenSet;              // -l
166         bool   mCompat;                 // -C
167         bool   mDaemon;                 // -D
168         bool   mDomain;                 // -V
169         bool   mFileInput;              // -F or -I
170         bool   mNodelay;                // -N
171         bool   mPrintMSS;               // -m
172         bool   mRemoveService;          // -R
173         bool   mStdin;                  // -I
174         bool   mStdout;                 // -o
175         bool   mSuggestWin;             // -W
176         bool   mUDP;                    // -u
177         bool   mMode_time;
178         bool   mReportSettings;
179         bool   mMulticast;
180         bool   mNoSettingsReport;       // -x s
181         bool   mNoConnectionReport;     // -x c
182         bool   mNoDataReport;           // -x d
183         bool   mNoServerReport;         // -x 
184         bool   mNoMultReport;           // -x m
185         bool   mSinlgeClient;           // -1 */
186     int flags;
187     Protocol   mProtocol;
188
189     // enums (which should be special int's)
190     ThreadMode mThreadMode;         // -s or -c
191     ReportMode mReportMode;
192     TestMode   mMode;               // -r or -d
193     // Hopefully int64_t's
194     max_size_t mDgramRate;          // -b
195     max_size_t mAmount;             // -n or -t
196     // doubles
197     double mInterval;               // -i
198     // shorts
199     unsigned short mListenPort;     // -L
200     unsigned short mPort;           // -p
201     unsigned short mMcastIface;     // -j or -J
202     // chars
203     char   mFormat;                 // -f
204     int    mTTL;                    // -T
205     int    cscov;                   // -u (partial csums)
206     char  *congAlgo;                // -A
207     char pad1[2];
208     // structs or miscellaneous
209     struct sockaddr_storage peer;   // remote part of socket
210     struct sockaddr_storage local;  // local part of socket
211     nthread_t mTID;
212 } thread_Settings;
213
214 /*
215  * Due to the use of thread_Settings in C and C++
216  * we are unable to use bool values. To provide
217  * the functionality of bools we use the following
218  * bitmask over an assumed 32 bit int. This will
219  * work fine on 64bit machines we will just be ignoring
220  * the upper 32bits.
221  *
222  * To add a flag simply define it as the next bit then
223  * add the 3 support functions below.
224  */
225 #define FLAG_BUFLENSET      0x00000001
226 #define FLAG_COMPAT         0x00000002
227 #define FLAG_DAEMON         0x00000004
228 #define FLAG_DOMAIN         0x00000008
229 #define FLAG_FILEINPUT      0x00000010
230 #define FLAG_NODELAY        0x00000020
231 #define FLAG_PRINTMSS       0x00000040
232 #define FLAG_REMOVESERVICE  0x00000080
233 #define FLAG_STDIN          0x00000100
234 #define FLAG_STDOUT         0x00000200
235 #define FLAG_SUGGESTWIN     0x00000400
236 #define FLAG_MODETIME       0x00001000
237 #define FLAG_REPORTSETTINGS 0x00002000
238 #define FLAG_MULTICAST      0x00004000
239 #define FLAG_NOSETTREPORT   0x00008000
240 #define FLAG_NOCONNREPORT   0x00010000
241 #define FLAG_NODATAREPORT   0x00020000
242 #define FLAG_NOSERVREPORT   0x00040000
243 #define FLAG_NOMULTREPORT   0x00080000
244 #define FLAG_SINGLECLIENT   0x00100000
245 #define FLAG_SINGLEUDP      0x00200000
246 #define FLAG_PACKETORIENTED 0x01000000
247
248
249 #define isBuflenSet(settings)      ((settings->flags & FLAG_BUFLENSET) != 0)
250 #define isCompat(settings)         ((settings->flags & FLAG_COMPAT) != 0)
251 #define isDaemon(settings)         ((settings->flags & FLAG_DAEMON) != 0)
252 #define isIPV6(settings)           ((settings->flags & FLAG_DOMAIN) != 0)
253 #define isFileInput(settings)      ((settings->flags & FLAG_FILEINPUT) != 0)
254 #define isNoDelay(settings)        ((settings->flags & FLAG_NODELAY) != 0)
255 #define isPrintMSS(settings)       ((settings->flags & FLAG_PRINTMSS) != 0)
256 #define isRemoveService(settings)  ((settings->flags & FLAG_REMOVESERVICE) != 0)
257 #define isSTDIN(settings)          ((settings->flags & FLAG_STDIN) != 0)
258 #define isSTDOUT(settings)         ((settings->flags & FLAG_STDOUT) != 0)
259 #define isSuggestWin(settings)     ((settings->flags & FLAG_SUGGESTWIN) != 0)
260 #define isModeTime(settings)       ((settings->flags & FLAG_MODETIME) != 0)
261 #define isReport(settings)         ((settings->flags & FLAG_REPORTSETTINGS) != 0)
262 #define isMulticast(settings)      ((settings->flags & FLAG_MULTICAST) != 0)
263 // Active Low for Reports
264 #define isSettingsReport(settings) ((settings->flags & FLAG_NOSETTREPORT) == 0)
265 #define isConnectionReport(settings)  ((settings->flags & FLAG_NOCONNREPORT) == 0)
266 #define isDataReport(settings)     ((settings->flags & FLAG_NODATAREPORT) == 0)
267 #define isServerReport(settings)   ((settings->flags & FLAG_NOSERVREPORT) == 0)
268 #define isMultipleReport(settings) ((settings->flags & FLAG_NOMULTREPORT) == 0)
269 // end Active Low
270 #define isSingleClient(settings)   ((settings->flags & FLAG_SINGLECLIENT) != 0)
271 #define isSingleUDP(settings)      ((settings->flags & FLAG_SINGLEUDP) != 0)
272 #define isConnectionLess(settings) is_connectionless((settings)->mProtocol)
273 #define isPacketOriented(settings) (((settings)->flags & FLAG_PACKETORIENTED) != 0)
274
275 #define setBuflenSet(settings)     settings->flags |= FLAG_BUFLENSET
276 #define setCompat(settings)        settings->flags |= FLAG_COMPAT
277 #define setDaemon(settings)        settings->flags |= FLAG_DAEMON
278 #define setIPV6(settings)          settings->flags |= FLAG_DOMAIN
279 #define setFileInput(settings)     settings->flags |= FLAG_FILEINPUT
280 #define setNoDelay(settings)       settings->flags |= FLAG_NODELAY
281 #define setPrintMSS(settings)      settings->flags |= FLAG_PRINTMSS
282 #define setRemoveService(settings) settings->flags |= FLAG_REMOVESERVICE
283 #define setSTDIN(settings)         settings->flags |= FLAG_STDIN
284 #define setSTDOUT(settings)        settings->flags |= FLAG_STDOUT
285 #define setSuggestWin(settings)    settings->flags |= FLAG_SUGGESTWIN
286 #define setModeTime(settings)      settings->flags |= FLAG_MODETIME
287 #define setReport(settings)        settings->flags |= FLAG_REPORTSETTINGS
288 #define setMulticast(settings)     settings->flags |= FLAG_MULTICAST
289 #define setNoSettReport(settings)  settings->flags |= FLAG_NOSETTREPORT
290 #define setNoConnReport(settings)  settings->flags |= FLAG_NOCONNREPORT
291 #define setNoDataReport(settings)  settings->flags |= FLAG_NODATAREPORT
292 #define setNoServReport(settings)  settings->flags |= FLAG_NOSERVREPORT
293 #define setNoMultReport(settings)  settings->flags |= FLAG_NOMULTREPORT
294 #define setSingleClient(settings)  settings->flags |= FLAG_SINGLECLIENT
295 #define setSingleUDP(settings)     settings->flags |= FLAG_SINGLEUDP
296 #define setPacketOriented(settings) settings->flags |= FLAG_PACKETORIENTED
297
298 #define unsetBuflenSet(settings)   settings->flags &= ~FLAG_BUFLENSET
299 #define unsetCompat(settings)      settings->flags &= ~FLAG_COMPAT
300 #define unsetDaemon(settings)      settings->flags &= ~FLAG_DAEMON
301 #define unsetIPV6(settings)        settings->flags &= ~FLAG_DOMAIN
302 #define unsetFileInput(settings)   settings->flags &= ~FLAG_FILEINPUT
303 #define unsetNoDelay(settings)     settings->flags &= ~FLAG_NODELAY
304 #define unsetPrintMSS(settings)    settings->flags &= ~FLAG_PRINTMSS
305 #define unsetRemoveService(settings)  settings->flags &= ~FLAG_REMOVESERVICE
306 #define unsetSTDIN(settings)       settings->flags &= ~FLAG_STDIN
307 #define unsetSTDOUT(settings)      settings->flags &= ~FLAG_STDOUT
308 #define unsetSuggestWin(settings)  settings->flags &= ~FLAG_SUGGESTWIN
309 #define unsetModeTime(settings)    settings->flags &= ~FLAG_MODETIME
310 #define unsetReport(settings)      settings->flags &= ~FLAG_REPORTSETTINGS
311 #define unsetMulticast(settings)   settings->flags &= ~FLAG_MULTICAST
312 #define unsetNoSettReport(settings)   settings->flags &= ~FLAG_NOSETTREPORT
313 #define unsetNoConnReport(settings)   settings->flags &= ~FLAG_NOCONNREPORT
314 #define unsetNoDataReport(settings)   settings->flags &= ~FLAG_NODATAREPORT
315 #define unsetNoServReport(settings)   settings->flags &= ~FLAG_NOSERVREPORT
316 #define unsetNoMultReport(settings)   settings->flags &= ~FLAG_NOMULTREPORT
317 #define unsetSingleClient(settings)   settings->flags &= ~FLAG_SINGLECLIENT
318 #define unsetSingleUDP(settings)      settings->flags &= ~FLAG_SINGLEUDP
319 #define unsetPacketOriented(settings) settings->flags &= ~FLAG_PACKETORIENTED
320
321
322 #define HEADER_VERSION1 0x80000000
323 #define RUN_NOW         0x00000001
324
325 /*
326  * Datagram record for record-oriented applications
327  * used to reference the 4 byte ID number we place in UDP datagrams
328  * use int32_t if possible, otherwise a 32 bit bitfield (e.g. on J90)
329  */
330 typedef struct dgram_record {
331 #ifdef HAVE_INT32_T
332     int32_t id;
333     u_int32_t tv_sec;
334     u_int32_t tv_usec;
335 #else
336     signed   int id      : 32;
337     unsigned int tv_sec  : 32;
338     unsigned int tv_usec : 32;
339 #endif
340 } dgram_record;
341
342 /*
343  * The client_hdr structure is sent from clients
344  * to servers to alert them of things that need
345  * to happen. Order must be perserved in all 
346  * future releases for backward compatibility.
347  * 1.7 has flags, numThreads, mPort, and bufferlen
348  */
349 typedef struct client_hdr {
350
351 #ifdef HAVE_INT32_T
352
353     /*
354      * flags is a bitmap for different options
355      * the most significant bits are for determining
356      * which information is available. So 1.7 uses
357      * 0x80000000 and the next time information is added
358      * the 1.7 bit will be set and 0x40000000 will be
359      * set signifying additional information. If no 
360      * information bits are set then the header is ignored.
361      * The lowest order diferentiates between dualtest and
362      * tradeoff modes, wheither the speaker needs to start 
363      * immediately or after the audience finishes.
364      */
365     int32_t flags;
366     int32_t numThreads;
367     int32_t mPort;
368     int32_t bufferlen;
369     int32_t mWinBand;
370     int32_t mAmount;
371 #else
372     signed int flags      : 32;
373     signed int numThreads : 32;
374     signed int mPort      : 32;
375     signed int bufferlen  : 32;
376     signed int mWinBand   : 32;
377     signed int mAmount    : 32;
378 #endif
379 } client_hdr;
380
381 /*
382  * The server_hdr structure facilitates the server
383  * report of jitter and loss on the client side.
384  * It piggy_backs on the existing clear to close
385  * packet.
386  */
387 typedef struct server_hdr {
388
389 #ifdef HAVE_INT32_T
390
391     /*
392      * flags is a bitmap for different options
393      * the most significant bits are for determining
394      * which information is available. So 1.7 uses
395      * 0x80000000 and the next time information is added
396      * the 1.7 bit will be set and 0x40000000 will be
397      * set signifying additional information. If no 
398      * information bits are set then the header is ignored.
399      */
400     int32_t flags;
401     int32_t total_len1;
402     int32_t total_len2;
403     int32_t stop_sec;
404     int32_t stop_usec;
405     int32_t error_cnt;
406     int32_t outorder_cnt;
407     int32_t datagrams;
408     int32_t jitter1;
409     int32_t jitter2;
410 #else
411     signed int flags        : 32;
412     signed int total_len1   : 32;
413     signed int total_len2   : 32;
414     signed int stop_sec     : 32;
415     signed int stop_usec    : 32;
416     signed int error_cnt    : 32;
417     signed int outorder_cnt : 32;
418     signed int datagrams    : 32;
419     signed int jitter1      : 32;
420     signed int jitter2      : 32;
421 #endif
422
423 } server_hdr;
424
425     // set to defaults
426     void Settings_Initialize( thread_Settings* main );
427
428     // copy structure
429     void Settings_Copy( thread_Settings* from, thread_Settings** into );
430
431     // free associated memory
432     void Settings_Destroy( thread_Settings *mSettings );
433
434     // parse settings from user's environment variables
435     void Settings_ParseEnvironment( thread_Settings *mSettings );
436
437     // parse settings from app's command line
438     void Settings_ParseCommandLine( int argc, char **argv, thread_Settings *mSettings );
439
440     // convert to lower case for [KMG]bits/sec
441     void Settings_GetLowerCaseArg(const char *,char *);
442
443     // convert to upper case for [KMG]bytes/sec
444     void Settings_GetUpperCaseArg(const char *,char *);
445
446     // generate settings for listener instance
447     void Settings_GenerateListenerSettings( thread_Settings *client, thread_Settings **listener);
448
449     // generate settings for speaker instance
450     void Settings_GenerateClientSettings( thread_Settings *server, 
451                                           thread_Settings **client,
452                                           client_hdr *hdr );
453
454     // generate client header for server
455     void Settings_GenerateClientHdr( thread_Settings *client, client_hdr *hdr );
456
457 #ifdef __cplusplus
458 } /* end extern "C" */
459 #endif
460
461 #endif // SETTINGS_H