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
13 #include <afsconfig.h>
14 #include "afs/param.h"
18 #include "afs/sysincludes.h" /* Standard vendor system headers */
21 #if !defined(AFS_LINUX20_ENV)
24 #include <netinet/in.h>
27 #include "h/hashing.h"
29 #if !defined(AFS_HPUX110_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_DARWIN_ENV)
30 #include <netinet/in_var.h>
31 #endif /* ! AFS_HPUX110_ENV */
32 #endif /* !defined(UKERNEL) */
34 #include "afsincludes.h" /* Afs-based standard headers */
35 #include "afs/afs_stats.h" /* afs statistics */
37 #if defined(AFS_SUN5_ENV)
39 #include <inet/common.h>
40 #include <netinet/ip6.h>
44 /* Exported variables */
45 afs_rwlock_t afs_xconn; /* allocation lock for new things */
46 afs_rwlock_t afs_xinterface; /* for multiple client address */
47 afs_int32 cryptall = 0; /* encrypt all communications */
49 /* some connection macros */
52 #define new_conn_vector(xcv) \
54 xcv = (struct sa_conn_vector *) \
55 afs_osi_Alloc(sizeof(struct sa_conn_vector)); \
57 memset((char *)xcv, 0, sizeof(struct sa_conn_vector)); \
61 /* select a connection to return (if no connection has lower utilization
63 #define conn_vec_select_conn(xcv, bix, conn) \
65 (bix) = ((xcv)->select_index)++ % CVEC_LEN; \
66 (conn) = &((xcv)->cvec[bix]); \
69 #define struct_conn(s) ((struct afs_conn *)(s))
71 #define REPORT_CONNECTIONS_ISSUED 0 /* enable to see utilization */
74 * Find a connection with call slots available, allocating one
75 * if nothing is available and we find an allocated slot
76 * @param xcv A connection vector
77 * @param create If set, a new connection may be created
79 static struct afs_conn *
80 find_preferred_connection(struct sa_conn_vector *xcv, int create)
83 struct afs_conn *tc = NULL;
86 for(cix = 0; cix < CVEC_LEN; ++cix) {
87 tc = &(xcv->cvec[cix]);
91 tc->forceConnectFS = 1;
97 if (tc->refCount < (RX_MAXCALLS-1)) {
100 } else if (cix == (CVEC_LEN-1))
101 conn_vec_select_conn(xcv, bix, tc);
103 } /* for cix < CVEC_LEN */
106 afs_warn("find_preferred_connection: no connection and !create\n");
115 #if REPORT_CONNECTIONS_ISSUED
116 afs_warn("Issuing conn %d refCount=%d parent refCount=%d\n", bix,
117 tc->refCount, xcv->refCount);
123 } /* find_preferred_connection */
127 * Release all connections for unix user xu at server xs
132 release_conns_user_server(struct unixuser *xu, struct server *xs)
137 struct sa_conn_vector *tcv, **lcv, *tcvn;
138 for (sa = (xs)->addr; sa; sa = sa->next_sa) {
140 for (tcv = *lcv; tcv; lcv = &tcv->next, tcv = *lcv) {
141 if (tcv->user == (xu) && tcv->refCount == 0) {
143 /* our old friend, the GLOCK */
144 glocked = ISAFS_GLOCK();
147 for(cix = 0; cix < CVEC_LEN; ++cix) {
148 tc = &(tcv->cvec[cix]);
150 rx_SetConnSecondsUntilNatPing(tc->id, 0);
151 rx_DestroyConnection(tc->id);
152 /* find another eligible connection */
153 if (sa->natping == tc) {
155 struct afs_conn *tcn;
156 for (tcvn = sa->conns; tcvn; tcvn = tcvn->next) {
159 for(cin = 0; cin < CVEC_LEN; ++cin) {
160 tcn = &(tcvn->cvec[cin]);
161 if (tcn->activated) {
162 rx_SetConnSecondsUntilNatPing(tcn->id, 20);
173 afs_osi_Free(tcv, sizeof(struct sa_conn_vector));
174 break; /* at most one instance per server */
175 } /*Found unreferenced connection for user */
177 } /*For each connection on the server */
179 } /* release_conns_user_server */
183 release_conns_vector(struct sa_conn_vector *xcv)
187 struct sa_conn_vector *tcv = NULL;
188 struct sa_conn_vector **lcv = NULL;
189 for (tcv = xcv; tcv; lcv = &tcv->next, tcv = *lcv) {
191 /* you know it, you love it, the GLOCK */
192 glocked = ISAFS_GLOCK();
195 for(cix = 0; cix < CVEC_LEN; ++cix) {
196 tc = &(tcv->cvec[cix]);
198 rx_SetConnSecondsUntilNatPing(tc->id, 0);
199 rx_DestroyConnection(tc->id);
200 if (tcv->srvr->natping == tc)
201 tcv->srvr->natping = NULL;
206 afs_osi_Free(tcv, sizeof(struct sa_conn_vector));
209 } /* release_conns_vector */
212 unsigned int VNOSERVERS = 0;
215 * Pick a security object to use for a connection to a given server,
219 * The AFS connection for which the security object is required
220 * @param[out] secLevel
221 * The security level of the returned object
224 * An rx security object. This function is guaranteed to return
225 * an object, although that object may be rxnull (with a secLevel
228 static struct rx_securityClass *
229 afs_pickSecurityObject(struct afs_conn *conn, int *secLevel)
231 struct rx_securityClass *secObj = NULL;
232 union tokenUnion *token;
234 /* Do we have tokens ? */
235 if (conn->parent->user->states & UHasTokens) {
236 token = afs_FindToken(conn->parent->user->tokens, RX_SECIDX_KAD);
238 *secLevel = RX_SECIDX_KAD;
239 /* kerberos tickets on channel 2 */
240 secObj = rxkad_NewClientSecurityObject(
241 cryptall ? rxkad_crypt : rxkad_clear,
242 (struct ktc_encryptionKey *)
243 token->rxkad.clearToken.HandShakeKey,
244 token->rxkad.clearToken.AuthHandle,
245 token->rxkad.ticketLen, token->rxkad.ticket);
246 /* We're going to use this token, so populate the viced */
247 conn->parent->user->viceId = token->rxkad.clearToken.ViceId;
250 if (secObj == NULL) {
252 secObj = rxnull_NewClientSecurityObject();
260 * Try setting up a connection to the server containing the specified fid.
261 * Gets the volume, checks if it's up and does the connection by server address.
264 * @param areq Request filled in by the caller.
265 * @param locktype Type of lock that will be used.
267 * @return The conn struct, or NULL.
270 afs_Conn(struct VenusFid *afid, struct vrequest *areq,
271 afs_int32 locktype, struct rx_connection **rxconn)
273 u_short fsport = AFS_FSPORT;
275 struct afs_conn *tconn = NULL;
276 struct srvAddr *lowp = NULL;
280 struct srvAddr *sa1p;
281 afs_int32 replicated = -1; /* a single RO will increment to 0 */
285 AFS_STATCNT(afs_Conn);
286 /* Get fid's volume. */
287 tv = afs_GetVolume(afid, areq, READ_LOCK);
290 afs_FinalizeReq(areq);
291 areq->volumeError = 1;
296 if (tv->serverHost[0] && tv->serverHost[0]->cell) {
297 fsport = tv->serverHost[0]->cell->fsport;
302 /* First is always lowest rank, if it's up */
303 if ((tv->status[0] == not_busy) && tv->serverHost[0]
304 && tv->serverHost[0]->addr
305 && !(tv->serverHost[0]->addr->sa_flags & SRVR_ISDOWN) &&
306 !(((areq->idleError > 0) || (areq->tokenError > 0))
307 && (areq->skipserver[0] == 1)))
308 lowp = tv->serverHost[0]->addr;
310 /* Otherwise we look at all of them. There are seven levels of
311 * not_busy. This means we will check a volume seven times before it
312 * is marked offline. Ideally, we only need two levels, but this
313 * serves a second purpose of waiting some number of seconds before
314 * the client decides the volume is offline (ie: a clone could finish
317 for (notbusy = not_busy; (!lowp && (notbusy <= end_not_busy)); notbusy++) {
318 for (i = 0; i < AFS_MAXHOSTS && tv->serverHost[i]; i++) {
319 if (tv->states & VRO)
321 if (((areq->tokenError > 0)||(areq->idleError > 0))
322 && (areq->skipserver[i] == 1))
324 if (tv->status[i] != notbusy) {
325 if (tv->status[i] == rd_busy || tv->status[i] == rdwr_busy) {
326 if (!areq->busyCount)
328 } else if (tv->status[i] == offline) {
329 if (!areq->volumeError)
330 areq->volumeError = VOLMISSING;
334 for (sa1p = tv->serverHost[i]->addr; sa1p; sa1p = sa1p->next_sa) {
335 if (sa1p->sa_flags & SRVR_ISDOWN)
337 if (!lowp || (lowp->sa_iprank > sa1p->sa_iprank))
342 if ((replicated == -1) && (tv->states & VRO)) {
343 for (i = 0; i < AFS_MAXHOSTS && tv->serverHost[i]; i++) {
344 if (tv->states & VRO)
350 afs_PutVolume(tv, READ_LOCK);
353 tu = afs_GetUser(areq->uid, afid->Cell, SHARED_LOCK);
354 tconn = afs_ConnBySA(lowp, fsport, afid->Cell, tu, 0 /*!force */ ,
355 1 /*create */ , locktype, replicated, rxconn);
357 afs_PutUser(tu, SHARED_LOCK);
365 * Connects to a server by it's server address.
367 * @param sap Server address.
368 * @param aport Server port.
370 * @param tu Connect as this user.
371 * @param force_if_down
374 * @param locktype Specifies type of lock to be used for this function.
376 * @return The new connection.
379 afs_ConnBySA(struct srvAddr *sap, unsigned short aport, afs_int32 acell,
380 struct unixuser *tu, int force_if_down, afs_int32 create,
381 afs_int32 locktype, afs_int32 replicated,
382 struct rx_connection **rxconn)
384 int glocked, foundvec;
385 struct afs_conn *tc = NULL;
386 struct sa_conn_vector *tcv = NULL;
387 struct rx_securityClass *csec; /*Security class object */
388 int isec; /*Security index */
390 int isrep = (replicated > 0)?CONN_REPLICATED:0;
394 /* find cached connection */
395 ObtainSharedLock(&afs_xconn, 15);
397 for (tcv = sap->conns; tcv; tcv = tcv->next) {
398 if (tcv->user == tu && tcv->port == aport &&
399 (isrep == (tcv->flags & CONN_REPLICATED))) {
400 /* return most eligible conn */
403 UpgradeSToWLock(&afs_xconn, 37);
404 tc = find_preferred_connection(tcv, create);
405 ConvertWToSLock(&afs_xconn);
410 if (!tc && !create) {
411 /* Not found and can't create a new one. */
412 ReleaseSharedLock(&afs_xconn);
416 if (AFS_IS_DISCONNECTED && !AFS_IN_SYNC) {
417 afs_warnuser("afs_ConnBySA: disconnected\n");
418 ReleaseSharedLock(&afs_xconn);
422 if (!foundvec && create) {
423 /* No such connection vector exists. Create one and splice it in.
424 * Make sure the server record has been marked as used (for the purposes
425 * of calculating up & down times, it's now considered to be an
426 * ``active'' server). Also make sure the server's lastUpdateEvalTime
427 * gets set, marking the time of its ``birth''.
429 UpgradeSToWLock(&afs_xconn, 37);
430 new_conn_vector(tcv);
435 tcv->next = sap->conns;
437 tcv->flags |= CONN_REPLICATED;
440 /* all struct afs_conn ptrs come from here */
441 tc = find_preferred_connection(tcv, create);
443 afs_ActivateServer(sap);
445 ConvertWToSLock(&afs_xconn);
446 } /* end of if (!tcv) */
449 /* Not found and no alternatives. */
450 ReleaseSharedLock(&afs_xconn);
454 if (tu->states & UTokensBad) {
455 /* we may still have an authenticated RPC connection here,
456 * we'll have to create a new, unauthenticated, connection.
457 * Perhaps a better way to do this would be to set
458 * conn->forceConnectFS on all conns when the token first goes
459 * bad, but that's somewhat trickier, due to locking
460 * constraints (though not impossible).
462 if (tc->id && (rx_SecurityClassOf(tc->id) != 0)) {
463 tc->forceConnectFS = 1; /* force recreation of connection */
465 tu->states &= ~UHasTokens; /* remove the authentication info */
468 glocked = ISAFS_GLOCK();
469 if (tc->forceConnectFS) {
470 UpgradeSToWLock(&afs_xconn, 38);
471 csec = (struct rx_securityClass *)0;
473 if (sap->natping == tc)
477 rx_SetConnSecondsUntilNatPing(tc->id, 0);
478 rx_DestroyConnection(tc->id);
483 * Stupid hack to determine if using vldb service or file system
486 if (aport == sap->server->cell->vlport)
492 csec = afs_pickSecurityObject(tc, &isec);
496 tc->id = rx_NewConnection(sap->sa_ip, aport, service, csec, isec);
500 rx_SetConnHardDeadTime(tc->id, afs_rx_harddead);
503 /* Setting idle dead time to non-zero activates RX_CALL_IDLE errors. */
505 rx_SetConnIdleDeadTime(tc->id, afs_rx_idledead_rep);
507 rx_SetConnIdleDeadTime(tc->id, afs_rx_idledead);
510 * Only do this for one connection
512 if ((service != 52) && (sap->natping == NULL)) {
514 rx_SetConnSecondsUntilNatPing(tc->id, 20);
517 tc->forceConnectFS = 0; /* apparently we're appropriately connected now */
520 ConvertWToSLock(&afs_xconn);
521 } /* end of if (tc->forceConnectFS)*/
524 rx_GetConnection(*rxconn);
526 ReleaseSharedLock(&afs_xconn);
531 * forceConnectFS is set whenever we must recompute the connection. UTokensBad
532 * is true only if we know that the tokens are bad. We thus clear this flag
533 * when we get a new set of tokens..
534 * Having force... true and UTokensBad true simultaneously means that the tokens
535 * went bad and we're supposed to create a new, unauthenticated, connection.
537 * @param aserver Server to connect to.
538 * @param aport Connection port.
539 * @param acell The cell where all of this happens.
540 * @param areq The request.
541 * @param aforce Force connection?
542 * @param locktype Type of lock to be used.
545 * @return The established connection.
548 afs_ConnByHost(struct server *aserver, unsigned short aport, afs_int32 acell,
549 struct vrequest *areq, int aforce, afs_int32 locktype,
550 afs_int32 replicated, struct rx_connection **rxconn)
553 struct afs_conn *tc = NULL;
554 struct srvAddr *sa = NULL;
558 AFS_STATCNT(afs_ConnByHost);
560 if (AFS_IS_DISCONNECTED && !AFS_IN_SYNC) {
561 afs_warnuser("afs_ConnByHost: disconnected\n");
566 1. look for an existing connection
567 2. create a connection at an address believed to be up
568 (if aforce is true, create a connection at the first address)
571 tu = afs_GetUser(areq->uid, acell, SHARED_LOCK);
573 for (sa = aserver->addr; sa; sa = sa->next_sa) {
574 tc = afs_ConnBySA(sa, aport, acell, tu, aforce,
575 0 /*don't create one */ ,
576 locktype, replicated, rxconn);
582 for (sa = aserver->addr; sa; sa = sa->next_sa) {
583 tc = afs_ConnBySA(sa, aport, acell, tu, aforce,
585 locktype, replicated, rxconn);
591 afs_PutUser(tu, SHARED_LOCK);
594 } /*afs_ConnByHost */
598 * Connect by multiple hosts.
599 * Try to connect to one of the hosts from the ahosts array.
601 * @param ahosts Multiple hosts to connect to.
602 * @param aport Connection port.
603 * @param acell The cell where all of this happens.
604 * @param areq The request.
605 * @param locktype Type of lock to be used.
608 * @return The established connection or NULL.
611 afs_ConnByMHosts(struct server *ahosts[], unsigned short aport,
612 afs_int32 acell, struct vrequest *areq,
613 afs_int32 locktype, afs_int32 replicated,
614 struct rx_connection **rxconn)
617 struct afs_conn *tconn;
622 /* try to find any connection from the set */
623 AFS_STATCNT(afs_ConnByMHosts);
624 for (i = 0; i < AFS_MAXCELLHOSTS; i++) {
625 if ((ts = ahosts[i]) == NULL)
627 tconn = afs_ConnByHost(ts, aport, acell, areq, 0, locktype,
635 } /*afs_ConnByMHosts */
639 * Decrement reference count to this connection.
644 afs_PutConn(struct afs_conn *ac, struct rx_connection *rxconn,
647 AFS_STATCNT(afs_PutConn);
649 if (ac->refCount < 0) {
650 osi_Panic("afs_PutConn: refcount imbalance 0x%lx %d",
651 (unsigned long)(uintptrsz)ac, (int)ac->refCount);
653 ac->parent->refCount--;
654 rx_PutConnection(rxconn);
659 * Free up a connection vector, allowing, eg, code in afs_user.c
660 * to ignore how connections are stored/pooled
664 afs_ReleaseConns(struct sa_conn_vector *tcv) {
665 release_conns_vector(tcv);
670 * Free connection vector(s) for a user
674 afs_ReleaseConnsUser(struct unixuser *au) {
679 for (i = 0; i < NSERVERS; i++) {
680 for (ts = afs_servers[i]; ts; ts = ts->next) {
681 release_conns_user_server(au, ts);
682 } /*For each server on chain */
683 } /*For each chain */
688 * For multi homed clients, a RPC may timeout because of a
689 * client network interface going down. We need to reopen new
690 * connections in this case.
692 * @param sap Server address.
695 ForceNewConnections(struct srvAddr *sap)
698 struct afs_conn *tc = NULL;
699 struct sa_conn_vector *tcv = NULL;
702 return; /* defensive check */
704 ObtainWriteLock(&afs_xconn, 413);
705 for (tcv = sap->conns; tcv; tcv = tcv->next) {
706 for(cix = 0; cix < CVEC_LEN; ++cix) {
707 tc = &(tcv->cvec[cix]);
709 tc->forceConnectFS = 1;
712 ReleaseWriteLock(&afs_xconn);