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>
16 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <netinet/in.h>
32 #include <rx/rx_multi.h>
33 #include <afs/cellconfig.h>
35 #include <afs/afsutil.h>
36 #include <afs/netutils.h>
39 #define UBIK_INTERNALS
43 /*! \name statics used to determine if we're the sync site */
44 static afs_int32 syncSiteUntil = 0; /*!< valid only if amSyncSite */
45 int ubik_amSyncSite = 0; /*!< flag telling if I'm sync site */
46 static nServers; /*!< total number of servers */
47 static char amIMagic = 0; /*!< is this host the magic host */
48 char amIClone = 0; /*!< is this a clone which doesn't vote */
49 static char ubik_singleServer = 0;
51 int (*ubik_CRXSecurityProc) ();
52 char *ubik_CRXSecurityRock;
53 afs_int32 ubikSecIndex;
54 struct rx_securityClass *ubikSecClass;
55 static verifyInterfaceAddress();
59 * Module responsible for both deciding if we're currently the sync site,
60 * and keeping collecting votes so as to stay sync site.
62 * The basic module contacts all of the servers it can, trying to get them to vote
63 * for this server for sync site. The vote request message (called a beacon message)
64 * also specifies until which time this site claims to be the sync site, if at all, thus enabling
65 * receiving sites to know how long the sync site guarantee is made for.
67 * Each of these beacon messages is thus both a declaration of how long this site will
68 * remain sync site, and an attempt to extend that time by collecting votes for a later
69 * sync site extension.
71 * The voting module is responsible for choosing a reasonable time until which it promises
72 * not to vote for someone else. This parameter (BIG seconds) is not actually passed in
73 * the interface (perhaps it should be?) but is instead a compile time constant that both
76 * The beacon and vote modules work intimately together; the vote module decides how long
77 * it should promise the beacon module its vote, and the beacon module takes all of these
78 * votes and decides for how long it is the synchronization site.
81 /*! \brief procedure called from debug rpc call to get this module's state for debugging */
84 register struct ubik_debug *aparm;
86 /* fill in beacon's state fields in the ubik_debug structure */
87 aparm->syncSiteUntil = syncSiteUntil;
88 aparm->nServers = nServers;
92 * \brief Procedure that determines whether this site has enough current votes to remain sync site.
94 * Called from higher-level modules (everything but the vote module).
96 * If we're the sync site, check that our guarantees, obtained by the ubeacon_Interact()
97 * light-weight process, haven't expired. We're sync site as long as a majority of the
98 * servers in existence have promised us unexpired guarantees. The variable #ubik_syncSiteUntil
99 * contains the time at which the latest of the majority of the sync site guarantees expires
100 * (if the variable #ubik_amSyncSite is true)
101 * This module also calls up to the recovery module if it thinks that the recovery module
102 * may have to pick up a new database (which offucr sif [sic] we lose the sync site votes).
104 * \return 1 if local site is the sync site
105 * \return 0 if sync site is elsewhere
109 register afs_int32 now;
110 register afs_int32 rcode;
112 /* special case for fast startup */
113 if (nServers == 1 && !amIClone) {
114 return 1; /* one guy is always the sync site */
117 if (ubik_amSyncSite == 0 || amIClone)
118 rcode = 0; /* if I don't think I'm the sync site, say so */
120 now = FT_ApproxTime();
121 if (syncSiteUntil <= now) { /* if my votes have expired, say so */
123 ubik_dprint("Ubik: I am no longer the sync site\n");
127 rcode = 1; /* otherwise still have the required votes */
131 urecovery_ResetState(); /* force recovery to re-execute */
132 ubik_dprint("beacon: amSyncSite is %d\n", rcode);
137 * \see ubeacon_InitServerListCommon()
139 ubeacon_InitServerListByInfo(ame, info, clones)
141 struct afsconf_cell *info;
146 code = ubeacon_InitServerListCommon(ame, info, clones, 0);
151 * \param ame "address of me"
152 * \param aservers list of other servers
154 * \see ubeacon_InitServerListCommon()
156 ubeacon_InitServerList(ame, aservers)
158 register afs_int32 aservers[];
163 ubeacon_InitServerListCommon(ame, (struct afsconf_cell *)0, 0,
169 * \brief setup server list
171 * \param ame "address of me"
172 * \param aservers list of other servers
174 * called only at initialization to set up the list of servers to contact for votes. Just creates
175 * the server structure.
177 * The "magic" host is the one with the lowest internet address. It is
178 * magic because its vote counts epsilon more than the others. This acts
179 * as a tie-breaker when we have an even number of hosts in the system.
180 * For example, if the "magic" host is up in a 2 site system, then it
181 * is sync site. Without the magic host hack, if anyone crashed in a 2
182 * site system, we'd be out of business.
184 * \note There are two connections in every server structure, one for
185 * vote calls (which must always go through quickly) and one for database operations, which
186 * are subject to waiting for locks. If we used only one, the votes would sometimes get
187 * held up behind database operations, and the sync site guarantees would timeout
188 * even though the host would be up for communication.
190 * \see ubeacon_InitServerList(), ubeacon_InitServerListByInfo()
192 ubeacon_InitServerListCommon(ame, info, clones, aservers)
194 struct afsconf_cell *info;
196 register afs_int32 aservers[];
198 register struct ubik_server *ts;
200 register afs_int32 servAddr;
201 register afs_int32 i, code;
203 struct ubik_server *magicServer;
205 /* verify that the addresses passed in are correct */
206 if ((code = verifyInterfaceAddress(&ame, info, aservers)))
209 /* get the security index to use, if we can */
210 if (ubik_CRXSecurityProc) {
211 i = (*ubik_CRXSecurityProc) (ubik_CRXSecurityRock, &ubikSecClass,
216 /* don't have sec module yet */
218 ubikSecClass = rxnull_NewClientSecurityObject();
220 magicHost = ntohl(ame); /* do comparisons in host order */
221 magicServer = (struct ubik_server *)0;
224 for (i = 0; i < info->numServers; i++) {
225 if (ntohl((afs_uint32) info->hostAddr[i].sin_addr.s_addr) ==
226 ntohl((afs_uint32) ame)) {
235 for (i = 0; i < info->numServers; i++) {
238 ts = (struct ubik_server *)malloc(sizeof(struct ubik_server));
239 memset(ts, 0, sizeof(struct ubik_server));
240 ts->next = ubik_servers;
242 ts->addr[0] = info->hostAddr[i].sin_addr.s_addr;
247 || ntohl((afs_uint32) ts->addr[0]) <
248 (afs_uint32) magicHost) {
249 magicHost = ntohl(ts->addr[0]);
256 rx_NewConnection(info->hostAddr[i].sin_addr.s_addr,
257 ubik_callPortal, VOTE_SERVICE_ID,
258 ubikSecClass, ubikSecIndex);
261 rx_NewConnection(info->hostAddr[i].sin_addr.s_addr,
262 ubik_callPortal, DISK_SERVICE_ID,
263 ubikSecClass, ubikSecIndex);
268 while ((servAddr = *aservers++)) {
270 return UNHOSTS; /* too many hosts */
271 ts = (struct ubik_server *)malloc(sizeof(struct ubik_server));
272 memset(ts, 0, sizeof(struct ubik_server));
273 ts->next = ubik_servers;
275 ts->addr[0] = servAddr; /* primary address in net byte order */
276 ts->vote_rxcid = rx_NewConnection(servAddr, ubik_callPortal, VOTE_SERVICE_ID, ubikSecClass, ubikSecIndex); /* for vote reqs */
277 ts->disk_rxcid = rx_NewConnection(servAddr, ubik_callPortal, DISK_SERVICE_ID, ubikSecClass, ubikSecIndex); /* for disk reqs */
278 ts->isClone = 0; /* don't know about clones */
280 if (ntohl((afs_uint32) servAddr) < (afs_uint32) magicHost) {
281 magicHost = ntohl(servAddr);
288 magicServer->magic = 1; /* remember for when counting votes */
290 if (!amIClone && !magicServer)
294 ++nServers; /* count this server as well as the remotes */
296 nServers = i + 1; /* count this server as well as the remotes */
298 ubik_quorum = (nServers >> 1) + 1; /* compute the majority figure */
299 /* send addrs to all other servers */
300 code = updateUbikNetworkAddress(ubik_host);
304 /* Shoud we set some defaults for RX??
306 r_nRetries = (RPCTIMEOUT/r_retryInterval);
309 if (!ubik_servers) /* special case 1 server */
310 ubik_singleServer = 1;
311 if (nServers == 1 && !amIClone) {
312 ubik_amSyncSite = 1; /* let's start as sync site */
313 syncSiteUntil = 0x7fffffff; /* and be it quite a while */
316 if (nServers == 1) /* special case 1 server */
317 ubik_singleServer = 1;
320 if (ubik_singleServer) {
321 if (!ubik_amSyncSite)
322 ubik_dprint("Ubik: I am the sync site - 1 server\n");
324 syncSiteUntil = 0x7fffffff; /* quite a while */
330 * \brief main lwp loop for code that sends out beacons.
332 * This code only runs while we're sync site or we want to be the sync site.
333 * It runs in its very own light-weight process.
336 ubeacon_Interact(void *dummy)
338 register afs_int32 code;
340 struct rx_connection *connections[MAXSERVERS];
341 struct ubik_server *servers[MAXSERVERS];
342 register afs_int32 i;
343 register struct ubik_server *ts;
344 afs_int32 temp, yesVotes, lastWakeupTime, oldestYesVote, syncsite;
345 struct ubik_tid ttid;
348 /* loop forever getting votes */
349 lastWakeupTime = 0; /* keep track of time we last started a vote collection */
352 /* don't wakeup more than every POLLTIME seconds */
353 temp = (lastWakeupTime + POLLTIME) - FT_ApproxTime();
354 /* don't sleep if last collection phase took too long (probably timed someone out ) */
360 #ifdef AFS_PTHREAD_ENV
361 code = select(0, 0, 0, 0, &tt);
363 code = IOMGR_Select(0, 0, 0, 0, &tt);
368 lastWakeupTime = FT_ApproxTime(); /* started a new collection phase */
370 if (ubik_singleServer)
371 continue; /* special-case 1 server for speedy startup */
373 if (!uvote_ShouldIRun())
374 continue; /* if voter has heard from a better candidate than us, don't bother running */
376 /* otherwise we should run for election, or we're the sync site (and have already won);
377 * send out the beacon packets */
378 /* build list of all up hosts (noticing dead hosts are running again
379 * is a task for the recovery module, not the beacon module), and
380 * prepare to send them an r multi-call containing the beacon message */
381 i = 0; /* collect connections */
382 for (ts = ubik_servers; ts; ts = ts->next) {
383 if (ts->up && ts->addr[0] != ubik_host[0]) {
385 connections[i++] = ts->vote_rxcid;
388 servers[i] = (struct ubik_server *)0; /* end of list */
389 /* note that we assume in the vote module that we'll always get at least BIGTIME
390 * seconds of vote from anyone who votes for us, which means we can conservatively
391 * assume we'll be fine until SMALLTIME seconds after we start collecting votes */
392 /* this next is essentially an expansion of rgen's ServBeacon routine */
394 ttid.epoch = ubik_epochTime;
395 if (ubik_dbase->flags & DBWRITING) {
397 * if a write is in progress, we have to send the writeTidCounter
398 * which holds the tid counter of the write transaction , and not
399 * send the tidCounter value which holds the tid counter of the
402 ttid.counter = ubik_dbase->writeTidCounter;
404 ttid.counter = ubik_dbase->tidCounter + 1;
405 #if defined(UBIK_PAUSE)
406 ubik_dbase->flags |= DBVOTING;
407 #endif /* UBIK_PAUSE */
409 /* now analyze return codes, counting up our votes */
410 yesVotes = 0; /* count how many to ensure we have quorum */
411 oldestYesVote = 0x7fffffff; /* time quorum expires */
412 syncsite = ubeacon_AmSyncSite();
413 startTime = FT_ApproxTime();
415 * Don't waste time using mult Rx calls if there are no connections out there
418 multi_Rx(connections, i) {
419 multi_VOTE_Beacon(syncsite, startTime, &ubik_dbase->version,
421 temp = FT_ApproxTime(); /* now, more or less */
422 ts = servers[multi_i];
423 ts->lastBeaconSent = temp;
425 /* note that the vote time (the return code) represents the time
426 * the vote was computed, *not* the time the vote expires. We compute
427 * the latter down below if we got enough votes to go with */
429 ts->lastVoteTime = code;
430 if (code < oldestYesVote)
431 oldestYesVote = code;
436 yesVotes++; /* the extra epsilon */
437 ts->up = 1; /* server is up (not really necessary: recovery does this for real) */
438 ts->beaconSinceDown = 1;
439 ubik_dprint("yes vote from host %s\n",
440 afs_inet_ntoa(ts->addr[0]));
441 } else if (code == 0) {
442 ts->lastVoteTime = temp;
444 ts->beaconSinceDown = 1;
445 ubik_dprint("no vote from %s\n",
446 afs_inet_ntoa(ts->addr[0]));
447 } else if (code < 0) {
449 ts->beaconSinceDown = 0;
450 urecovery_LostServer();
451 ubik_dprint("time out from %s\n",
452 afs_inet_ntoa(ts->addr[0]));
457 /* now call our own voter module to see if we'll vote for ourself. Note that
458 * the same restrictions apply for our voting for ourself as for our voting
459 * for anyone else. */
460 i = SVOTE_Beacon((struct rx_call *)0, ubeacon_AmSyncSite(), startTime,
461 &ubik_dbase->version, &ttid);
465 yesVotes++; /* extra epsilon */
466 if (i < oldestYesVote)
469 #if defined(UBIK_PAUSE)
470 ubik_dbase->flags &= ~DBVOTING;
471 #endif /* UBIK_PAUSE */
473 /* now decide if we have enough votes to become sync site.
474 * Note that we can still get enough votes even if we didn't for ourself. */
475 if (yesVotes > nServers) { /* yesVotes is bumped by 2 or 3 for each site */
476 if (!ubik_amSyncSite)
477 ubik_dprint("Ubik: I am the sync site\n");
479 syncSiteUntil = oldestYesVote + SMALLTIME;
480 #ifndef AFS_PTHREAD_ENV
481 /* I did not find a corresponding LWP_WaitProcess(&ubik_amSyncSite) --
482 this may be a spurious signal call -- sjenkins */
483 LWP_NoYieldSignal(&ubik_amSyncSite);
487 ubik_dprint("Ubik: I am no longer the sync site\n");
489 urecovery_ResetState(); /* tell recovery we're no longer the sync site */
497 * \brief Verify that a given IP addresses does actually exist on this machine.
499 * \param ame the pointer to my IP address specified in the
501 * \param aservers an array containing IP
502 * addresses of remote ubik servers. The array is
503 * terminated by a zero address.
505 * Algorithm : Verify that my IP addresses \p ame does actually exist
506 * on this machine. If any of my IP addresses are there
507 * in the remote server list \p aserver, remove them from
508 * this list. Update global variable \p ubik_host[] with
511 * \return 0 on success, non-zero on failure
514 verifyInterfaceAddress(ame, info, aservers)
515 afs_uint32 *ame; /* one of my interface addr in net byte order */
516 struct afsconf_cell *info;
517 afs_uint32 aservers[]; /* list of all possible server addresses */
519 afs_uint32 myAddr[UBIK_MAX_INTERFACE_ADDR], *servList, tmpAddr;
520 afs_uint32 myAddr2[UBIK_MAX_INTERFACE_ADDR];
521 int tcount, count, found, i, j, totalServers, start, end, usednetfiles =
525 totalServers = info->numServers;
526 else { /* count the number of servers */
527 for (totalServers = 0, servList = aservers; *servList; servList++)
531 if (AFSDIR_SERVER_NETRESTRICT_FILEPATH || AFSDIR_SERVER_NETINFO_FILEPATH) {
533 * Find addresses we are supposed to register as per the netrestrict file
534 * if it exists, else just register all the addresses we find on this
535 * host as returned by rx_getAllAddr (in NBO)
539 parseNetFiles(myAddr, NULL, NULL, UBIK_MAX_INTERFACE_ADDR, reason,
540 AFSDIR_SERVER_NETINFO_FILEPATH,
541 AFSDIR_SERVER_NETRESTRICT_FILEPATH);
543 ubik_print("ubik: Can't register any valid addresses:%s\n",
545 ubik_print("Aborting..\n");
550 /* get all my interface addresses in net byte order */
551 count = rx_getAllAddr(myAddr, UBIK_MAX_INTERFACE_ADDR);
554 if (count <= 0) { /* no address found */
555 ubik_print("ubik: No network addresses found, aborting..");
559 /* verify that the My-address passed in by ubik is correct */
560 for (j = 0, found = 0; j < count; j++) {
561 if (*ame == myAddr[j]) { /* both in net byte order */
568 ubik_print("ubik: primary address %s does not exist\n",
569 afs_inet_ntoa(*ame));
570 /* if we had the result of rx_getAllAddr already, avoid subverting
571 * the "is gethostbyname(gethostname()) us" check. If we're
572 * using NetInfo/NetRestrict, we assume they have enough clue
573 * to avoid that big hole in their foot from the loaded gun. */
575 /* take the address we did get, then see if ame was masked */
577 tcount = rx_getAllAddr(myAddr2, UBIK_MAX_INTERFACE_ADDR);
578 if (tcount <= 0) { /* no address found */
579 ubik_print("ubik: No network addresses found, aborting..");
583 /* verify that the My-address passed in by ubik is correct */
584 for (j = 0, found = 0; j < tcount; j++) {
585 if (*ame == myAddr2[j]) { /* both in net byte order */
595 /* if any of my addresses are there in serverList, then
596 ** use that as my primary addresses : the higher level
597 ** application screwed up in dealing with multihomed concepts
599 for (j = 0, found = 0; j < count; j++) {
600 for (i = 0; i < totalServers; i++) {
602 tmpAddr = (afs_uint32) info->hostAddr[i].sin_addr.s_addr;
604 tmpAddr = aservers[i];
605 if (myAddr[j] == tmpAddr) {
614 ubik_print("Using %s as my primary address\n", afs_inet_ntoa(*ame));
617 /* get rid of servers which were purged because all
618 ** those interface addresses are myself
620 for (start = 0, end = totalServers - 1; (start < end); start++, end--) {
621 /* find the first zero entry from the beginning */
622 for (; (start < end) && (aservers[start]); start++);
624 /* find the last non-zero entry from the end */
625 for (; (end >= 0) && (!aservers[end]); end--);
627 /* if there is nothing more to purge, exit from loop */
632 aservers[start] = aservers[end];
633 aservers[end] = 0; /* this entry was moved */
637 /* update all my addresses in ubik_host in such a way
638 ** that ubik_host[0] has the primary address
641 for (j = 0, i = 1; j < count; j++)
642 if (*ame != myAddr[j])
643 ubik_host[i++] = myAddr[j];
645 return 0; /* return success */
650 * \brief Exchange IP address information with remote servers.
652 * \param ubik_host an array containing all my IP addresses.
654 * Algorithm : Do an RPC to all remote ubik servers infroming them
655 * about my IP addresses. Get their IP addresses and
656 * update my linked list of ubik servers \p ubik_servers
658 * \return 0 on success, non-zero on failure
661 updateUbikNetworkAddress(ubik_host)
662 afs_uint32 ubik_host[UBIK_MAX_INTERFACE_ADDR];
664 int j, count, code = 0;
665 UbikInterfaceAddr inAddr, outAddr;
666 struct rx_connection *conns[MAXSERVERS];
667 struct ubik_server *ts, *server[MAXSERVERS];
670 for (count = 0, ts = ubik_servers; ts; count++, ts = ts->next) {
671 conns[count] = ts->disk_rxcid;
676 /* inform all other servers only if there are more than one
677 * database servers in the cell */
681 for (j = 0; j < UBIK_MAX_INTERFACE_ADDR; j++)
682 inAddr.hostAddr[j] = ntohl(ubik_host[j]);
685 /* do the multi-RX RPC to all other servers */
686 multi_Rx(conns, count) {
687 multi_DISK_UpdateInterfaceAddr(&inAddr, &outAddr);
688 ts = server[multi_i]; /* reply received from this server */
690 if (ts->addr[0] != htonl(outAddr.hostAddr[0])) {
692 strcpy(buffer, (char *)afs_inet_ntoa(ts->addr[0]));
693 ubik_print("ubik:Two primary addresses for same server \
694 %s %s\n", buffer, afs_inet_ntoa(htonl(outAddr.hostAddr[0])));
696 for (j = 1; j < UBIK_MAX_INTERFACE_ADDR; j++)
697 ts->addr[j] = htonl(outAddr.hostAddr[j]);
699 } else if (multi_error == RXGEN_OPCODE) { /* pre 3.5 remote server */
701 ("ubik server %s does not support UpdateInterfaceAddr RPC\n",
702 afs_inet_ntoa(ts->addr[0]));
703 } else if (multi_error == UBADHOST) {
704 code = UBADHOST; /* remote CellServDB inconsistency */
705 ubik_print("Inconsistent Cell Info on server: ");
706 for (j = 0; j < UBIK_MAX_INTERFACE_ADDR && ts->addr[j]; j++)
707 ubik_print("%s ", afs_inet_ntoa(ts->addr[j]));
710 ts->up = 0; /* mark the remote server as down */