server-rxmaxmtu-option-20060221
[openafs.git] / src / vlserver / vlserver.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 RCSID
14     ("$Header$");
15
16 #include <afs/stds.h>
17 #include <sys/types.h>
18 #include <signal.h>
19 #include <sys/stat.h>
20 #ifdef HAVE_FCNTL_H
21 #include <fcntl.h>
22 #endif
23 #ifdef AFS_NT40_ENV
24 #include <winsock2.h>
25 #include <WINNT/afsevent.h>
26 #endif
27 #ifdef HAVE_SYS_FILE_H
28 #include <sys/file.h>
29 #endif
30 #include <time.h>
31 #ifdef HAVE_NETDB_H
32 #include <netdb.h>
33 #endif
34 #ifdef HAVE_NETINET_IN_H
35 #include <netinet/in.h>
36 #endif
37 #include <stdio.h>
38
39 #ifdef HAVE_STRING_H
40 #include <string.h>
41 #else
42 #ifdef HAVE_STRINGS_H
43 #include <strings.h>
44 #endif
45 #endif
46
47 #include <rx/xdr.h>
48 #include <rx/rx.h>
49 #include <rx/rx_globals.h>
50 #include <afs/cellconfig.h>
51 #include <afs/keys.h>
52 #include <afs/auth.h>
53 #include <lock.h>
54 #include <ubik.h>
55 #include <afs/afsutil.h>
56 #include "vlserver.h"
57
58
59 #define MAXLWP 16
60 const char *vl_dbaseName;
61 struct afsconf_dir *vldb_confdir = 0;   /* vldb configuration dir */
62 int lwps = 9;
63
64 struct vldstats dynamic_statistics;
65 struct ubik_dbase *VL_dbase;
66 afs_uint32 HostAddress[MAXSERVERID + 1];
67 extern int afsconf_CheckAuth();
68 extern int afsconf_ServerAuth();
69
70 static CheckSignal();
71 int LogLevel = 0;
72 int smallMem = 0;
73 int rxJumbograms = 1;           /* default is to send and receive jumbo grams */
74 int rxMaxMTU = -1;
75
76 static void
77 CheckSignal_Signal()
78 {
79     IOMGR_SoftSig(CheckSignal, 0);
80 }
81
82 static
83 CheckSignal()
84 {
85     register int i, errorcode;
86     struct ubik_trans *trans;
87
88     if (errorcode =
89         Init_VLdbase(&trans, LOCKREAD, VLGETSTATS - VL_LOWEST_OPCODE))
90         return errorcode;
91     VLog(0, ("Dump name hash table out\n"));
92     for (i = 0; i < HASHSIZE; i++) {
93         HashNDump(trans, i);
94     }
95     VLog(0, ("Dump id hash table out\n"));
96     for (i = 0; i < HASHSIZE; i++) {
97         HashIdDump(trans, i);
98     }
99     return (ubik_EndTrans(trans));
100 }                               /*CheckSignal */
101
102
103 /* Initialize the stats for the opcodes */
104 void
105 initialize_dstats()
106 {
107     int i;
108
109     dynamic_statistics.start_time = (afs_uint32) time(0);
110     for (i = 0; i < MAX_NUMBER_OPCODES; i++) {
111         dynamic_statistics.requests[i] = 0;
112         dynamic_statistics.aborts[i] = 0;
113     }
114 }
115
116 /* check whether caller is authorized to manage RX statistics */
117 int
118 vldb_rxstat_userok(call)
119      struct rx_call *call;
120 {
121     return afsconf_SuperUser(vldb_confdir, call, NULL);
122 }
123
124 /* Main server module */
125
126 #include "AFS_component_version_number.c"
127
128 main(argc, argv)
129      int argc;
130      char **argv;
131 {
132     register afs_int32 code;
133     afs_int32 myHost;
134     struct rx_service *tservice;
135     struct rx_securityClass *sc[3];
136     extern int VL_ExecuteRequest();
137     extern int RXSTATS_ExecuteRequest();
138     struct afsconf_dir *tdir;
139     struct ktc_encryptionKey tkey;
140     struct afsconf_cell info;
141     struct hostent *th;
142     char hostname[VL_MAXNAMELEN];
143     int noAuth = 0, index, i;
144     extern int rx_extraPackets;
145     char commandLine[150];
146     char clones[MAXHOSTSPERCELL];
147
148 #ifdef  AFS_AIX32_ENV
149     /*
150      * The following signal action for AIX is necessary so that in case of a 
151      * crash (i.e. core is generated) we can include the user's data section 
152      * in the core dump. Unfortunately, by default, only a partial core is
153      * generated which, in many cases, isn't too useful.
154      */
155     struct sigaction nsa;
156
157     rx_extraPackets = 100;      /* should be a switch, I guess... */
158     sigemptyset(&nsa.sa_mask);
159     nsa.sa_handler = SIG_DFL;
160     nsa.sa_flags = SA_FULLDUMP;
161     sigaction(SIGABRT, &nsa, NULL);
162     sigaction(SIGSEGV, &nsa, NULL);
163 #endif
164     osi_audit_init();
165
166     /* Parse command line */
167     for (index = 1; index < argc; index++) {
168         if (strcmp(argv[index], "-noauth") == 0) {
169             noAuth = 1;
170
171         } else if (strcmp(argv[index], "-p") == 0) {
172             lwps = atoi(argv[++index]);
173             if (lwps > MAXLWP) {
174                 printf("Warning: '-p %d' is too big; using %d instead\n",
175                        lwps, MAXLWP);
176                 lwps = MAXLWP;
177             }
178
179         } else if (strcmp(argv[index], "-nojumbo") == 0) {
180             rxJumbograms = 0;
181
182         } else if (!strcmp(argv[i], "-rxmaxmtu")) {
183             if ((i + 1) >= argc) {
184                 fprintf(stderr, "missing argument for -rxmaxmtu\n"); 
185                 return -1; 
186             }
187             rxMaxMTU = atoi(argv[++i]);
188             if ((rxMaxMTU < RX_MIN_PACKET_SIZE) || 
189                 (rxMaxMTU > RX_MAX_PACKET_DATA_SIZE)) {
190                 printf("rxMaxMTU %d% invalid; must be between %d-%d\n",
191                        rxMaxMTU, RX_MIN_PACKET_SIZE, 
192                        RX_MAX_PACKET_DATA_SIZE);
193                 return -1;
194             }
195
196         } else if (strcmp(argv[index], "-smallmem") == 0) {
197             smallMem = 1;
198
199         } else if (strcmp(argv[index], "-trace") == 0) {
200             extern char rxi_tracename[80];
201             strcpy(rxi_tracename, argv[++index]);
202
203        } else if (strcmp(argv[index], "-auditlog") == 0) {
204            int tempfd, flags;
205            FILE *auditout;
206            char oldName[MAXPATHLEN];
207            char *fileName = argv[++index];
208
209 #ifndef AFS_NT40_ENV
210            struct stat statbuf;
211
212            if ((lstat(fileName, &statbuf) == 0) 
213                && (S_ISFIFO(statbuf.st_mode))) {
214                flags = O_WRONLY | O_NONBLOCK;
215            } else 
216 #endif
217            {
218                strcpy(oldName, fileName);
219                strcat(oldName, ".old");
220                renamefile(fileName, oldName);
221                flags = O_WRONLY | O_TRUNC | O_CREAT;
222            }
223            tempfd = open(fileName, flags, 0666);
224            if (tempfd > -1) {
225                auditout = fdopen(tempfd, "a");
226                if (auditout) {
227                    osi_audit_file(auditout);
228                } else
229                    printf("Warning: auditlog %s not writable, ignored.\n", fileName);
230            } else
231                printf("Warning: auditlog %s not writable, ignored.\n", fileName);
232         } else if (strcmp(argv[index], "-enable_peer_stats") == 0) {
233             rx_enablePeerRPCStats();
234         } else if (strcmp(argv[index], "-enable_process_stats") == 0) {
235             rx_enableProcessRPCStats();
236 #ifndef AFS_NT40_ENV
237         } else if (strcmp(argv[index], "-syslog") == 0) {
238             /* set syslog logging flag */
239             serverLogSyslog = 1;
240         } else if (strncmp(argv[index], "-syslog=", 8) == 0) {
241             serverLogSyslog = 1;
242             serverLogSyslogFacility = atoi(argv[index] + 8);
243 #endif
244         } else {
245             /* support help flag */
246 #ifndef AFS_NT40_ENV
247             printf("Usage: vlserver [-p <number of processes>] [-nojumbo] "
248                    "[-rxmaxmtu <bytes>] "
249                    "[-auditlog <log path>] "
250                    "[-syslog[=FACILITY]] "
251                    "[-enable_peer_stats] [-enable_process_stats] "
252                    "[-help]\n");
253 #else
254             printf("Usage: vlserver [-p <number of processes>] [-nojumbo] "
255                    "[-rxmaxmtu <bytes>] "
256                    "[-auditlog <log path>] "
257                    "[-enable_peer_stats] [-enable_process_stats] "
258                    "[-help]\n");
259 #endif
260             fflush(stdout);
261             exit(0);
262         }
263     }
264
265     /* Initialize dirpaths */
266     if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
267 #ifdef AFS_NT40_ENV
268         ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
269 #endif
270         fprintf(stderr, "%s: Unable to obtain AFS server directory.\n",
271                 argv[0]);
272         exit(2);
273     }
274     vl_dbaseName = AFSDIR_SERVER_VLDB_FILEPATH;
275
276 #ifndef AFS_NT40_ENV
277     serverLogSyslogTag = "vlserver";
278 #endif
279     OpenLog(AFSDIR_SERVER_VLOG_FILEPATH);       /* set up logging */
280     SetupLogSignals();
281
282     tdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
283     if (!tdir) {
284         printf
285             ("vlserver: can't open configuration files in dir %s, giving up.\n",
286              AFSDIR_SERVER_ETC_DIRPATH);
287         exit(1);
288     }
289 #ifdef AFS_NT40_ENV
290     /* initialize winsock */
291     if (afs_winsockInit() < 0) {
292         ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
293         fprintf(stderr, "vlserver: couldn't initialize winsock. \n");
294         exit(1);
295     }
296 #endif
297     /* get this host */
298     gethostname(hostname, sizeof(hostname));
299     th = gethostbyname(hostname);
300     if (!th) {
301         printf("vlserver: couldn't get address of this host (%s).\n",
302                hostname);
303         exit(1);
304     }
305     memcpy(&myHost, th->h_addr, sizeof(afs_int32));
306
307 #if !defined(AFS_HPUX_ENV) && !defined(AFS_NT40_ENV) && !defined(AFS_DJGPP_ENV)
308     signal(SIGXCPU, CheckSignal_Signal);
309 #endif
310     /* get list of servers */
311     code =
312         afsconf_GetExtendedCellInfo(tdir, NULL, AFSCONF_VLDBSERVICE, &info,
313                                     &clones);
314     if (code) {
315         printf("vlserver: Couldn't get cell server list for 'afsvldb'.\n");
316         exit(2);
317     }
318
319     vldb_confdir = tdir;        /* Preserve our configuration dir */
320     /* rxvab no longer supported */
321     memset(&tkey, 0, sizeof(tkey));
322
323     if (noAuth)
324         afsconf_SetNoAuthFlag(tdir, 1);
325
326     ubik_nBuffers = 512;
327     ubik_CRXSecurityProc = afsconf_ClientAuth;
328     ubik_CRXSecurityRock = (char *)tdir;
329     ubik_SRXSecurityProc = afsconf_ServerAuth;
330     ubik_SRXSecurityRock = (char *)tdir;
331     ubik_CheckRXSecurityProc = afsconf_CheckAuth;
332     ubik_CheckRXSecurityRock = (char *)tdir;
333     code =
334         ubik_ServerInitByInfo(myHost, htons(AFSCONF_VLDBPORT), &info, &clones,
335                               vl_dbaseName, &VL_dbase);
336     if (code) {
337         printf("vlserver: Ubik init failed with code %d\n", code);
338         exit(2);
339     }
340     if (!rxJumbograms) {
341         rx_SetNoJumbo();
342     }
343     if (rxMaxMTU != -1) {
344         rx_SetMaxMTU(rxMaxMTU);
345     }
346     rx_SetRxDeadTime(50);
347
348     memset(HostAddress, 0, sizeof(HostAddress));
349     initialize_dstats();
350
351     sc[0] = rxnull_NewServerSecurityObject();
352     sc[1] = (struct rx_securityClass *)0;
353     sc[2] = rxkad_NewServerSecurityObject(0, tdir, afsconf_GetKey, NULL);
354     tservice =
355         rx_NewService(0, USER_SERVICE_ID, "Vldb server", sc, 3,
356                       VL_ExecuteRequest);
357     if (tservice == (struct rx_service *)0) {
358         printf("vlserver: Could not create VLDB_SERVICE rx service\n");
359         exit(3);
360     }
361     rx_SetMinProcs(tservice, 2);
362     if (lwps < 4)
363         lwps = 4;
364     rx_SetMaxProcs(tservice, lwps);
365
366     tservice =
367         rx_NewService(0, RX_STATS_SERVICE_ID, "rpcstats", sc, 3,
368                       RXSTATS_ExecuteRequest);
369     if (tservice == (struct rx_service *)0) {
370         printf("vlserver: Could not create rpc stats rx service\n");
371         exit(3);
372     }
373     rx_SetMinProcs(tservice, 2);
374     rx_SetMaxProcs(tservice, 4);
375
376     for (commandLine[0] = '\0', i = 0; i < argc; i++) {
377         if (i > 0)
378             strcat(commandLine, " ");
379         strcat(commandLine, argv[i]);
380     }
381     ViceLog(0,
382             ("Starting AFS vlserver %d (%s)\n", VLDBVERSION_4, commandLine));
383     printf("%s\n", cml_version_number); /* Goes to the log */
384
385     /* allow super users to manage RX statistics */
386     rx_SetRxStatUserOk(vldb_rxstat_userok);
387
388     rx_StartServer(1);          /* Why waste this idle process?? */
389 }