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>
13 #ifdef IGNORE_SOME_GCC_WARNINGS
14 # pragma GCC diagnostic warning "-Wstrict-prototypes"
18 #include "afsincludes.h"
22 #include <afs/pthread_glock.h>
33 #include <netinet/in.h>
35 #include <afs/rxgen_consts.h>
36 #define UBIK_LEGACY_CALLITER
43 short ubik_initializationState; /*!< initial state is zero */
47 * \brief Parse list for clients.
50 ubik_ParseClientList(int argc, char **argv, afs_uint32 * aothers)
59 inServer = 0; /* haven't seen -servers yet */
61 for (i = 1; i < argc; i++) {
62 /* look for -servers argument */
68 /* otherwise this is a new host name */
70 th = gethostbyname(tp);
75 memmove((void *)&temp, (const void *)th->h_addr,
78 if (counter++ >= MAXSERVERS)
82 /* haven't seen a -server yet */
83 if (!strcmp(tp, "-servers")) {
89 /* never saw a -server */
92 if (counter < MAXSERVERS)
93 *aothers++ = 0; /* null terminate if room */
97 #include <afs/afs_assert.h>
98 #ifdef AFS_PTHREAD_ENV
101 static pthread_once_t random_once = PTHREAD_ONCE_INIT;
102 static int called_afs_random_once;
103 static pthread_key_t random_number_key;
106 afs_random_once(void)
108 osi_Assert(pthread_key_create(&random_number_key, NULL) == 0);
109 called_afs_random_once = 1;
114 #if !defined(UKERNEL)
116 * \brief use time and pid to try to get some initial randomness.
118 #define ranstage(x) (x)= (afs_uint32) (3141592621U*((afs_uint32)x)+1)
121 * \brief Random number generator and constants from KnuthV2 2d ed, p170
124 * X = (aX + c) % m \n
125 * m is a power of two \n
127 * a is 0.73m should be 0.01m .. 0.99m \n
128 * c is more or less immaterial. 1 or a is suggested. \n
130 * NB: LOW ORDER BITS are not very random. To get small random numbers,
131 * treat result as <1, with implied binary point, and multiply by
134 * NB: Has to be unsigned, since shifts on signed quantities may preserve
137 * In this case, m == 2^32, the mod operation is implicit. a == pi, which
138 * is used because it has some interesting characteristics (lacks any
139 * interesting bit-patterns).
144 #ifdef AFS_PTHREAD_ENV
147 if (!called_afs_random_once)
148 pthread_once(&random_once, afs_random_once);
150 state = (uintptr_t) pthread_getspecific(random_number_key);
152 static afs_uint32 state = 0;
157 state = time(0) + getpid();
158 for (i = 0; i < 15; i++) {
164 #ifdef AFS_PTHREAD_ENV
165 pthread_setspecific(random_number_key, (const void *)(uintptr_t)state);
172 * \brief Returns int 0..14 using the high bits of a pseudo-random number instead of
173 * the low bits, as the low bits are "less random" than the high ones...
175 * \todo Slight roundoff error exists, an excercise for the reader.
177 * Need to multiply by something with lots of ones in it, so multiply by
178 * 8 or 16 is right out.
181 afs_randomMod15(void)
185 temp = afs_random() >> 4;
186 temp = (temp * 15) >> 28;
190 #endif /* !defined(UKERNEL) */
195 #define abs(a) ((a) < 0 ? -1*(a) : (a))
197 ubik_ClientInit(struct rx_connection **serverconns,
198 struct ubik_client **aclient)
203 struct ubik_client *tc;
205 initialize_U_error_table();
207 if (*aclient) { /* the application is doing a re-initialization */
208 LOCK_UBIK_CLIENT((*aclient));
209 /* this is an important defensive check */
210 if (!((*aclient)->initializationState)) {
211 UNLOCK_UBIK_CLIENT((*aclient));
212 return UREINITIALIZE;
215 /* release all existing connections */
216 for (tc = *aclient, i = 0; i < MAXSERVERS; i++) {
217 struct rx_connection *rxConn = ubik_GetRPCConn(tc, i);
220 #ifdef AFS_PTHREAD_ENV
221 rx_ReleaseCachedConnection(rxConn);
223 rx_DestroyConnection(rxConn);
226 UNLOCK_UBIK_CLIENT((*aclient));
227 #ifdef AFS_PTHREAD_ENV
228 if (pthread_mutex_destroy(&((*aclient)->cm)))
229 return UMUTEXDESTROY;
232 tc = (struct ubik_client *)malloc(sizeof(struct ubik_client));
236 memset((void *)tc, 0, sizeof(*tc));
237 #ifdef AFS_PTHREAD_ENV
238 if (pthread_mutex_init(&(tc->cm), (const pthread_mutexattr_t *)0)) {
242 tc->initializationState = ++ubik_initializationState;
244 /* first count the # of server conns so we can randomize properly */
246 for (i = 0; i < MAXSERVERS; i++) {
247 if (serverconns[i] == (struct rx_connection *)0)
252 /* here count is the # of servers we're actually passed in. Compute
253 * offset, a number between 0..count-1, where we'll start copying from the
254 * client-provided array. */
255 for (i = 0; i < count; i++) {
256 offset = afs_randomMod15() % count;
257 for (j = abs(offset); j < 2 * count; j++) {
258 if (!tc->conns[abs(j % count)]) {
259 tc->conns[abs(j % count)] = serverconns[i];
270 * \brief Destroy an ubik connection.
272 * It calls rx to destroy the component rx connections, then frees the ubik
273 * connection structure.
276 ubik_ClientDestroy(struct ubik_client * aclient)
282 LOCK_UBIK_CLIENT(aclient);
283 for (c = 0; c < MAXSERVERS; c++) {
284 struct rx_connection *rxConn = ubik_GetRPCConn(aclient, c);
287 #ifdef AFS_PTHREAD_ENV
288 rx_ReleaseCachedConnection(rxConn);
290 rx_DestroyConnection(rxConn);
293 aclient->initializationState = 0; /* client in not initialized */
294 UNLOCK_UBIK_CLIENT(aclient);
295 #ifdef AFS_PTHREAD_ENV
296 pthread_mutex_destroy(&(aclient->cm)); /* ignore failure */
303 * \brief So that intermittent failures that cause connections to die
304 * don't kill whole ubik connection, refresh them when the connection is in
307 struct rx_connection *
308 ubik_RefreshConn(struct rx_connection *tc)
313 struct rx_securityClass *sc;
315 struct rx_connection *newTc;
317 host = rx_HostOf(rx_PeerOf(tc));
318 port = rx_PortOf(rx_PeerOf(tc));
319 service = rx_ServiceIdOf(tc);
320 sc = rx_SecurityObjectOf(tc);
321 si = rx_SecurityClassOf(tc);
324 * destroy old one after creating new one so that refCount on security
325 * object cannot reach zero.
327 newTc = rx_NewConnection(host, port, service, sc, si);
328 rx_DestroyConnection(tc);
332 #ifdef AFS_PTHREAD_ENV
334 pthread_once_t ubik_client_once = PTHREAD_ONCE_INIT;
335 pthread_mutex_t ubik_client_mutex;
336 #define LOCK_UCLNT_CACHE do { \
337 osi_Assert(pthread_once(&ubik_client_once, ubik_client_init_mutex) == 0); \
338 MUTEX_ENTER(&ubik_client_mutex); \
340 #define UNLOCK_UCLNT_CACHE MUTEX_EXIT(&ubik_client_mutex)
343 ubik_client_init_mutex(void)
345 MUTEX_INIT(&ubik_client_mutex, "client init", MUTEX_DEFAULT, 0);
350 #define LOCK_UCLNT_CACHE
351 #define UNLOCK_UCLNT_CACHE
356 static int *calls_needsync[SYNCCOUNT]; /* proc calls that need the sync site */
357 static int synccount = 0;
362 * \brief Call this after getting back a #UNOTSYNC.
364 * \note Getting a #UNOTSYNC error code back does \b not guarantee
365 * that there is a sync site yet elected. However, if there is a sync
366 * site out there somewhere, and you're trying an operation that
367 * requires a sync site, ubik will return #UNOTSYNC, indicating the
368 * operation won't work until you find a sync site
371 try_GetSyncSite(struct ubik_client *aclient, afs_int32 apos)
376 afs_int32 thisHost, newHost;
377 struct rx_connection *tc;
380 origLevel = aclient->initializationState;
383 tc = aclient->conns[apos];
384 if (tc && rx_ConnError(tc)) {
385 aclient->conns[apos] = (tc = ubik_RefreshConn(tc));
391 /* now see if we can find the sync site host */
392 code = VOTE_GetSyncSite(tc, &newHost);
393 if (aclient->initializationState != origLevel) {
394 return -1; /* somebody did a ubik_ClientInit */
397 if (!code && newHost) {
398 newHost = htonl(newHost); /* convert back to network order */
401 * position count at the appropriate slot in the client
402 * structure and retry. If we can't find in slot, we'll just
403 * continue through the whole list
405 for (i = 0; i < MAXSERVERS; i++) {
406 rxp = rx_PeerOf(aclient->conns[i]);
407 thisHost = rx_HostOf(rxp);
410 } else if (thisHost == newHost) {
411 return i; /* we were told to use this one */
422 * \brief Create an internal version of ubik_CallIter that takes an additional
423 * parameter - to indicate whether the ubik client handle has already
427 CallIter(int (*aproc) (), struct ubik_client *aclient,
428 afs_int32 aflags, int *apos, long p1, long p2, long p3, long p4,
429 long p5, long p6, long p7, long p8, long p9, long p10, long p11,
430 long p12, long p13, long p14, long p15, long p16, int needlock)
433 struct rx_connection *tc;
437 LOCK_UBIK_CLIENT(aclient);
439 origLevel = aclient->initializationState;
443 while (*apos < MAXSERVERS) {
444 /* tc is the next conn to try */
445 tc = aclient->conns[*apos];
448 UNLOCK_UBIK_CLIENT(aclient);
453 if (rx_ConnError(tc)) {
454 tc = ubik_RefreshConn(tc);
455 aclient->conns[*apos] = tc;
458 if ((aflags & UPUBIKONLY) && (aclient->states[*apos] & CFLastFailed)) {
459 (*apos)++; /* try another one if this server is down */
461 break; /* this is the desired path */
464 if (*apos >= MAXSERVERS) {
466 UNLOCK_UBIK_CLIENT(aclient);
472 (*aproc) (tc, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13,
474 if (aclient->initializationState != origLevel) {
476 UNLOCK_UBIK_CLIENT(aclient);
478 return code; /* somebody did a ubik_ClientInit */
481 /* what should I do in case of UNOQUORUM ? */
483 aclient->states[*apos] |= CFLastFailed; /* network errors */
485 /* either misc ubik code, or misc application code or success. */
486 aclient->states[*apos] &= ~CFLastFailed; /* operation worked */
491 UNLOCK_UBIK_CLIENT(aclient);
497 * \brief This is part of an iterator. It doesn't handle finding sync sites.
500 ubik_CallIter(int (*aproc) (), struct ubik_client *aclient,
501 afs_int32 aflags, int *apos, long p1, long p2,
502 long p3, long p4, long p5, long p6, long p7,
503 long p8, long p9, long p10, long p11, long p12,
504 long p13, long p14, long p15, long p16)
506 return CallIter(aproc, aclient, aflags, apos, p1, p2, p3, p4, p5, p6, p7,
507 p8, p9, p10, p11, p12, p13, p14, p15, p16, NEED_LOCK);
511 * \brief Call this instead of stub and we'll guarantee to find a host that's up.
513 * \todo In the future, we should also put in a protocol to find the sync site.
516 ubik_Call_New(int (*aproc) (), struct ubik_client *aclient,
517 afs_int32 aflags, long p1, long p2, long p3, long p4, long p5,
518 long p6, long p7, long p8, long p9, long p10, long p11,
519 long p12, long p13, long p14, long p15, long p16)
521 afs_int32 code, rcode;
528 LOCK_UBIK_CLIENT(aclient);
531 origLevel = aclient->initializationState;
533 /* Do two passes. First pass only checks servers known running */
534 for (aflags |= UPUBIKONLY, pass = 0; pass < 2;
535 pass++, aflags &= ~UPUBIKONLY) {
540 CallIter(aproc, aclient, aflags, &count, p1, p2, p3, p4, p5,
541 p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16,
543 if (code && (aclient->initializationState != origLevel)) {
546 if (code == UNOSERVERS) {
549 rcode = code; /* remember code from last good call */
551 if (code == UNOTSYNC) { /* means this requires a sync site */
552 if (aclient->conns[3]) { /* don't bother unless 4 or more srv */
553 temp = try_GetSyncSite(aclient, count);
554 if (aclient->initializationState != origLevel) {
555 goto restart; /* somebody did a ubik_ClientInit */
557 if ((temp >= 0) && ((temp > count) || (stepBack++ <= 2))) {
558 count = temp; /* generally try to make progress */
561 } else if ((code >= 0) && (code != UNOQUORUM)) {
562 UNLOCK_UBIK_CLIENT(aclient);
563 return code; /* success or global error condition */
567 UNLOCK_UBIK_CLIENT(aclient);
572 * call this instead of stub and we'll guarantee to find a host that's up.
574 * \todo In the future, we should also put in a protocol to find the sync site.
577 ubik_Call(int (*aproc) (), struct ubik_client *aclient,
578 afs_int32 aflags, long p1, long p2, long p3, long p4,
579 long p5, long p6, long p7, long p8, long p9, long p10,
580 long p11, long p12, long p13, long p14, long p15, long p16)
582 afs_int32 rcode, code, newHost, thisHost, i, count;
583 int chaseCount, pass, needsync, inlist, j;
584 struct rx_connection *tc;
588 if (aflags & UBIK_CALL_NEW)
589 return ubik_Call_New(aproc, aclient, aflags, p1, p2, p3, p4,
590 p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15,
595 LOCK_UBIK_CLIENT(aclient);
598 origLevel = aclient->initializationState;
600 chaseCount = inlist = needsync = 0;
603 for (j = 0; ((j < SYNCCOUNT) && calls_needsync[j]); j++) {
604 if (calls_needsync[j] == (int *)aproc) {
605 inlist = needsync = 1;
611 * First pass, we try all servers that are up.
612 * Second pass, we try all servers.
614 for (pass = 0; pass < 2; pass++) { /*p */
615 /* For each entry in our servers list */
616 for (count = 0;; count++) { /*s */
619 /* Need a sync site. Lets try to quickly find it */
620 if (aclient->syncSite) {
621 newHost = aclient->syncSite; /* already in network order */
622 aclient->syncSite = 0; /* Will reset if it works */
623 } else if (aclient->conns[3]) {
624 /* If there are fewer than four db servers in a cell,
625 * there's no point in making the GetSyncSite call.
626 * At best, it's a wash. At worst, it results in more
627 * RPCs than you would otherwise make.
629 tc = aclient->conns[count];
630 if (tc && rx_ConnError(tc)) {
631 aclient->conns[count] = tc = ubik_RefreshConn(tc);
635 code = VOTE_GetSyncSite(tc, &newHost);
636 if (aclient->initializationState != origLevel)
637 goto restart; /* somebody did a ubik_ClientInit */
640 newHost = htonl(newHost); /* convert to network order */
645 /* position count at the appropriate slot in the client
646 * structure and retry. If we can't find in slot, we'll
647 * just continue through the whole list
649 for (i = 0; i < MAXSERVERS && aclient->conns[i]; i++) {
650 rxp = rx_PeerOf(aclient->conns[i]);
651 thisHost = rx_HostOf(rxp);
654 if (thisHost == newHost) {
655 if (chaseCount++ > 2)
656 break; /* avoid loop asking */
657 count = i; /* this index is the sync site */
664 tc = aclient->conns[count];
665 if (tc && rx_ConnError(tc)) {
666 aclient->conns[count] = tc = ubik_RefreshConn(tc);
671 if ((pass == 0) && (aclient->states[count] & CFLastFailed)) {
672 continue; /* this guy's down */
676 (*aproc) (tc, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11,
677 p12, p13, p14, p15, p16);
678 if (aclient->initializationState != origLevel) {
679 /* somebody did a ubik_ClientInit */
681 goto restart; /* call failed */
683 goto done; /* call suceeded */
685 if (rcode < 0) { /* network errors */
686 aclient->states[count] |= CFLastFailed; /* Mark serer down */
687 } else if (rcode == UNOTSYNC) {
689 } else if (rcode != UNOQUORUM) {
690 /* either misc ubik code, or misc appl code, or success. */
691 aclient->states[count] &= ~CFLastFailed; /* mark server up */
692 goto done; /* all done */
699 if (!inlist) { /* Remember proc call that needs sync site */
701 calls_needsync[synccount % SYNCCOUNT] = (int *)aproc;
706 if (!rcode) { /* Remember the sync site - cmd successful */
707 rxp = rx_PeerOf(aclient->conns[count]);
708 aclient->syncSite = rx_HostOf(rxp);
711 UNLOCK_UBIK_CLIENT(aclient);