2 * Copyright 2000, International Business Machines Corporation and others.
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
12 * Implementation of the client side of the AFS File Server extended
13 * statistics facility.
15 *------------------------------------------------------------------------*/
17 #include <afsconfig.h>
18 #include <afs/param.h>
22 #include "xstat_fs.h" /*Interface for this module*/
23 #include <lwp.h> /*Lightweight process package*/
33 #define LWP_STACK_SIZE (16 * 1024)
36 * Routines we need that don't have explicit include file definitions.
38 extern int RXAFSCB_ExecuteRequest(); /*AFS callback dispatcher*/
39 extern char *hostutil_GetNameByINet(); /*Host parsing utility*/
42 * Help out the linker by explicitly importing the callback routines
43 * File Servers may be lobbing at us.
45 extern afs_int32 SRXAFSCB_CallBack();
46 extern afs_int32 SRXAFSCB_InitCallBackState3();
47 extern afs_int32 SRXAFSCB_Probe();
48 extern afs_int32 SRXAFSCB_GetCE();
49 extern afs_int32 SRXAFSCB_GetLock();
54 int xstat_fs_numServers; /*Num connected servers*/
55 struct xstat_fs_ConnectionInfo
56 *xstat_fs_ConnInfo; /*Ptr to connection array*/
57 int numCollections; /*Number of data collections*/
58 struct xstat_fs_ProbeResults xstat_fs_Results; /*Latest probe results*/
59 char terminationEvent; /*One-shot termination event*/
61 afs_int32 xstat_fsData[AFS_MAX_XSTAT_LONGS]; /*Buffer for collected data*/
66 static int xstat_fs_ProbeFreqInSecs; /*Probe freq. in seconds*/
67 static int xstat_fs_initflag = 0; /*Was init routine called?*/
68 static int xstat_fs_debug = 0; /*Debugging output enabled?*/
69 static int xstat_fs_oneShot = 0; /*One-shot operation?*/
70 static int (*xstat_fs_Handler)(); /*Probe handler routine*/
71 static PROCESS probeLWP_ID; /*Probe LWP process ID*/
72 static int xstat_fs_numCollections; /*Number of desired collections*/
73 static afs_int32 *xstat_fs_collIDP; /*Ptr to collection IDs desired*/
76 * We have to pass a port to Rx to start up our callback listener
77 * service, but 7001 is already taken up by the Cache Manager. So,
80 #define XSTAT_FS_CBPORT 7101
83 /*------------------------------------------------------------------------
84 * [private] xstat_fs_CleanupInit
87 * Set up for recovery after an error in initialization (i.e.,
88 * during a call to xstat_fs_Init.
95 * Error value otherwise.
98 * This routine is private to the module.
101 * Zeros out basic data structures.
102 *------------------------------------------------------------------------*/
104 static int xstat_fs_CleanupInit()
106 { /*xstat_fs_CleanupInit*/
108 afs_int32 code; /*Return code from callback stubs*/
109 struct rx_call *rxcall; /*Bogus param*/
110 AFSCBFids *Fids_Array; /*Bogus param*/
111 AFSCBs *CallBack_Array; /*Bogus param*/
113 xstat_fs_ConnInfo = (struct xstat_fs_ConnectionInfo *)0;
114 xstat_fs_Results.probeNum = 0;
115 xstat_fs_Results.probeTime = 0;
116 xstat_fs_Results.connP = (struct xstat_fs_ConnectionInfo *)0;
117 xstat_fs_Results.collectionNumber = 0;
118 xstat_fs_Results.data.AFS_CollData_len = AFS_MAX_XSTAT_LONGS;
119 xstat_fs_Results.data.AFS_CollData_val = (afs_int32 *)xstat_fsData;
120 xstat_fs_Results.probeOK = 0;
122 rxcall = (struct rx_call *)0;
123 Fids_Array = (AFSCBFids *)0;
124 CallBack_Array = (AFSCBs *)0;
127 * Call each of the callback routines our module provides (in
128 * xstat_fs_callback.c) to make sure they're all there.
130 code = SRXAFSCB_CallBack(rxcall, Fids_Array, CallBack_Array);
133 code = SRXAFSCB_InitCallBackState3(rxcall, (afsUUID *) 0);
136 code = SRXAFSCB_Probe(rxcall);
139 } /*xstat_fs_CleanupInit*/
142 /*------------------------------------------------------------------------
143 * [exported] xstat_fs_Cleanup
146 * Clean up our memory and connection state.
149 * int a_releaseMem : Should we free up malloc'ed areas?
152 * 0 on total success,
153 * -1 if the module was never initialized, or there was a problem
154 * with the xstat_fs connection array.
157 * xstat_fs_numServers should be properly set. We don't do anything
158 * unless xstat_fs_Init() has already been called.
161 * Shuts down Rx connections gracefully, frees allocated space
163 *------------------------------------------------------------------------*/
165 int xstat_fs_Cleanup(a_releaseMem)
168 { /*xstat_fs_Cleanup*/
170 static char rn[] = "xstat_fs_Cleanup"; /*Routine name*/
171 int code; /*Return code*/
172 int conn_idx; /*Current connection index*/
173 struct xstat_fs_ConnectionInfo *curr_conn; /*Ptr to xstat_fs connection*/
176 * Assume the best, but check the worst.
178 if (!xstat_fs_initflag) {
179 fprintf(stderr, "[%s] Refused; module not initialized\n", rn);
186 * Take care of all Rx connections first. Check to see that the
187 * server count is a legal value.
189 if (xstat_fs_numServers <= 0) {
191 "[%s] Illegal number of servers (xstat_fs_numServers = %d)\n",
192 rn, xstat_fs_numServers);
196 if (xstat_fs_ConnInfo != (struct xstat_fs_ConnectionInfo *)0) {
198 * The xstat_fs connection structure array exists. Go through
199 * it and close up any Rx connections it holds.
201 curr_conn = xstat_fs_ConnInfo;
202 for (conn_idx = 0; conn_idx < xstat_fs_numServers; conn_idx++) {
203 if (curr_conn->rxconn != (struct rx_connection *)0) {
204 rx_DestroyConnection(curr_conn->rxconn);
205 curr_conn->rxconn = (struct rx_connection *)0;
208 } /*for each xstat_fs connection*/
209 } /*xstat_fs connection structure exists*/
210 } /*Legal number of servers*/
213 * If asked to, release the space we've allocated.
216 if (xstat_fs_ConnInfo != (struct xstat_fs_ConnectionInfo *)0)
217 free(xstat_fs_ConnInfo);
221 * Return the news, whatever it is.
225 } /*xstat_fs_Cleanup*/
228 /*------------------------------------------------------------------------
229 * [private] xstat_fs_LWP
232 * This LWP iterates over the server connections and gathers up
233 * the desired statistics from each one on a regular basis. When
234 * the sweep is done, the associated handler function is called
235 * to process the new data.
244 * Started by xstat_fs_Init(), uses global structures and the
245 * global private xstat_fs_oneShot variable.
248 * Nothing interesting.
249 *------------------------------------------------------------------------*/
251 static void xstat_fs_LWP()
255 static char rn[] = "xstat_fs_LWP"; /*Routine name*/
256 register afs_int32 code; /*Results of calls*/
257 int oneShotCode; /*Result of one-shot signal*/
258 struct timeval tv; /*Time structure*/
259 int conn_idx; /*Connection index*/
260 struct xstat_fs_ConnectionInfo *curr_conn; /*Current connection*/
261 afs_int32 srvVersionNumber; /*Xstat version #*/
262 afs_int32 clientVersionNumber; /*Client xstat version*/
263 afs_int32 numColls; /*Number of collections to get*/
264 afs_int32 *currCollIDP; /*Curr collection ID desired*/
266 static afs_int32 xstat_VersionNumber; /*Version # of server*/
269 * Set up some numbers we'll need.
271 clientVersionNumber = AFS_XSTAT_VERSION;
273 while (1) { /*Service loop*/
275 * Iterate through the server connections, gathering data.
276 * Don't forget to bump the probe count and zero the statistics
277 * areas before calling the servers.
280 printf("[%s] Waking up, getting data from %d server(s)\n",
281 rn, xstat_fs_numServers);
282 curr_conn = xstat_fs_ConnInfo;
283 xstat_fs_Results.probeNum++;
285 for (conn_idx = 0; conn_idx < xstat_fs_numServers; conn_idx++) {
287 * Grab the statistics for the current File Server, if the
288 * connection is valid.
291 printf("[%s] Getting collections from File Server '%s'\n",
292 rn, curr_conn->hostName);
293 if (curr_conn->rxconn != (struct rx_connection *)0) {
295 printf("[%s] Connection OK, calling RXAFS_GetXStats\n",
298 currCollIDP = xstat_fs_collIDP;
300 numColls < xstat_fs_numCollections;
301 numColls++, currCollIDP++) {
303 * Initialize the per-probe values.
306 printf("[%s] Asking for data collection %d\n",
308 xstat_fs_Results.collectionNumber = *currCollIDP;
309 xstat_fs_Results.data.AFS_CollData_len = AFS_MAX_XSTAT_LONGS;
310 memset(xstat_fs_Results.data.AFS_CollData_val, 0, AFS_MAX_XSTAT_LONGS * 4);
312 xstat_fs_Results.connP = curr_conn;
314 if (xstat_fs_debug) {
315 printf("%s: Calling RXAFS_GetXStats, conn=0x%x, clientVersionNumber=%d, collectionNumber=%d, srvVersionNumberP=0x%x, timeP=0x%x, dataP=0x%x\n",
316 rn, curr_conn->rxconn,
320 &(xstat_fs_Results.probeTime),
321 &(xstat_fs_Results.data));
322 printf("%s: [bufflen=%d, buffer at 0x%x]\n",
324 xstat_fs_Results.data.AFS_CollData_len,
325 xstat_fs_Results.data.AFS_CollData_val);
328 xstat_fs_Results.probeOK =
329 RXAFS_GetXStats(curr_conn->rxconn,
333 &(xstat_fs_Results.probeTime),
334 &(xstat_fs_Results.data));
337 * Now that we (may) have the data for this connection,
338 * call the associated handler function. The handler does
339 * not take any explicit parameters, but rather gets to the
340 * goodies via some of the objects exported by this module.
343 printf("[%s] Calling handler routine.\n", rn);
344 code = xstat_fs_Handler();
347 "[%s] Handler returned error code %d\n",
350 } /*For each collection*/
351 } /*Valid Rx connection*/
354 * Advance the xstat_fs connection pointer.
358 } /*For each xstat_fs connection*/
361 * All (valid) connections have been probed. Fall asleep for the
362 * prescribed number of seconds, unless we're a one-shot. In
363 * that case, we need to signal our caller that we're done.
366 printf("[%s] Polling complete for probe round %d.\n",
367 rn, xstat_fs_Results.probeNum);
369 if (xstat_fs_oneShot) {
371 * One-shot execution desired. Signal our main procedure
372 * that we've finished our collection round.
375 printf("[%s] Signalling main process at 0x%x\n",
376 rn, &terminationEvent);
377 oneShotCode = LWP_SignalProcess(&terminationEvent);
380 "[%s] Error %d from LWP_SignalProcess()",
382 break; /*from the perpetual while loop*/
383 } /*One-shot execution*/
386 * Continuous execution desired. Sleep for the required
389 tv.tv_sec = xstat_fs_ProbeFreqInSecs;
392 printf("[%s] Falling asleep for %d seconds\n",
393 rn, xstat_fs_ProbeFreqInSecs);
394 code = IOMGR_Select(0, /*Num fids*/
395 0, /*Descs ready for reading*/
396 0, /*Descs ready for writing*/
397 0, /*Descs w/exceptional conditions*/
398 &tv); /*Ptr to timeout structure*/
401 "[%s] IOMGR_Select returned code %d\n",
403 } /*Continuous execution*/
409 /*------------------------------------------------------------------------
410 * [exported] xstat_fs_Init
413 * Initialize the xstat_fs module: set up Rx connections to the
414 * given set of File Servers, start up the probe and callback LWPs,
415 * and associate the routine to be called when a probe completes.
416 * Also, let it know which collections you're interested in.
419 * int a_numServers : Num. servers to connect to.
420 * struct sockaddr_in *a_socketArray : Array of server sockets.
421 * int a_ProbeFreqInSecs : Probe frequency in seconds.
422 * int (*a_ProbeHandler)() : Ptr to probe handler fcn.
423 * int a_flags : Various flags.
424 * int a_numCollections : Number of collections desired.
425 * afs_int32 *a_collIDP : Ptr to collection IDs.
429 * -2 for (at least one) connection error,
430 * LWP process creation code, if it failed,
431 * -1 for other fatal errors.
434 * *** MUST BE THE FIRST ROUTINE CALLED FROM THIS PACKAGE ***
435 * Also, the server security object CBsecobj MUST be a static,
436 * since it has to stick around after this routine exits.
439 * Sets up just about everything.
440 *------------------------------------------------------------------------*/
442 int xstat_fs_Init(a_numServers, a_socketArray, a_ProbeFreqInSecs, a_ProbeHandler,
443 a_flags, a_numCollections, a_collIDP)
445 struct sockaddr_in *a_socketArray;
446 int a_ProbeFreqInSecs;
447 int (*a_ProbeHandler)();
449 int a_numCollections;
450 afs_int32 *a_collIDP;
454 static char rn[] = "xstat_fs_Init"; /*Routine name*/
455 register afs_int32 code; /*Return value*/
456 static struct rx_securityClass *CBsecobj; /*Callback security object*/
457 struct rx_securityClass *secobj; /*Client security object*/
458 struct rx_service *rxsrv_afsserver; /*Server for AFS*/
459 int arg_errfound; /*Argument error found?*/
460 int curr_srv; /*Current server idx*/
461 struct xstat_fs_ConnectionInfo *curr_conn; /*Ptr to current conn*/
462 char *hostNameFound; /*Ptr to returned host name*/
463 int conn_err; /*Connection error?*/
464 int PortToUse; /*Callback port to use*/
465 int collIDBytes; /*Num bytes in coll ID array*/
468 * If we've already been called, snicker at the bozo, gently
469 * remind him of his doubtful heritage, and return success.
471 if (xstat_fs_initflag) {
472 fprintf(stderr, "[%s] Called multiple times!\n", rn);
476 xstat_fs_initflag = 1;
479 * Check the parameters for bogosities.
482 if (a_numServers <= 0) {
483 fprintf(stderr, "[%s] Illegal number of servers: %d\n",
487 if (a_socketArray == (struct sockaddr_in *)0) {
488 fprintf(stderr, "[%s] Null server socket array argument\n", rn);
491 if (a_ProbeFreqInSecs <= 0) {
492 fprintf(stderr, "[%s] Illegal probe frequency: %d\n",
493 rn, a_ProbeFreqInSecs);
496 if (a_ProbeHandler == (int (*)())0) {
497 fprintf(stderr, "[%s] Null probe handler function argument\n",
501 if (a_numCollections <= 0) {
502 fprintf(stderr, "[%s] Illegal collection count argument: %d\n",
506 if (a_collIDP == NULL) {
507 fprintf(stderr, "[%s] Null collection ID array argument\n", rn);
514 * Record our passed-in info.
516 xstat_fs_debug = (a_flags & XSTAT_FS_INITFLAG_DEBUGGING);
517 xstat_fs_oneShot = (a_flags & XSTAT_FS_INITFLAG_ONE_SHOT);
518 xstat_fs_numServers = a_numServers;
519 xstat_fs_Handler = a_ProbeHandler;
520 xstat_fs_ProbeFreqInSecs = a_ProbeFreqInSecs;
521 xstat_fs_numCollections = a_numCollections;
522 collIDBytes = xstat_fs_numCollections * sizeof(afs_int32);
523 xstat_fs_collIDP = (afs_int32 *)(malloc(collIDBytes));
524 memcpy(xstat_fs_collIDP, a_collIDP, collIDBytes);
525 if (xstat_fs_debug) {
526 printf("[%s] Asking for %d collection(s): ", rn, xstat_fs_numCollections);
527 for (curr_srv = 0; curr_srv < xstat_fs_numCollections; curr_srv++)
528 printf("%d ", *(xstat_fs_collIDP+curr_srv));
533 * Get ready in case we have to do a cleanup - basically, zero
536 code = xstat_fs_CleanupInit();
541 * Allocate the necessary data structures and initialize everything
545 (struct xstat_fs_ConnectionInfo *)
546 malloc(a_numServers * sizeof(struct xstat_fs_ConnectionInfo));
547 if (xstat_fs_ConnInfo == (struct xstat_fs_ConnectionInfo *)0) {
549 "[%s] Can't allocate %d connection info structs (%d bytes)\n",
551 (a_numServers * sizeof(struct xstat_fs_ConnectionInfo)));
552 return(-1); /*No cleanup needs to be done yet*/
556 * Initialize the Rx subsystem, just in case nobody's done it.
559 printf("[%s] Initializing Rx\n", rn);
560 PortToUse = XSTAT_FS_CBPORT;
563 code = rx_Init(htons(PortToUse));
565 if (code == RX_ADDRINUSE) {
568 "[%s] Callback port %d in use, advancing\n",
573 fprintf(stderr, "[%s] Fatal error in rx_Init()\n", rn);
579 printf("[%s] Rx initialized on port %d\n", rn, PortToUse);
582 * Create a null Rx server security object, to be used by the
585 CBsecobj = (struct rx_securityClass *)
586 rxnull_NewServerSecurityObject();
587 if (CBsecobj == (struct rx_securityClass *)0) {
589 "[%s] Can't create callback listener's security object.\n",
591 xstat_fs_Cleanup(1); /*Delete already-malloc'ed areas*/
595 printf("[%s] Callback server security object created\n", rn);
598 * Create a null Rx client security object, to be used by the
601 secobj = rxnull_NewClientSecurityObject();
602 if (secobj == (struct rx_securityClass *)0) {
604 "[%s] Can't create probe LWP client security object.\n",
606 xstat_fs_Cleanup(1); /*Delete already-malloc'ed areas*/
610 printf("[%s] Probe LWP client security object created\n", rn);
612 curr_conn = xstat_fs_ConnInfo;
614 for (curr_srv = 0; curr_srv < a_numServers; curr_srv++) {
616 * Copy in the socket info for the current server, resolve its
617 * printable name if possible.
619 if (xstat_fs_debug) {
620 printf("[%s] Copying in the following socket info:\n",
622 printf("[%s] IP addr 0x%lx, port %d\n", rn,
623 (a_socketArray + curr_srv)->sin_addr.s_addr,
624 (a_socketArray + curr_srv)->sin_port);
626 memcpy(&(curr_conn->skt), a_socketArray + curr_srv, sizeof(struct sockaddr_in));
629 hostutil_GetNameByINet(curr_conn->skt.sin_addr.s_addr);
630 if (hostNameFound == NULL) {
632 "[%s] Can't map Internet address %lu to a string name\n",
633 rn, curr_conn->skt.sin_addr.s_addr);
634 curr_conn->hostName[0] = '\0';
637 strcpy(curr_conn->hostName, hostNameFound);
639 printf("[%s] Host name for server index %d is %s\n",
640 rn, curr_srv, curr_conn->hostName);
644 * Make an Rx connection to the current server.
647 printf("[%s] Connecting to srv idx %d, IP addr 0x%lx, port %d, service 1\n",
648 rn, curr_srv, curr_conn->skt.sin_addr.s_addr,
649 curr_conn->skt.sin_port);
652 rx_NewConnection(curr_conn->skt.sin_addr.s_addr, /*Server addr*/
653 curr_conn->skt.sin_port, /*Server port*/
655 secobj, /*Security obj*/
657 if (curr_conn->rxconn == (struct rx_connection *)0) {
659 "[%s] Can't create Rx connection to server '%s' (%lu)\n",
660 rn, curr_conn->hostName, curr_conn->skt.sin_addr.s_addr);
664 printf("[%s] New connection at 0x%lx\n",
665 rn, curr_conn->rxconn);
668 * Bump the current xstat_fs connection to set up.
675 * Create the AFS callback service (listener).
678 printf("[%s] Creating AFS callback listener\n", rn);
680 rx_NewService(0, /*Use default port*/
682 "afs", /*Service name*/
683 &CBsecobj, /*Ptr to security object(s)*/
684 1, /*# of security objects*/
685 RXAFSCB_ExecuteRequest); /*Dispatcher*/
686 if (rxsrv_afsserver == (struct rx_service *)0) {
688 "[%s] Can't create callback Rx service/listener\n",
690 xstat_fs_Cleanup(1); /*Delete already-malloc'ed areas*/
694 printf("[%s] Callback listener created\n", rn);
697 * Start up the AFS callback service.
700 printf("[%s] Starting up callback listener.\n", rn);
701 rx_StartServer(0); /*Don't donate yourself to LWP pool*/
704 * Start up the probe LWP.
707 printf("[%s] Creating the probe LWP\n", rn);
709 LWP_CreateProcess(xstat_fs_LWP, /*Function to start up*/
710 LWP_STACK_SIZE, /*Stack size in bytes*/
712 (void *) 0, /*Parameters*/
713 "xstat_fs Worker", /*Name to use*/
714 &probeLWP_ID); /*Returned LWP process ID*/
717 "[%s] Can't create xstat_fs LWP! Error is %d\n",
719 xstat_fs_Cleanup(1); /*Delete already-malloc'ed areas*/
723 printf("[%s] Probe LWP process structure located at 0x%x\n",
727 * Return the final results.
737 /*------------------------------------------------------------------------
738 * [exported] xstat_fs_ForceProbeNow
741 * Wake up the probe LWP, forcing it to execute a probe immediately.
748 * Error value otherwise.
751 * The module must have been initialized.
755 *------------------------------------------------------------------------*/
757 int xstat_fs_ForceProbeNow()
759 { /*xstat_fs_ForceProbeNow*/
761 static char rn[] = "xstat_fs_ForceProbeNow"; /*Routine name*/
764 * There isn't a prayer unless we've been initialized.
766 if (!xstat_fs_initflag) {
767 fprintf(stderr, "[%s] Must call xstat_fs_Init first!\n", rn);
772 * Kick the sucker in the side.
774 IOMGR_Cancel(probeLWP_ID);
777 * We did it, so report the happy news.
781 } /*xstat_fs_ForceProbeNow*/