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
10 #include <afsconfig.h>
11 #include <afs/param.h>
23 #include <netinet/in.h>
36 #include <afs/assert.h>
39 #include <afs/afsint.h>
40 #include <afs/rxgen_consts.h>
42 #include <afs/errors.h>
43 #include <afs/ihandle.h>
44 #include <afs/vnode.h>
45 #include <afs/volume.h>
46 #ifdef AFS_ATHENA_STDENV
50 #include <afs/ptclient.h>
51 #include <afs/prs_fs.h>
53 #include <afs/afsutil.h>
55 #include <afs/cellconfig.h>
61 #ifdef AFS_PTHREAD_ENV
62 pthread_mutex_t host_glock_mutex;
63 #endif /* AFS_PTHREAD_ENV */
66 extern int CurrentConnections;
68 extern int AnonymousID;
69 extern prlist AnonCPS;
71 extern struct afsconf_dir *confDir; /* config dir object */
72 extern int lwps; /* the max number of server threads */
73 extern afsUUID FS_HostUUID;
75 int CEs = 0; /* active clients */
76 int CEBlocks = 0; /* number of blocks of CEs */
77 struct client *CEFree = 0; /* first free client */
78 struct host *hostList = 0; /* linked list of all hosts */
79 int hostCount = 0; /* number of hosts in hostList */
83 #define CESPERBLOCK 73
84 struct CEBlock /* block of CESPERBLOCK file entries */
86 struct client entry[CESPERBLOCK];
90 * Make sure the subnet macros have been defined.
93 #define IN_SUBNETA(i) ((((afs_int32)(i))&0x80800000)==0x00800000)
96 #ifndef IN_CLASSA_SUBNET
97 #define IN_CLASSA_SUBNET 0xffff0000
101 #define IN_SUBNETB(i) ((((afs_int32)(i))&0xc0008000)==0x80008000)
104 #ifndef IN_CLASSB_SUBNET
105 #define IN_CLASSB_SUBNET 0xffffff00
108 #define rxr_GetEpoch(aconn) (((struct rx_connection *)(aconn))->epoch)
110 #define rxr_CidOf(aconn) (((struct rx_connection *)(aconn))->cid)
112 #define rxr_PortOf(aconn) \
113 rx_PortOf(rx_PeerOf(((struct rx_connection *)(aconn))))
115 #define rxr_HostOf(aconn) \
116 rx_HostOf(rx_PeerOf((struct rx_connection *)(aconn)))
119 /* get a new block of CEs and chain it on CEFree */
120 static void GetCEBlock()
122 register struct CEBlock *block;
125 block = (struct CEBlock *)malloc(sizeof(struct CEBlock));
127 ViceLog(0, ("Failed malloc in GetCEBlock\n"));
128 ShutDownAndCore(PANIC);
131 for(i = 0; i < (CESPERBLOCK -1); i++) {
132 Lock_Init(&block->entry[i].lock);
133 block->entry[i].next = &(block->entry[i+1]);
135 block->entry[CESPERBLOCK-1].next = 0;
136 Lock_Init(&block->entry[CESPERBLOCK-1].lock);
137 CEFree = (struct client *)block;
143 /* get the next available CE */
144 static struct client *GetCE()
147 register struct client *entry;
152 ViceLog(0, ("CEFree NULL in GetCE\n"));
153 ShutDownAndCore(PANIC);
157 CEFree = entry->next;
159 memset((char *)entry, 0, CLIENT_TO_ZERO(entry));
165 /* return an entry to the free list */
166 static void FreeCE(entry)
167 register struct client *entry;
170 entry->next = CEFree;
177 * The HTs and HTBlocks variables were formerly static, but they are
178 * now referenced elsewhere in the FileServer.
180 int HTs = 0; /* active file entries */
181 int HTBlocks = 0; /* number of blocks of HTs */
182 static struct host *HTFree = 0; /* first free file entry */
185 * Hash tables of host pointers. We need two tables, one
186 * to map IP addresses onto host pointers, and another
187 * to map host UUIDs onto host pointers.
189 static struct h_hashChain *hostHashTable[h_HASHENTRIES];
190 static struct h_hashChain *hostUuidHashTable[h_HASHENTRIES];
191 #define h_HashIndex(hostip) ((hostip) & (h_HASHENTRIES-1))
192 #define h_UuidHashIndex(uuidp) (((int)(afs_uuid_hash(uuidp))) & (h_HASHENTRIES-1))
194 struct HTBlock /* block of HTSPERBLOCK file entries */
196 struct host entry[h_HTSPERBLOCK];
200 /* get a new block of HTs and chain it on HTFree */
201 static void GetHTBlock()
204 register struct HTBlock *block;
206 static int index = 0;
208 block = (struct HTBlock *)malloc(sizeof(struct HTBlock));
210 ViceLog(0, ("Failed malloc in GetHTBlock\n"));
211 ShutDownAndCore(PANIC);
214 #ifdef AFS_PTHREAD_ENV
215 for(i=0; i < (h_HTSPERBLOCK); i++)
216 assert(pthread_cond_init(&block->entry[i].cond, NULL) == 0);
217 #endif /* AFS_PTHREAD_ENV */
218 for(i=0; i < (h_HTSPERBLOCK); i++)
219 Lock_Init(&block->entry[i].lock);
220 for(i=0; i < (h_HTSPERBLOCK -1); i++)
221 block->entry[i].next = &(block->entry[i+1]);
222 for (i=0; i< (h_HTSPERBLOCK); i++)
223 block->entry[i].index = index++;
224 block->entry[h_HTSPERBLOCK-1].next = 0;
225 HTFree = (struct host *)block;
226 hosttableptrs[HTBlocks++] = block->entry;
231 /* get the next available HT */
232 static struct host *GetHT()
235 register struct host *entry;
241 HTFree = entry->next;
243 memset((char *)entry, 0, HOST_TO_ZERO(entry));
249 /* return an entry to the free list */
250 static void FreeHT(entry)
251 register struct host *entry;
254 entry->next = HTFree;
261 static short consolePort = 0;
264 register struct host *host;
273 * If this thread does not have a hold on this host AND
274 * if other threads also dont have any holds on this host AND
275 * If either the HOSTDELETED or CLIENTDELETED flags are set
278 int h_Release_r(host)
279 register struct host *host;
282 if (!((host)->holds[h_holdSlot()] & ~h_holdbit()) ) {
283 if (! h_OtherHolds_r(host) ) {
284 /* must avoid masking this until after h_OtherHolds_r runs
285 but it should be run before h_TossStuff_r */
286 (host)->holds[h_holdSlot()] &= ~h_holdbit();
287 if ( (host->hostFlags & HOSTDELETED) ||
288 (host->hostFlags & CLIENTDELETED) ) {
292 (host)->holds[h_holdSlot()] &= ~h_holdbit();
294 (host)->holds[h_holdSlot()] &= ~h_holdbit();
300 register struct host *host;
304 retVal = h_Held_r(host);
309 int h_OtherHolds_r(host)
310 register struct host *host;
312 register int i, bit, slot;
315 for (i = 0 ; i < h_maxSlots ; i++) {
316 if (host->holds[i] != ((i == slot) ? bit : 0)) {
323 int h_OtherHolds(host)
324 register struct host *host;
328 retVal = h_OtherHolds_r(host);
334 register struct host *host;
344 * returns 1 if already locked
345 * else returns locks and returns 0
349 register struct host *host;
351 struct Lock *hostLock = &host->lock;
356 if ( !(hostLock->excl_locked) && !(hostLock->readers_reading) )
357 hostLock->excl_locked = WRITE_LOCK;
361 LOCK_UNLOCK(hostLock)
370 #if FS_STATS_DETAILED
371 /*------------------------------------------------------------------------
372 * PRIVATE h_AddrInSameNetwork
375 * Given a target IP address and a candidate IP address (both
376 * in host byte order), return a non-zero value (1) if the
377 * candidate address is in a different network from the target
381 * a_targetAddr : Target address.
382 * a_candAddr : Candidate address.
385 * 1 if the candidate address is in the same net as the target,
389 * The target and candidate addresses are both in host byte
390 * order, NOT network byte order, when passed in. We return
391 * our value as a character, since that's the type of field in
392 * the host structure, where this info will be stored.
396 *------------------------------------------------------------------------*/
398 static char h_AddrInSameNetwork(a_targetAddr, a_candAddr)
399 afs_uint32 a_targetAddr;
400 afs_uint32 a_candAddr;
402 { /*h_AddrInSameNetwork*/
404 afs_uint32 targetNet;
408 * Pull out the network and subnetwork numbers from the target
409 * and candidate addresses. We can short-circuit this whole
410 * affair if the target and candidate addresses are not of the
413 if (IN_CLASSA(a_targetAddr)) {
414 if (!(IN_CLASSA(a_candAddr))) {
417 targetNet = a_targetAddr & IN_CLASSA_NET;
418 candNet = a_candAddr & IN_CLASSA_NET;
421 if (IN_CLASSB(a_targetAddr)) {
422 if (!(IN_CLASSB(a_candAddr))) {
425 targetNet = a_targetAddr & IN_CLASSB_NET;
426 candNet = a_candAddr & IN_CLASSB_NET;
429 if (IN_CLASSC(a_targetAddr)) {
430 if (!(IN_CLASSC(a_candAddr))) {
433 targetNet = a_targetAddr & IN_CLASSC_NET;
434 candNet = a_candAddr & IN_CLASSC_NET;
437 targetNet = a_targetAddr;
438 candNet = a_candAddr;
439 } /*Class D address*/
442 * Now, simply compare the extracted net values for the two addresses
443 * (which at this point are known to be of the same class)
445 if (targetNet == candNet)
450 } /*h_AddrInSameNetwork*/
451 #endif /* FS_STATS_DETAILED */
455 h_gethostcps_r(host,now)
456 register struct host *host;
457 register afs_int32 now;
462 /* at this point, the host might not be locked, nor held */
463 /* make sure that we do not disappear behind the RPC */
464 if ( !(held = h_Held_r(host)) )
467 /* wait if somebody else is already doing the getCPS call */
468 while ( host->hostFlags & HPCS_INPROGRESS )
470 slept = 1; /* I did sleep */
471 host->hostFlags |= HPCS_WAITING; /* I am sleeping now */
472 #ifdef AFS_PTHREAD_ENV
473 pthread_cond_wait(&host->cond, &host_glock_mutex);
474 #else /* AFS_PTHREAD_ENV */
475 if (( code = LWP_WaitProcess( &(host->hostFlags ))) != LWP_SUCCESS)
476 ViceLog(0, ("LWP_WaitProcess returned %d\n", code));
477 #endif /* AFS_PTHREAD_ENV */
481 host->hostFlags |= HPCS_INPROGRESS; /* mark as CPSCall in progress */
482 if (host->hcps.prlist_val)
483 free(host->hcps.prlist_val); /* this is for hostaclRefresh */
484 host->hcps.prlist_val = NULL;
485 host->hcps.prlist_len = 0;
486 slept? (host->cpsCall = FT_ApproxTime()): (host->cpsCall = now );
489 code = pr_GetHostCPS(htonl(host->host), &host->hcps);
493 * Although ubik_Call (called by pr_GetHostCPS) traverses thru all protection servers
494 * and reevaluates things if no sync server or quorum is found we could still end up
495 * with one of these errors. In such case we would like to reevaluate the rpc call to
496 * find if there's cps for this guy. We treat other errors (except network failures
497 * ones - i.e. code < 0) as an indication that there is no CPS for this host. Ideally
498 * we could like to deal this problem the other way around (i.e. if code == NOCPS
499 * ignore else retry next time) but the problem is that there're other errors (i.e.
500 * EPERM) for which we don't want to retry and we don't know the whole code list!
502 if (code < 0 || code == UNOQUORUM || code == UNOTSYNC) {
504 * We would have preferred to use a while loop and try again since ops in protected
505 * acls for this host will fail now but they'll be reevaluated on any subsequent
506 * call. The attempt to wait for a quorum/sync site or network error won't work
507 * since this problems really should only occurs during a complete fileserver
508 * restart. Since the fileserver will start before the ptservers (and thus before
509 * quorums are complete) clients will be utilizing all the fileserver's lwps!!
511 host->hcpsfailed = 1;
512 ViceLog(0, ("Warning: GetHostCPS failed (%d) for %x; will retry\n", code, host->host));
514 host->hcpsfailed = 0;
515 ViceLog(1, ("gethost: GetHostCPS failed (%d) for %x; ignored\n", code, host->host));
517 if (host->hcps.prlist_val)
518 free(host->hcps.prlist_val);
519 host->hcps.prlist_val = NULL;
520 host->hcps.prlist_len = 0; /* Make sure it's zero */
522 host->hcpsfailed = 0;
524 host->hostFlags &= ~HPCS_INPROGRESS;
525 /* signal all who are waiting */
526 if ( host->hostFlags & HPCS_WAITING) /* somebody is waiting */
528 host->hostFlags &= ~HPCS_WAITING;
529 #ifdef AFS_PTHREAD_ENV
530 assert(pthread_cond_broadcast(&host->cond) == 0);
531 #else /* AFS_PTHREAD_ENV */
532 if ( (code = LWP_NoYieldSignal( &(host->hostFlags) )) != LWP_SUCCESS )
533 ViceLog(0, ("LWP_NoYieldSignal returns %d\n", code));
534 #endif /* AFS_PTHREAD_ENV */
537 /* if we had held the host, release it now */
542 void h_flushhostcps(hostaddr, hport)
543 register afs_uint32 hostaddr, hport; /* net byte order */
545 register struct host *host;
549 host = h_Lookup_r(hostaddr, hport, &held);
551 host->hcpsfailed = 1;
562 * Allocate a host. It will be identified by the peer (ip,port) info in the
563 * rx connection provided. The host is returned un-held and un-locked
565 #define DEF_ROPCONS 2115
567 struct host *h_Alloc(r_con)
568 register struct rx_connection *r_con;
572 retVal = h_Alloc_r(r_con);
577 struct host *h_Alloc_r(r_con)
578 register struct rx_connection *r_con;
582 struct servent *serverentry;
583 register index = h_HashIndex(rxr_HostOf(r_con));
584 register struct host *host;
585 static struct rx_securityClass *sc = 0;
587 struct h_hashChain* h_hashChain;
588 #if FS_STATS_DETAILED
589 afs_uint32 newHostAddr_HBO; /*New host IP addr, in host byte order*/
590 #endif /* FS_STATS_DETAILED */
594 h_hashChain = (struct h_hashChain*) malloc(sizeof(struct h_hashChain));
596 h_hashChain->hostPtr = host;
597 h_hashChain->addr = rxr_HostOf(r_con);
598 h_hashChain->next = hostHashTable[index];
599 hostHashTable[index] = h_hashChain;
601 host->host = rxr_HostOf(r_con);
602 host->port = rxr_PortOf(r_con);
603 if(consolePort == 0 ) { /* find the portal number for console */
604 #if defined(AFS_OSF_ENV)
605 serverentry = getservbyname("ropcons", "");
607 serverentry = getservbyname("ropcons", 0);
610 consolePort = serverentry->s_port;
612 consolePort = DEF_ROPCONS; /* Use a default */
614 if (host->port == consolePort) host->Console = 1;
615 /* Make a callback channel even for the console, on the off chance that it
616 makes a request that causes a break call back. It shouldn't. */
619 sc = rxnull_NewClientSecurityObject();
620 host->callback_rxcon = rx_NewConnection (host->host, host->port,
622 rx_SetConnDeadTime(host->callback_rxcon, 50);
623 rx_SetConnHardDeadTime(host->callback_rxcon, AFS_HARDDEADTIME);
625 now = host->LastCall = host->cpsCall = host->ActiveCall = FT_ApproxTime();
627 host->hcps.prlist_val = NULL;
628 host->hcps.prlist_len = 0;
629 host->hcps.prlist_val = NULL;
632 host->hcpsfailed = 0; /* save cycles */
633 h_gethostcps(host); /* do this under host lock */
635 host->FirstClient = 0;
636 h_InsertList_r(host); /* update global host List */
637 #if FS_STATS_DETAILED
639 * Compare the new host's IP address (in host byte order) with ours
640 * (the File Server's), remembering if they are in the same network.
642 newHostAddr_HBO = (afs_uint32)ntohl(host->host);
643 host->InSameNetwork = h_AddrInSameNetwork(FS_HostAddr_HBO,
645 #endif /* FS_STATS_DETAILED */
651 /* Lookup a host given an IP address and UDP port number. */
652 /* hostaddr and hport are in network order */
653 struct host *h_Lookup(afs_uint32 hostaddr, afs_uint32 hport, int *heldp)
657 retVal = h_Lookup_r(hostaddr, hport, heldp);
662 /* Note: host should be released by caller if 0 == *heldp and non-null */
663 /* hostaddr and hport are in network order */
664 struct host *h_Lookup_r(afs_uint32 hostaddr, afs_uint32 hport, int *heldp)
666 register afs_int32 now;
667 register struct host *host=0;
668 register struct h_hashChain* chain;
669 register index = h_HashIndex(hostaddr);
670 extern int hostaclRefresh;
673 for (chain=hostHashTable[index]; chain; chain=chain->next) {
674 host = chain->hostPtr;
676 if (!(host->hostFlags & HOSTDELETED) && chain->addr == hostaddr
677 && host->port == hport) {
678 *heldp = h_Held_r(host);
682 if (host->hostFlags & HOSTDELETED) {
689 now = FT_ApproxTime(); /* always evaluate "now" */
690 if (host->hcpsfailed || (host->cpsCall+hostaclRefresh < now )) {
692 * Every hostaclRefresh period (def 2 hrs) get the new
693 * membership list for the host. Note this could be the
694 * first time that the host is added to a group. Also
695 * here we also retry on previous legitimate hcps failures.
697 h_gethostcps_r(host,now);
707 /* Lookup a host given its UUID. */
708 struct host *h_LookupUuid_r(afsUUID *uuidp)
710 register struct host *host=0;
711 register struct h_hashChain* chain;
712 register index = h_UuidHashIndex(uuidp);
714 for (chain=hostUuidHashTable[index]; chain; chain=chain->next) {
715 host = chain->hostPtr;
717 if (!(host->hostFlags & HOSTDELETED) && host->interface
718 && afs_uuid_equal(&host->interface->uuid, uuidp)) {
729 * h_Hold: Establish a hold by the current LWP on this host--the host
730 * or its clients will not be physically deleted until all holds have
733 * NOTE: h_Hold_r is a macro defined in host.h.
736 int h_Hold(register struct host *host)
745 /* h_TossStuff: Toss anything in the host structure (the host or
746 * clients marked for deletion. Called from r_Release ONLY.
747 * To be called, there must be no holds, and either host->deleted
748 * or host->clientDeleted must be set.
750 int h_TossStuff_r(register struct host *host)
752 register struct client **cp, *client;
755 /* if somebody still has this host held */
756 for (i=0; (i<h_maxSlots)&&(!(host)->holds[i]); i++);
760 /* ASSUMPTION: r_FreeConnection() does not yield */
761 for (cp = &host->FirstClient; (client = *cp); ) {
762 if ((host->hostFlags & HOSTDELETED) || client->deleted) {
763 if ((client->ViceId != ANONYMOUSID) && client->CPS.prlist_val) {
764 free(client->CPS.prlist_val);
765 client->CPS.prlist_val = NULL;
768 rx_SetSpecific(client->tcon, rxcon_client_key, (void *)0);
770 CurrentConnections--;
773 } else cp = &client->next;
776 /* We've just cleaned out all the deleted clients; clear the flag */
777 host->hostFlags &= ~CLIENTDELETED;
779 if (host->hostFlags & HOSTDELETED) {
780 register struct h_hashChain **hp, *th;
781 register struct rx_connection *rxconn;
786 if (host->Console & 1) Console--;
787 if ((rxconn = host->callback_rxcon)) {
788 host->callback_rxcon = (struct rx_connection *)0;
790 * If rx_DestroyConnection calls h_FreeConnection we will
791 * deadlock on the host_glock_mutex. Work around the problem
792 * by unhooking the client from the connection before
793 * destroying the connection.
795 client = rx_GetSpecific(rxconn, rxcon_client_key);
796 if (client && client->tcon == rxconn)
798 rx_SetSpecific(rxconn, rxcon_client_key, (void *)0);
799 rx_DestroyConnection(rxconn);
801 if (host->hcps.prlist_val)
802 free(host->hcps.prlist_val);
803 host->hcps.prlist_val = NULL;
804 host->hcps.prlist_len = 0;
805 DeleteAllCallBacks_r(host);
806 host->hostFlags &= ~RESETDONE; /* just to be safe */
808 /* if alternate addresses do not exist */
809 if ( !(host->interface) )
811 for (hp = &hostHashTable[h_HashIndex(host->host)];
812 (th = *hp); hp = &th->next)
815 if (th->hostPtr == host)
818 h_DeleteList_r(host);
826 /* delete all hash entries for the UUID */
827 uuidp = &host->interface->uuid;
828 for (hp = &hostUuidHashTable[h_UuidHashIndex(uuidp)];
829 (th = *hp); hp = &th->next) {
831 if (th->hostPtr == host)
838 /* delete all hash entries for alternate addresses */
839 assert(host->interface->numberOfInterfaces > 0 );
840 for ( i=0; i < host->interface->numberOfInterfaces; i++)
842 hostAddr = host->interface->addr[i];
843 for (hp = &hostHashTable[h_HashIndex(hostAddr)];
844 (th = *hp); hp = &th->next)
847 if (th->hostPtr == host)
855 free(host->interface);
856 host->interface = NULL;
857 h_DeleteList_r(host); /* remove host from global host List */
859 } /* if alternate address exists */
864 /* Called by rx when a server connection disappears */
865 int h_FreeConnection(struct rx_connection *tcon)
867 register struct client *client;
869 client = (struct client *) rx_GetSpecific(tcon, rxcon_client_key);
872 if (client->tcon == tcon)
873 client->tcon = (struct rx_connection *)0;
876 } /*h_FreeConnection*/
879 /* h_Enumerate: Calls (*proc)(host, held, param) for at least each host in the
880 * system at the start of the enumeration (perhaps more). Hosts may be deleted
881 * (have delete flag set); ditto for clients. (*proc) is always called with
882 * host h_held(). The hold state of the host with respect to this lwp is passed
883 * to (*proc) as the param held. The proc should return 0 if the host should be
884 * released, 1 if it should be held after enumeration.
886 h_Enumerate(proc, param)
891 register struct host *host, **list;
893 register int i, count;
896 if (hostCount == 0) {
900 list = (struct host **)malloc(hostCount * sizeof(struct host *));
901 assert(list != NULL);
902 held = (int *)malloc(hostCount * sizeof(int));
903 assert(held != NULL);
904 for (count = 0, host = hostList ; host ; host = host->next, count++) {
906 if (!(held[count] = h_Held_r(host)))
909 assert(count == hostCount);
911 for ( i = 0 ; i < count ; i++) {
912 held[i] = (*proc)(list[i], held[i], param);
914 h_Release(list[i]);/* this might free up the host */
920 /* h_Enumerate_r: Calls (*proc)(host, held, param) for at least each host in
921 * the at the start of the enumeration (perhaps more). Hosts may be deleted
922 * (have delete flag set); ditto for clients. (*proc) is always called with
923 * host h_held() and the global host lock (H_LOCK) locked.The hold state of the
924 * host with respect to this lwp is passed to (*proc) as the param held.
925 * The proc should return 0 if the host should be released, 1 if it should
926 * be held after enumeration.
928 h_Enumerate_r(proc, param)
933 register struct host *host;
936 if (hostCount == 0) {
939 for (host = hostList ; host ; host = host->next) {
940 if (!(held = h_Held_r(host)))
942 held = (*proc)(host, held, param);
944 h_Release_r(host);/* this might free up the host */
949 /* Host is returned held */
950 struct host *h_GetHost_r(tcon)
951 struct rx_connection *tcon;
955 struct host *oldHost;
958 struct interfaceAddr interf;
960 afs_int32 buffer[AFS_MAX_INTERFACE_ADDR];
961 struct Identity *identP = NULL;
965 char hoststr[16], hoststr2[16];
967 haddr = rxr_HostOf(tcon);
968 hport = rxr_PortOf(tcon);
971 identP = (struct Identity *)rx_GetSpecific(tcon, rxcon_ident_key);
972 host = h_Lookup_r(haddr, hport, &held);
973 if (host && !identP && !(host->Console&1)) {
974 /* This is a new connection, and we already have a host
975 * structure for this address. Verify that the identity
976 * of the caller matches the identity in the host structure.
979 if ( !(host->hostFlags & ALTADDR) )
981 /* Another thread is doing initialization */
983 if ( !held) h_Release_r(host);
984 ViceLog(125, ("Host %s:%d starting h_Lookup again\n",
985 afs_inet_ntoa_r(host->host, hoststr), host->port));
988 host->hostFlags &= ~ALTADDR;
990 code = RXAFSCB_WhoAreYou(host->callback_rxcon, &interf);
992 if ( code == RXGEN_OPCODE ) {
993 identP = (struct Identity *)malloc(sizeof(struct Identity));
995 rx_SetSpecific(tcon, rxcon_ident_key, identP);
996 /* The host on this connection was unable to respond to
997 * the WhoAreYou. We will treat this as a new connection
998 * from the existing host. The worst that can happen is
999 * that we maintain some extra callback state information */
1000 if (host->interface) {
1002 ("Host %s:%d used to support WhoAreYou, deleting.\n",
1003 afs_inet_ntoa_r(host->host, hoststr), host->port));
1004 host->hostFlags |= HOSTDELETED;
1006 if (!held) h_Release_r(host);
1010 } else if (code == 0) {
1012 identP = (struct Identity *)malloc(sizeof(struct Identity));
1014 identP->uuid = interf.uuid;
1015 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1016 /* Check whether the UUID on this connection matches
1017 * the UUID in the host structure. If they don't match
1018 * then this is not the same host as before. */
1019 if ( !host->interface
1020 || !afs_uuid_equal(&interf.uuid, &host->interface->uuid) ) {
1022 ("Host %s:%d has changed its identity, deleting.\n",
1023 afs_inet_ntoa_r(host->host, hoststr), host->port));
1024 host->hostFlags |= HOSTDELETED;
1026 if (!held) h_Release_r(host);
1031 afs_inet_ntoa_r(host->host, hoststr);
1032 ViceLog(0,("CB: WhoAreYou failed for %s:%d, error %d\n",
1033 hoststr, ntohs(host->port), code));
1034 host->hostFlags |= VENUSDOWN;
1036 host->hostFlags |= ALTADDR;
1039 if ( ! (host->hostFlags & ALTADDR) )
1041 /* another thread is doing the initialisation */
1042 ViceLog(125, ("Host %s:%d waiting for host-init to complete\n",
1043 afs_inet_ntoa_r(host->host, hoststr), host->port));
1046 if ( !held) h_Release_r(host);
1047 ViceLog(125, ("Host %s:%d starting h_Lookup again\n",
1048 afs_inet_ntoa_r(host->host, hoststr), host->port));
1051 /* We need to check whether the identity in the host structure
1052 * matches the identity on the connection. If they don't match
1053 * then treat this a new host. */
1054 if ( !(host->Console&1)
1055 && ( ( !identP->valid && host->interface )
1056 || ( identP->valid && !host->interface )
1058 && !afs_uuid_equal(&identP->uuid, &host->interface->uuid) ) ) ) {
1059 /* The host in the cache is not the host for this connection */
1060 host->hostFlags |= HOSTDELETED;
1062 if (!held) h_Release_r(host);
1063 ViceLog(0, ("CB: new identity for host %s:%d, deleting\n",
1064 afs_inet_ntoa_r(host->host, hoststr), host->port));
1068 host = h_Alloc_r(tcon);
1071 h_gethostcps_r(host,FT_ApproxTime());
1072 if (!(host->Console&1)) {
1073 if (!identP || !interfValid) {
1075 code = RXAFSCB_WhoAreYou(host->callback_rxcon, &interf);
1077 if ( code == RXGEN_OPCODE ) {
1078 identP = (struct Identity *)malloc(sizeof(struct Identity));
1080 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1082 ("Host %s:%d does not support WhoAreYou.\n",
1083 afs_inet_ntoa_r(host->host, hoststr), host->port));
1085 } else if (code == 0) {
1087 identP = (struct Identity *)malloc(sizeof(struct Identity));
1089 identP->uuid = interf.uuid;
1090 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1091 ViceLog(25, ("WhoAreYou success on %s:%d\n",
1092 afs_inet_ntoa_r(host->host, hoststr), host->port));
1095 if (code == 0 && !identP->valid) {
1097 code = RXAFSCB_InitCallBackState(host->callback_rxcon);
1099 } else if (code == 0) {
1100 oldHost = h_LookupUuid_r(&identP->uuid);
1102 /* This is a new address for an existing host. Update
1103 * the list of interfaces for the existing host and
1104 * delete the host structure we just allocated. */
1105 if (!(held = h_Held_r(oldHost)))
1108 ViceLog(25, ("CB: new addr %s:%d for old host %s:%d\n",
1109 afs_inet_ntoa_r(host->host, hoststr), host->port,
1110 afs_inet_ntoa_r(oldHost->host, hoststr2), oldHost->port));
1111 host->hostFlags |= HOSTDELETED;
1115 addInterfaceAddr_r(host, haddr);
1117 /* This really is a new host */
1118 hashInsertUuid_r(&identP->uuid, host);
1120 code = RXAFSCB_InitCallBackState3(host->callback_rxcon,
1124 ViceLog(25, ("InitCallBackState3 success on %s:%d\n",
1125 afs_inet_ntoa_r(host->host, hoststr), host->port));
1126 assert(interfValid == 1);
1127 initInterfaceAddr_r(host, &interf);
1132 afs_inet_ntoa_r(host->host, hoststr);
1133 ViceLog(0,("CB: RCallBackConnectBack failed for %s:%d\n",
1134 hoststr, ntohs(host->port)));
1135 host->hostFlags |= VENUSDOWN;
1138 host->hostFlags |= RESETDONE;
1141 host->hostFlags |= ALTADDR;/* host structure iniatilisation complete */
1149 static char localcellname[PR_MAXNAMELEN+1];
1150 char local_realm[AFS_REALM_SZ] = "";
1153 void h_InitHostPackage()
1155 afsconf_GetLocalCell (confDir, localcellname, PR_MAXNAMELEN);
1156 if (!local_realm[0]) {
1157 if (afs_krb_get_lrealm(local_realm, 0) != 0/*KSUCCESS*/) {
1158 ViceLog(0, ("afs_krb_get_lrealm failed, using %s.\n",localcellname));
1159 strcpy (local_realm, localcellname);
1162 rxcon_ident_key = rx_KeyCreate((rx_destructor_t)free);
1163 rxcon_client_key = rx_KeyCreate((rx_destructor_t)0);
1164 #ifdef AFS_PTHREAD_ENV
1165 assert(pthread_mutex_init(&host_glock_mutex, NULL) == 0);
1166 #endif /* AFS_PTHREAD_ENV */
1169 static MapName_r(aname, acell, aval)
1178 afs_int32 anamelen, cnamelen;
1182 anamelen=strlen(aname);
1183 if (anamelen >= PR_MAXNAMELEN)
1184 return -1; /* bad name -- caller interprets this as anonymous, but retries later */
1186 lnames.namelist_len = 1;
1187 lnames.namelist_val = (prname *) aname; /* don't malloc in the common case */
1188 lids.idlist_len = 0;
1189 lids.idlist_val = NULL;
1191 cnamelen=strlen(acell);
1193 if (strcasecmp(local_realm, acell) && strcasecmp(localcellname, acell)) {
1194 ViceLog(2, ("MapName: cell is foreign. cell=%s, localcell=%s, localrealm=%s\n",
1195 acell, localcellname, local_realm));
1196 if ((anamelen+cnamelen+1) >= PR_MAXNAMELEN) {
1197 ViceLog(2, ("MapName: Name too long, using AnonymousID for %s@%s\n",
1199 *aval = AnonymousID;
1202 foreign = 1; /* attempt cross-cell authentication */
1203 tname = (char *) malloc(anamelen+cnamelen+2);
1204 strcpy(tname, aname);
1205 tname[anamelen] = '@';
1206 strcpy(tname+anamelen+1, acell);
1207 lnames.namelist_val = (prname *) tname;
1212 code = pr_NameToId(&lnames, &lids);
1215 if (lids.idlist_val) {
1216 *aval = lids.idlist_val[0];
1217 if (*aval == AnonymousID) {
1218 ViceLog(2, ("MapName: NameToId on %s returns anonymousID\n", lnames.namelist_val));
1220 free(lids.idlist_val); /* return parms are not malloced in stub if server proc aborts */
1222 ViceLog(0, ("MapName: NameToId on '%s' is unknown\n", lnames.namelist_val));
1228 free(lnames.namelist_val); /* We allocated this above, so we must free it now. */
1235 /* NOTE: this returns the client with a Shared lock */
1236 struct client *h_ID2Client(vid)
1239 register struct client *client;
1240 register struct host *host;
1244 for (host=hostList; host; host=host->next) {
1245 if (host->hostFlags & HOSTDELETED)
1247 for (client = host->FirstClient; client; client = client->next) {
1248 if (!client->deleted && client->ViceId == vid) {
1251 ObtainSharedLock(&client->lock);
1265 * Called by the server main loop. Returns a h_Held client, which must be
1266 * released later the main loop. Allocates a client if the matching one
1267 * isn't around. The client is returned with its reference count incremented
1268 * by one. The caller must call h_ReleaseClient_r when finished with
1271 struct client *h_FindClient_r(tcon)
1272 struct rx_connection *tcon;
1275 register struct client *client;
1276 register struct host *host;
1277 struct client *oldClient;
1282 #if (64-MAXKTCNAMELEN)
1283 ticket name length != 64
1287 char uname[PR_MAXNAMELEN];
1288 char tcell[MAXKTCREALMLEN];
1291 client = (struct client *) rx_GetSpecific(tcon, rxcon_client_key);
1292 if (client && !client->deleted) {
1294 h_Hold_r(client->host);
1295 if (client->prfail != 2) { /* Could add shared lock on client here */
1296 /* note that we don't have to lock entry in this path to
1297 * ensure CPS is initialized, since we don't call rxr_SetSpecific
1298 * until initialization is done, and we only get here if
1299 * rx_GetSpecific located the client structure.
1304 ObtainWriteLock(&client->lock); /* released at end */
1306 } else if (client) {
1310 authClass = rx_SecurityClassOf((struct rx_connection *)tcon);
1311 ViceLog(5,("FindClient: authenticating connection: authClass=%d\n",
1313 if (authClass == 1) {
1314 /* A bcrypt tickets, no longer supported */
1315 ViceLog(1, ("FindClient: bcrypt ticket, using AnonymousID\n"));
1316 viceid = AnonymousID;
1317 expTime = 0x7fffffff;
1318 } else if (authClass == 2) {
1321 /* kerberos ticket */
1322 code = rxkad_GetServerInfo (tcon, /*level*/0, &expTime,
1323 tname, tinst, tcell, &kvno);
1325 ViceLog(1, ("Failed to get rxkad ticket info\n"));
1326 viceid = AnonymousID;
1327 expTime = 0x7fffffff;
1329 int ilen = strlen(tinst);
1331 ("FindClient: rxkad conn: name=%s,inst=%s,cell=%s,exp=%d,kvno=%d\n",
1332 tname, tinst, tcell, expTime, kvno));
1333 strncpy (uname, tname, sizeof(uname));
1335 if (strlen(uname) + 1 + ilen >= sizeof(uname))
1337 strcat (uname, ".");
1338 strcat (uname, tinst);
1340 /* translate the name to a vice id */
1341 code = MapName_r(uname, tcell, &viceid);
1344 ViceLog(1, ("failed to map name=%s, cell=%s -> code=%d\n",
1345 uname, tcell, code));
1347 viceid = AnonymousID;
1348 expTime = 0x7fffffff;
1352 viceid = AnonymousID; /* unknown security class */
1353 expTime = 0x7fffffff;
1357 host = h_GetHost_r(tcon); /* Returns it h_Held */
1359 /* First try to find the client structure */
1360 for (client = host->FirstClient; client; client = client->next) {
1361 if (!client->deleted && (client->sid == rxr_CidOf(tcon)) &&
1362 (client->VenusEpoch == rxr_GetEpoch(tcon))) {
1363 if (client->tcon && (client->tcon != tcon)) {
1364 ViceLog(0, ("*** Vid=%d, sid=%x, tcon=%x, Tcon=%x ***\n",
1365 client->ViceId, client->sid, client->tcon, tcon));
1366 client->tcon = (struct rx_connection *)0;
1370 ObtainWriteLock(&client->lock);
1376 /* Still no client structure - get one */
1379 ObtainWriteLock(&client->lock);
1380 client->host = host;
1381 client->next = host->FirstClient;
1382 host->FirstClient = client;
1383 #if FS_STATS_DETAILED
1384 client->InSameNetwork = host->InSameNetwork;
1385 #endif /* FS_STATS_DETAILED */
1386 client->ViceId = viceid;
1387 client->expTime = expTime; /* rx only */
1388 client->authClass = authClass; /* rx only */
1389 client->sid = rxr_CidOf(tcon);
1390 client->VenusEpoch = rxr_GetEpoch(tcon);
1391 client->CPS.prlist_val = 0;
1392 client->refCount = 1;
1393 CurrentConnections++; /* increment number of connections */
1396 client->prfail = fail;
1398 if (!(client->CPS.prlist_val) || (viceid != client->ViceId)) {
1399 if (client->CPS.prlist_val && (client->ViceId != ANONYMOUSID)) {
1400 free(client->CPS.prlist_val);
1402 client->CPS.prlist_val = NULL;
1403 client->ViceId = viceid;
1404 client->expTime = expTime;
1406 if (viceid == ANONYMOUSID) {
1407 client->CPS.prlist_len = AnonCPS.prlist_len;
1408 client->CPS.prlist_val = AnonCPS.prlist_val;
1411 code = pr_GetCPS(viceid, &client->CPS);
1415 ViceLog(0, ("pr_GetCPS failed(%d) for user %d, host %s:%d\n",
1417 afs_inet_ntoa_r(client->host->host, hoststr),
1418 client->host->port));
1420 /* Although ubik_Call (called by pr_GetCPS) traverses thru
1421 * all protection servers and reevaluates things if no
1422 * sync server or quorum is found we could still end up
1423 * with one of these errors. In such case we would like to
1424 * reevaluate the rpc call to find if there's cps for this
1425 * guy. We treat other errors (except network failures
1426 * ones - i.e. code < 0) as an indication that there is no
1427 * CPS for this host. Ideally we could like to deal this
1428 * problem the other way around (i.e. if code == NOCPS
1429 * ignore else retry next time) but the problem is that
1430 * there're other errors (i.e. EPERM) for which we don't
1431 * want to retry and we don't know the whole code list!
1433 if (code < 0 || code == UNOQUORUM || code == UNOTSYNC)
1437 /* the disabling of system:administrators is so iffy and has so many
1438 * possible failure modes that we will disable it again */
1439 /* Turn off System:Administrator for safety
1440 if (AL_IsAMember(SystemId, client->CPS) == 0)
1441 assert(AL_DisableGroup(SystemId, client->CPS) == 0); */
1444 /* Now, tcon may already be set to a rock, since we blocked with no host
1445 * or client locks set above in pr_GetCPS (XXXX some locking is probably
1446 * required). So, before setting the RPC's rock, we should disconnect
1447 * the RPC from the other client structure's rock.
1449 if ((oldClient = (struct client *) rx_GetSpecific(tcon, rxcon_client_key))) {
1450 oldClient->tcon = (struct rx_connection *) 0;
1451 /* rx_SetSpecific will be done immediately below */
1453 client->tcon = tcon;
1454 rx_SetSpecific(tcon, rxcon_client_key, client);
1455 ReleaseWriteLock(&client->lock);
1459 } /*h_FindClient_r*/
1461 int h_ReleaseClient_r(client)
1462 struct client *client;
1464 assert(client->refCount > 0);
1471 * Sigh: this one is used to get the client AGAIN within the individual
1472 * server routines. This does not bother h_Holding the host, since
1473 * this is assumed already have been done by the server main loop.
1474 * It does check tokens, since only the server routines can return the
1475 * VICETOKENDEAD error code
1477 int GetClient(tcon, cp)
1478 struct rx_connection * tcon;
1482 register struct client *client;
1486 *cp = client = (struct client *) rx_GetSpecific(tcon, rxcon_client_key);
1488 assert(client && client->tcon && rxr_CidOf(client->tcon) == client->sid);
1490 client->LastCall > client->expTime && client->expTime) {
1492 ViceLog(1, ("Token for %s at %s:%d expired %d\n",
1494 afs_inet_ntoa_r(client->host->host, hoststr),
1495 client->host->port, client->expTime));
1497 return VICETOKENDEAD;
1506 /* Client user name for short term use. Note that this is NOT inexpensive */
1507 char *h_UserName(client)
1508 struct client *client;
1511 static char User[PR_MAXNAMELEN+1];
1515 lids.idlist_len = 1;
1516 lids.idlist_val = (afs_int32 *)malloc(1*sizeof(afs_int32));
1517 lnames.namelist_len = 0;
1518 lnames.namelist_val = (prname *)0;
1519 lids.idlist_val[0] = client->ViceId;
1520 if (pr_IdToName(&lids,&lnames)) {
1521 /* We need to free id we alloced above! */
1522 free(lids.idlist_val);
1523 return "*UNKNOWN USER NAME*";
1525 strncpy(User,lnames.namelist_val[0],PR_MAXNAMELEN);
1526 free(lids.idlist_val);
1527 free(lnames.namelist_val);
1537 ("Total Client entries = %d, blocks = %d; Host entries = %d, blocks = %d\n",
1538 CEs, CEBlocks, HTs, HTBlocks));
1543 static int h_PrintClient(host, held, file)
1544 register struct host *host;
1546 StreamHandle_t *file;
1548 register struct client *client;
1555 if (host->hostFlags & HOSTDELETED) {
1559 sprintf(tmpStr,"Host %s:%d down = %d, LastCall %s",
1560 afs_inet_ntoa_r(host->host, hoststr), host->port,
1561 (host->hostFlags & VENUSDOWN),
1562 afs_ctime((time_t *)&host->LastCall, tbuffer, sizeof(tbuffer)));
1563 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1564 for (client = host->FirstClient; client; client=client->next) {
1565 if (!client->deleted) {
1567 sprintf(tmpStr, " user id=%d, name=%s, sl=%s till %s",
1568 client->ViceId, h_UserName(client),
1569 client->authClass ? "Authenticated" : "Not authenticated",
1571 afs_ctime((time_t *)&client->expTime, tbuffer, sizeof(tbuffer))
1573 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1576 sprintf(tmpStr, " user=%s, no current server connection\n",
1577 h_UserName(client));
1578 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1580 sprintf(tmpStr, " CPS-%d is [", client->CPS.prlist_len);
1581 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1582 if (client->CPS.prlist_val) {
1583 for (i=0; i > client->CPS.prlist_len; i++) {
1584 sprintf(tmpStr, " %d", client->CPS.prlist_val[i]);
1585 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1588 sprintf(tmpStr, "]\n");
1589 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1600 * Print a list of clients, with last security level and token value seen,
1610 StreamHandle_t *file = STREAM_OPEN(AFSDIR_SERVER_CLNTDUMP_FILEPATH, "w");
1613 ViceLog(0, ("Couldn't create client dump file %s\n", AFSDIR_SERVER_CLNTDUMP_FILEPATH));
1616 now = FT_ApproxTime();
1617 sprintf(tmpStr, "List of active users at %s\n",
1618 afs_ctime(&now, tbuffer, sizeof(tbuffer)));
1619 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1620 h_Enumerate(h_PrintClient, (char *)file);
1621 STREAM_REALLYCLOSE(file);
1622 ViceLog(0, ("Created client dump %s\n", AFSDIR_SERVER_CLNTDUMP_FILEPATH));
1628 static int h_DumpHost(host, held, file)
1629 register struct host *host;
1631 StreamHandle_t *file;
1638 sprintf(tmpStr, "ip:%x port:%d hidx:%d cbid:%d lock:%x last:%u active:%u down:%d del:%d cons:%d cldel:%d\n\t hpfailed:%d hcpsCall:%u hcps [",
1639 host->host, host->port, host->index, host->cblist,
1640 CheckLock(&host->lock), host->LastCall, host->ActiveCall,
1641 (host->hostFlags & VENUSDOWN), host->hostFlags&HOSTDELETED,
1642 host->Console, host->hostFlags & CLIENTDELETED,
1643 host->hcpsfailed, host->cpsCall);
1644 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1645 if (host->hcps.prlist_val)
1646 for (i=0; i < host->hcps.prlist_len; i++) {
1647 sprintf(tmpStr, " %d", host->hcps.prlist_val[i]);
1648 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1650 sprintf(tmpStr, "] [");
1651 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1652 if ( host->interface)
1653 for (i=0; i < host->interface->numberOfInterfaces; i++) {
1654 sprintf(tmpStr, " %x", host->interface->addr[i]);
1655 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1657 sprintf(tmpStr, "] holds: ");
1658 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1660 for (i = 0 ; i < h_maxSlots ; i++) {
1661 sprintf(tmpStr, "%04x", host->holds[i]);
1662 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1664 sprintf(tmpStr, " slot/bit: %d/%d\n", h_holdSlot(), h_holdbit());
1665 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1677 StreamHandle_t *file = STREAM_OPEN(AFSDIR_SERVER_HOSTDUMP_FILEPATH, "w");
1682 ViceLog(0, ("Couldn't create host dump file %s\n", AFSDIR_SERVER_HOSTDUMP_FILEPATH));
1685 now = FT_ApproxTime();
1686 sprintf(tmpStr, "List of active hosts at %s\n",
1687 afs_ctime(&now, tbuffer, sizeof(tbuffer)));
1688 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1689 h_Enumerate(h_DumpHost, (char *) file);
1690 STREAM_REALLYCLOSE(file);
1691 ViceLog(0, ("Created host dump %s\n", AFSDIR_SERVER_HOSTDUMP_FILEPATH));
1697 * This counts the number of workstations, the number of active workstations,
1698 * and the number of workstations declared "down" (i.e. not heard from
1699 * recently). An active workstation has received a call since the cutoff
1700 * time argument passed.
1702 h_GetWorkStats(nump, activep, delp, cutofftime)
1706 afs_int32 cutofftime;
1710 register struct host *host;
1711 register int num=0, active=0, del=0;
1714 for (host = hostList; host; host = host->next) {
1715 if (!(host->hostFlags & HOSTDELETED)) {
1717 if (host->ActiveCall > cutofftime)
1719 if (host->hostFlags & VENUSDOWN)
1731 } /*h_GetWorkStats*/
1734 /*------------------------------------------------------------------------
1735 * PRIVATE h_ClassifyAddress
1738 * Given a target IP address and a candidate IP address (both
1739 * in host byte order), classify the candidate into one of three
1740 * buckets in relation to the target by bumping the counters passed
1744 * a_targetAddr : Target address.
1745 * a_candAddr : Candidate address.
1746 * a_sameNetOrSubnetP : Ptr to counter to bump when the two
1747 * addresses are either in the same network
1748 * or the same subnet.
1749 * a_diffSubnetP : ...when the candidate is in a different
1751 * a_diffNetworkP : ...when the candidate is in a different
1758 * The target and candidate addresses are both in host byte
1759 * order, NOT network byte order, when passed in.
1763 *------------------------------------------------------------------------*/
1765 static void h_ClassifyAddress(a_targetAddr, a_candAddr, a_sameNetOrSubnetP,
1766 a_diffSubnetP, a_diffNetworkP)
1767 afs_uint32 a_targetAddr;
1768 afs_uint32 a_candAddr;
1769 afs_int32 *a_sameNetOrSubnetP;
1770 afs_int32 *a_diffSubnetP;
1771 afs_int32 *a_diffNetworkP;
1773 { /*h_ClassifyAddress*/
1775 register int i; /*Iterator thru host hash table*/
1776 register struct host *hostP; /*Ptr to current host entry*/
1777 register afs_uint32 currHostAddr; /*Current host address*/
1778 afs_uint32 targetNet;
1779 afs_uint32 targetSubnet;
1781 afs_uint32 candSubnet;
1784 * Put bad values into the subnet info to start with.
1786 targetSubnet = (afs_uint32) 0;
1787 candSubnet = (afs_uint32) 0;
1790 * Pull out the network and subnetwork numbers from the target
1791 * and candidate addresses. We can short-circuit this whole
1792 * affair if the target and candidate addresses are not of the
1795 if (IN_CLASSA(a_targetAddr)) {
1796 if (!(IN_CLASSA(a_candAddr))) {
1797 (*a_diffNetworkP)++;
1800 targetNet = a_targetAddr & IN_CLASSA_NET;
1801 candNet = a_candAddr & IN_CLASSA_NET;
1802 if (IN_SUBNETA(a_targetAddr))
1803 targetSubnet = a_targetAddr & IN_CLASSA_SUBNET;
1804 if (IN_SUBNETA(a_candAddr))
1805 candSubnet = a_candAddr & IN_CLASSA_SUBNET;
1808 if (IN_CLASSB(a_targetAddr)) {
1809 if (!(IN_CLASSB(a_candAddr))) {
1810 (*a_diffNetworkP)++;
1813 targetNet = a_targetAddr & IN_CLASSB_NET;
1814 candNet = a_candAddr & IN_CLASSB_NET;
1815 if (IN_SUBNETB(a_targetAddr))
1816 targetSubnet = a_targetAddr & IN_CLASSB_SUBNET;
1817 if (IN_SUBNETB(a_candAddr))
1818 candSubnet = a_candAddr & IN_CLASSB_SUBNET;
1819 } /*Class B target*/
1821 if (IN_CLASSC(a_targetAddr)) {
1822 if (!(IN_CLASSC(a_candAddr))) {
1823 (*a_diffNetworkP)++;
1826 targetNet = a_targetAddr & IN_CLASSC_NET;
1827 candNet = a_candAddr & IN_CLASSC_NET;
1830 * Note that class C addresses can't have subnets,
1831 * so we leave the defaults untouched.
1833 } /*Class C target*/
1835 targetNet = a_targetAddr;
1836 candNet = a_candAddr;
1837 } /*Class D address*/
1840 * Now, simply compare the extracted net and subnet values for
1841 * the two addresses (which at this point are known to be of the
1844 if (targetNet == candNet) {
1845 if (targetSubnet == candSubnet)
1846 (*a_sameNetOrSubnetP)++;
1851 (*a_diffNetworkP)++;
1853 } /*h_ClassifyAddress*/
1856 /*------------------------------------------------------------------------
1857 * EXPORTED h_GetHostNetStats
1860 * Iterate through the host table, and classify each (non-deleted)
1861 * host entry into ``proximity'' categories (same net or subnet,
1862 * different subnet, different network).
1865 * a_numHostsP : Set to total number of (non-deleted) hosts.
1866 * a_sameNetOrSubnetP : Set to # hosts on same net/subnet as server.
1867 * a_diffSubnetP : Set to # hosts on diff subnet as server.
1868 * a_diffNetworkP : Set to # hosts on diff network as server.
1874 * We only count non-deleted hosts. The storage pointed to by our
1875 * parameters is zeroed upon entry.
1879 *------------------------------------------------------------------------*/
1881 void h_GetHostNetStats(a_numHostsP, a_sameNetOrSubnetP, a_diffSubnetP,
1883 afs_int32 *a_numHostsP;
1884 afs_int32 *a_sameNetOrSubnetP;
1885 afs_int32 *a_diffSubnetP;
1886 afs_int32 *a_diffNetworkP;
1888 { /*h_GetHostNetStats*/
1890 register struct host *hostP; /*Ptr to current host entry*/
1891 register afs_uint32 currAddr_HBO; /*Curr host addr, host byte order*/
1894 * Clear out the storage pointed to by our parameters.
1896 *a_numHostsP = (afs_int32) 0;
1897 *a_sameNetOrSubnetP = (afs_int32) 0;
1898 *a_diffSubnetP = (afs_int32) 0;
1899 *a_diffNetworkP = (afs_int32) 0;
1902 for (hostP = hostList; hostP; hostP = hostP->next) {
1903 if (!(hostP->hostFlags & HOSTDELETED)) {
1905 * Bump the number of undeleted host entries found.
1906 * In classifying the current entry's address, make
1907 * sure to first convert to host byte order.
1910 currAddr_HBO = (afs_uint32)ntohl(hostP->host);
1911 h_ClassifyAddress(FS_HostAddr_HBO,
1916 } /*Only look at non-deleted hosts*/
1917 } /*For each host record hashed to this index*/
1920 } /*h_GetHostNetStats*/
1922 static afs_uint32 checktime;
1923 static afs_uint32 clientdeletetime;
1924 static struct AFSFid zerofid;
1928 * XXXX: This routine could use Multi-R to avoid serializing the timeouts.
1929 * Since it can serialize them, and pile up, it should be a separate LWP
1930 * from other events.
1932 int CheckHost(host, held)
1933 register struct host *host;
1937 register struct client *client;
1938 struct interfaceAddr interf;
1941 /* Host is held by h_Enumerate */
1943 for (client = host->FirstClient; client; client = client->next) {
1944 if (client->refCount == 0 && client->LastCall < clientdeletetime) {
1945 client->deleted = 1;
1946 host->hostFlags |= CLIENTDELETED;
1949 if (host->LastCall < checktime) {
1951 if (!(host->hostFlags & HOSTDELETED)) {
1952 if (host->LastCall < clientdeletetime) {
1953 host->hostFlags |= HOSTDELETED;
1954 if (!(host->hostFlags & VENUSDOWN)) {
1955 host->hostFlags &= ~ALTADDR; /* alternate address invalid*/
1956 if (host->interface) {
1958 code = RXAFSCB_InitCallBackState3(host->callback_rxcon,
1963 code = RXAFSCB_InitCallBackState(host->callback_rxcon);
1966 host->hostFlags |= ALTADDR; /* alternate addresses valid */
1970 afs_inet_ntoa_r(host->host, hoststr);
1972 ("CB: RCallBackConnectBack (host.c) failed for host %s:%d\n",
1973 hoststr, ntohs(host->port)));
1974 host->hostFlags |= VENUSDOWN;
1976 /* Note: it's safe to delete hosts even if they have call
1977 * back state, because break delayed callbacks (called when a
1978 * message is received from the workstation) will always send a
1979 * break all call backs to the workstation if there is no
1985 if (!(host->hostFlags & VENUSDOWN) && host->cblist) {
1986 if (host->interface) {
1987 afsUUID uuid = host->interface->uuid;
1989 code = RXAFSCB_ProbeUuid(host->callback_rxcon, &uuid);
1992 if ( MultiProbeAlternateAddress_r(host) ) {
1994 afs_inet_ntoa_r(host->host, hoststr);
1996 ("ProbeUuid failed for host %s:%d\n",
1997 hoststr, ntohs(host->port)));
1998 host->hostFlags |= VENUSDOWN;
2003 code = RXAFSCB_Probe(host->callback_rxcon);
2007 afs_inet_ntoa_r(host->host, hoststr);
2008 ViceLog(0, ("ProbeUuid failed for host %s:%d\n",
2009 hoststr, ntohs(host->port)));
2010 host->hostFlags |= VENUSDOWN;
2025 * Set VenusDown for any hosts that have not had a call in 15 minutes and
2026 * don't respond to a probe. Note that VenusDown can only be cleared if
2027 * a message is received from the host (see ServerLWP in file.c).
2028 * Delete hosts that have not had any calls in 1 hour, clients that
2029 * have not had any calls in 15 minutes.
2031 * This routine is called roughly every 5 minutes.
2035 afs_uint32 now = FT_ApproxTime();
2037 memset((char *)&zerofid, 0, sizeof(zerofid));
2039 * Send a probe to the workstation if it hasn't been heard from in
2042 checktime = now - 15*60;
2043 clientdeletetime = now - 120*60; /* 2 hours ago */
2044 h_Enumerate(CheckHost, NULL);
2049 * This is called with host locked and held. At this point, the
2050 * hostHashTable should not be having entries for the alternate
2051 * interfaces. This function has to insert these entries in the
2054 * The addresses in the ineterfaceAddr list are in host byte order.
2057 initInterfaceAddr_r(host, interf)
2059 struct interfaceAddr *interf;
2063 afs_int32 myPort, myHost;
2065 struct Interface *interface;
2070 ViceLog(125,("initInterfaceAddr : host %x numAddr %d\n",
2071 host->host, interf->numberOfInterfaces));
2073 number = interf->numberOfInterfaces;
2074 myPort = host->port;
2075 myHost = host->host; /* current interface address */
2077 /* validation checks */
2080 ViceLog(0,("Number of alternate addresses returned is %d\n",
2086 * Convert IP addresses to network byte order, and remove for
2087 * duplicate IP addresses from the interface list.
2089 for (i = 0, count = 0, found = 0; i < number; i++)
2091 interf->addr_in[i] = htonl(interf->addr_in[i]);
2092 for (j = 0 ; j < count ; j++) {
2093 if (interf->addr_in[j] == interf->addr_in[i])
2097 interf->addr_in[count] = interf->addr_in[i];
2098 if (interf->addr_in[count] == myHost)
2105 * Allocate and initialize an interface structure for this host.
2108 interface = (struct Interface *)
2109 malloc(sizeof(struct Interface) +
2110 (sizeof(afs_int32) * (count-1)));
2112 interface->numberOfInterfaces = count;
2114 interface = (struct Interface *)
2115 malloc(sizeof(struct Interface) +
2116 (sizeof(afs_int32) * count));
2118 interface->numberOfInterfaces = count + 1;
2119 interface->addr[count] = myHost;
2121 interface->uuid = interf->uuid;
2122 for (i = 0 ; i < count ; i++)
2123 interface->addr[i] = interf->addr_in[i];
2125 assert(!host->interface);
2126 host->interface = interface;
2128 for ( i=0; i < host->interface->numberOfInterfaces; i++)
2130 ViceLog(125,("--- alt address %x\n", host->interface->addr[i]));
2137 * This is called with host locked and held. At this point, the
2138 * hostHashTable should not be having entries for the alternate
2139 * interfaces. This function has to insert these entries in the
2142 * All addresses are in network byte order.
2145 addInterfaceAddr_r(host, addr)
2152 struct Interface *interface;
2155 assert(host->interface);
2157 ViceLog(125,("addInterfaceAddr : host %x addr %d\n",
2161 * Make sure this address is on the list of known addresses
2164 number = host->interface->numberOfInterfaces;
2165 for ( i=0, found=0; i < number && !found; i++)
2167 if ( host->interface->addr[i] == addr)
2171 interface = (struct Interface *)
2172 malloc(sizeof(struct Interface) +
2173 (sizeof(afs_int32) * number));
2174 interface->numberOfInterfaces = number + 1;
2175 interface->uuid = host->interface->uuid;
2176 for (i = 0 ; i < number ; i++)
2177 interface->addr[i] = host->interface->addr[i];
2178 interface->addr[number] = addr;
2179 free(host->interface);
2180 host->interface = interface;
2184 * Create a hash table entry for this address
2186 hashInsert_r(addr, host);
2191 /* inserts a new HashChain structure corresponding to this address */
2192 hashInsert_r(addr, host)
2197 struct h_hashChain* chain;
2199 /* hash into proper bucket */
2200 index = h_HashIndex(addr);
2202 /* insert into beginning of list for this bucket */
2203 chain = (struct h_hashChain *)malloc(sizeof(struct h_hashChain));
2205 chain->hostPtr = host;
2206 chain->next = hostHashTable[index];
2208 hostHashTable[index] = chain;
2212 /* inserts a new HashChain structure corresponding to this UUID */
2213 hashInsertUuid_r(uuid, host)
2214 struct afsUUID *uuid;
2218 struct h_hashChain* chain;
2220 /* hash into proper bucket */
2221 index = h_UuidHashIndex(uuid);
2223 /* insert into beginning of list for this bucket */
2224 chain = (struct h_hashChain *)malloc(sizeof(struct h_hashChain));
2226 chain->hostPtr = host;
2227 chain->next = hostUuidHashTable[index];
2228 hostUuidHashTable[index] = chain;
2231 /* deleted a HashChain structure for this address and host */
2232 /* returns 1 on success */
2234 hashDelete_r(addr, host)
2240 register struct h_hashChain **hp, *th;
2242 for (hp = &hostHashTable[h_HashIndex(addr)]; (th = *hp); )
2244 assert(th->hostPtr);
2245 if (th->hostPtr == host && th->addr == addr)
2260 ** prints out all alternate interface address for the host. The 'level'
2261 ** parameter indicates what level of debugging sets this output
2263 printInterfaceAddr(host, level)
2268 if ( host-> interface )
2270 /* check alternate addresses */
2271 number = host->interface->numberOfInterfaces;
2272 assert( number > 0 );
2273 for ( i=0; i < number; i++)
2274 ViceLog(level, ("%x ", host->interface->addr[i]));
2276 ViceLog(level, ("\n"));