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;
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);
156 afs_osi_Free(tcv, sizeof(struct sa_conn_vector));
157 break; /* at most one instance per server */
158 } /*Found unreferenced connection for user */
160 } /*For each connection on the server */
162 } /* release_conns_user_server */
166 release_conns_vector(struct sa_conn_vector *xcv)
170 struct sa_conn_vector *tcv = NULL;
171 struct sa_conn_vector **lcv = NULL;
172 for (tcv = xcv; tcv; lcv = &tcv->next, tcv = *lcv) {
174 /* you know it, you love it, the GLOCK */
175 glocked = ISAFS_GLOCK();
178 for(cix = 0; cix < CVEC_LEN; ++cix) {
179 tc = &(tcv->cvec[cix]);
181 rx_SetConnSecondsUntilNatPing(tc->id, 0);
182 rx_DestroyConnection(tc->id);
187 afs_osi_Free(tcv, sizeof(struct sa_conn_vector));
190 } /* release_conns_vector */
193 unsigned int VNOSERVERS = 0;
196 * Pick a security object to use for a connection to a given server,
200 * The AFS connection for which the security object is required
201 * @param[out] secLevel
202 * The security level of the returned object
205 * An rx security object. This function is guaranteed to return
206 * an object, although that object may be rxnull (with a secLevel
209 static struct rx_securityClass *
210 afs_pickSecurityObject(struct afs_conn *conn, int *secLevel)
212 struct rx_securityClass *secObj = NULL;
213 union tokenUnion *token;
215 /* Do we have tokens ? */
216 if (conn->parent->user->states & UHasTokens) {
217 token = afs_FindToken(conn->parent->user->tokens, RX_SECIDX_KAD);
219 *secLevel = RX_SECIDX_KAD;
220 /* kerberos tickets on channel 2 */
221 secObj = rxkad_NewClientSecurityObject(
222 cryptall ? rxkad_crypt : rxkad_clear,
223 (struct ktc_encryptionKey *)
224 token->rxkad.clearToken.HandShakeKey,
225 token->rxkad.clearToken.AuthHandle,
226 token->rxkad.ticketLen, token->rxkad.ticket);
227 /* We're going to use this token, so populate the viced */
228 conn->parent->user->viceId = token->rxkad.clearToken.ViceId;
231 if (secObj == NULL) {
233 secObj = rxnull_NewClientSecurityObject();
241 * Try setting up a connection to the server containing the specified fid.
242 * Gets the volume, checks if it's up and does the connection by server address.
245 * @param areq Request filled in by the caller.
246 * @param locktype Type of lock that will be used.
248 * @return The conn struct, or NULL.
251 afs_Conn(struct VenusFid *afid, struct vrequest *areq,
252 afs_int32 locktype, struct rx_connection **rxconn)
254 u_short fsport = AFS_FSPORT;
256 struct afs_conn *tconn = NULL;
257 struct srvAddr *lowp = NULL;
261 struct srvAddr *sa1p;
262 afs_int32 replicated = -1; /* a single RO will increment to 0 */
266 AFS_STATCNT(afs_Conn);
267 /* Get fid's volume. */
268 tv = afs_GetVolume(afid, areq, READ_LOCK);
271 afs_FinalizeReq(areq);
272 areq->volumeError = 1;
277 if (tv->serverHost[0] && tv->serverHost[0]->cell) {
278 fsport = tv->serverHost[0]->cell->fsport;
283 /* First is always lowest rank, if it's up */
284 if ((tv->status[0] == not_busy) && tv->serverHost[0]
285 && !(tv->serverHost[0]->addr->sa_flags & SRVR_ISDOWN) &&
286 !(((areq->idleError > 0) || (areq->tokenError > 0))
287 && (areq->skipserver[0] == 1)))
288 lowp = tv->serverHost[0]->addr;
290 /* Otherwise we look at all of them. There are seven levels of
291 * not_busy. This means we will check a volume seven times before it
292 * is marked offline. Ideally, we only need two levels, but this
293 * serves a second purpose of waiting some number of seconds before
294 * the client decides the volume is offline (ie: a clone could finish
297 for (notbusy = not_busy; (!lowp && (notbusy <= end_not_busy)); notbusy++) {
298 for (i = 0; i < AFS_MAXHOSTS && tv->serverHost[i]; i++) {
299 if (tv->states & VRO)
301 if (((areq->tokenError > 0)||(areq->idleError > 0))
302 && (areq->skipserver[i] == 1))
304 if (tv->status[i] != notbusy) {
305 if (tv->status[i] == rd_busy || tv->status[i] == rdwr_busy) {
306 if (!areq->busyCount)
308 } else if (tv->status[i] == offline) {
309 if (!areq->volumeError)
310 areq->volumeError = VOLMISSING;
314 for (sa1p = tv->serverHost[i]->addr; sa1p; sa1p = sa1p->next_sa) {
315 if (sa1p->sa_flags & SRVR_ISDOWN)
317 if (!lowp || (lowp->sa_iprank > sa1p->sa_iprank))
322 if ((replicated == -1) && (tv->states & VRO)) {
323 for (i = 0; i < AFS_MAXHOSTS && tv->serverHost[i]; i++) {
324 if (tv->states & VRO)
330 afs_PutVolume(tv, READ_LOCK);
333 tu = afs_GetUser(areq->uid, afid->Cell, SHARED_LOCK);
334 tconn = afs_ConnBySA(lowp, fsport, afid->Cell, tu, 0 /*!force */ ,
335 1 /*create */ , locktype, replicated, rxconn);
337 afs_PutUser(tu, SHARED_LOCK);
345 * Connects to a server by it's server address.
347 * @param sap Server address.
348 * @param aport Server port.
350 * @param tu Connect as this user.
351 * @param force_if_down
354 * @param locktype Specifies type of lock to be used for this function.
356 * @return The new connection.
359 afs_ConnBySA(struct srvAddr *sap, unsigned short aport, afs_int32 acell,
360 struct unixuser *tu, int force_if_down, afs_int32 create,
361 afs_int32 locktype, afs_int32 replicated,
362 struct rx_connection **rxconn)
364 int glocked, foundvec;
365 struct afs_conn *tc = NULL;
366 struct sa_conn_vector *tcv = NULL;
367 struct rx_securityClass *csec; /*Security class object */
368 int isec; /*Security index */
370 int isrep = (replicated > 0)?CONN_REPLICATED:0;
374 /* find cached connection */
375 ObtainSharedLock(&afs_xconn, 15);
377 for (tcv = sap->conns; tcv; tcv = tcv->next) {
378 if (tcv->user == tu && tcv->port == aport &&
379 (isrep == (tcv->flags & CONN_REPLICATED))) {
380 /* return most eligible conn */
383 UpgradeSToWLock(&afs_xconn, 37);
384 tc = find_preferred_connection(tcv, create);
385 ConvertWToSLock(&afs_xconn);
390 if (!tc && !create) {
391 /* Not found and can't create a new one. */
392 ReleaseSharedLock(&afs_xconn);
396 if (AFS_IS_DISCONNECTED && !AFS_IN_SYNC) {
397 afs_warnuser("afs_ConnBySA: disconnected\n");
398 ReleaseSharedLock(&afs_xconn);
402 if (!foundvec && create) {
403 /* No such connection vector exists. Create one and splice it in.
404 * Make sure the server record has been marked as used (for the purposes
405 * of calculating up & down times, it's now considered to be an
406 * ``active'' server). Also make sure the server's lastUpdateEvalTime
407 * gets set, marking the time of its ``birth''.
409 UpgradeSToWLock(&afs_xconn, 37);
410 new_conn_vector(tcv);
415 tcv->next = sap->conns;
417 tcv->flags |= CONN_REPLICATED;
420 /* all struct afs_conn ptrs come from here */
421 tc = find_preferred_connection(tcv, create);
423 afs_ActivateServer(sap);
425 ConvertWToSLock(&afs_xconn);
426 } /* end of if (!tcv) */
429 /* Not found and no alternatives. */
430 ReleaseSharedLock(&afs_xconn);
434 if (tu->states & UTokensBad) {
435 /* we may still have an authenticated RPC connection here,
436 * we'll have to create a new, unauthenticated, connection.
437 * Perhaps a better way to do this would be to set
438 * conn->forceConnectFS on all conns when the token first goes
439 * bad, but that's somewhat trickier, due to locking
440 * constraints (though not impossible).
442 if (tc->id && (rx_SecurityClassOf(tc->id) != 0)) {
443 tc->forceConnectFS = 1; /* force recreation of connection */
445 tu->states &= ~UHasTokens; /* remove the authentication info */
448 glocked = ISAFS_GLOCK();
449 if (tc->forceConnectFS) {
450 UpgradeSToWLock(&afs_xconn, 38);
451 csec = (struct rx_securityClass *)0;
455 rx_SetConnSecondsUntilNatPing(tc->id, 0);
456 rx_DestroyConnection(tc->id);
461 * Stupid hack to determine if using vldb service or file system
464 if (aport == sap->server->cell->vlport)
470 csec = afs_pickSecurityObject(tc, &isec);
474 tc->id = rx_NewConnection(sap->sa_ip, aport, service, csec, isec);
478 rx_SetConnHardDeadTime(tc->id, afs_rx_harddead);
481 /* Setting idle dead time to non-zero activates RX_CALL_IDLE errors. */
483 rx_SetConnIdleDeadTime(tc->id, afs_rx_idledead_rep);
485 rx_SetConnIdleDeadTime(tc->id, afs_rx_idledead);
488 * Only do this for the base connection, not per-user.
489 * Will need to be revisited if/when CB gets security.
491 if ((isec == 0) && (service != 52) && !(tu->states & UTokensBad) &&
492 (tu->viceId == UNDEFVID) && (isrep == 0)
493 #ifndef UKERNEL /* ukernel runs as just one uid anyway */
497 rx_SetConnSecondsUntilNatPing(tc->id, 20);
499 tc->forceConnectFS = 0; /* apparently we're appropriately connected now */
502 ConvertWToSLock(&afs_xconn);
503 } /* end of if (tc->forceConnectFS)*/
506 rx_GetConnection(*rxconn);
508 ReleaseSharedLock(&afs_xconn);
513 * forceConnectFS is set whenever we must recompute the connection. UTokensBad
514 * is true only if we know that the tokens are bad. We thus clear this flag
515 * when we get a new set of tokens..
516 * Having force... true and UTokensBad true simultaneously means that the tokens
517 * went bad and we're supposed to create a new, unauthenticated, connection.
519 * @param aserver Server to connect to.
520 * @param aport Connection port.
521 * @param acell The cell where all of this happens.
522 * @param areq The request.
523 * @param aforce Force connection?
524 * @param locktype Type of lock to be used.
527 * @return The established connection.
530 afs_ConnByHost(struct server *aserver, unsigned short aport, afs_int32 acell,
531 struct vrequest *areq, int aforce, afs_int32 locktype,
532 afs_int32 replicated, struct rx_connection **rxconn)
535 struct afs_conn *tc = NULL;
536 struct srvAddr *sa = NULL;
540 AFS_STATCNT(afs_ConnByHost);
542 if (AFS_IS_DISCONNECTED && !AFS_IN_SYNC) {
543 afs_warnuser("afs_ConnByHost: disconnected\n");
548 1. look for an existing connection
549 2. create a connection at an address believed to be up
550 (if aforce is true, create a connection at the first address)
553 tu = afs_GetUser(areq->uid, acell, SHARED_LOCK);
555 for (sa = aserver->addr; sa; sa = sa->next_sa) {
556 tc = afs_ConnBySA(sa, aport, acell, tu, aforce,
557 0 /*don't create one */ ,
558 locktype, replicated, rxconn);
564 for (sa = aserver->addr; sa; sa = sa->next_sa) {
565 tc = afs_ConnBySA(sa, aport, acell, tu, aforce,
567 locktype, replicated, rxconn);
573 afs_PutUser(tu, SHARED_LOCK);
576 } /*afs_ConnByHost */
580 * Connect by multiple hosts.
581 * Try to connect to one of the hosts from the ahosts array.
583 * @param ahosts Multiple hosts to connect to.
584 * @param aport Connection port.
585 * @param acell The cell where all of this happens.
586 * @param areq The request.
587 * @param locktype Type of lock to be used.
590 * @return The established connection or NULL.
593 afs_ConnByMHosts(struct server *ahosts[], unsigned short aport,
594 afs_int32 acell, struct vrequest *areq,
595 afs_int32 locktype, afs_int32 replicated,
596 struct rx_connection **rxconn)
599 struct afs_conn *tconn;
604 /* try to find any connection from the set */
605 AFS_STATCNT(afs_ConnByMHosts);
606 for (i = 0; i < AFS_MAXCELLHOSTS; i++) {
607 if ((ts = ahosts[i]) == NULL)
609 tconn = afs_ConnByHost(ts, aport, acell, areq, 0, locktype,
617 } /*afs_ConnByMHosts */
621 * Decrement reference count to this connection.
626 afs_PutConn(struct afs_conn *ac, struct rx_connection *rxconn,
629 AFS_STATCNT(afs_PutConn);
631 if (ac->refCount < 0) {
632 osi_Panic("afs_PutConn: refcount imbalance 0x%lx %d",
633 (unsigned long)(uintptrsz)ac, (int)ac->refCount);
635 ac->parent->refCount--;
636 rx_PutConnection(rxconn);
641 * Free up a connection vector, allowing, eg, code in afs_user.c
642 * to ignore how connections are stored/pooled
646 afs_ReleaseConns(struct sa_conn_vector *tcv) {
647 release_conns_vector(tcv);
652 * Free connection vector(s) for a user
656 afs_ReleaseConnsUser(struct unixuser *au) {
661 for (i = 0; i < NSERVERS; i++) {
662 for (ts = afs_servers[i]; ts; ts = ts->next) {
663 release_conns_user_server(au, ts);
664 } /*For each server on chain */
665 } /*For each chain */
670 * For multi homed clients, a RPC may timeout because of a
671 * client network interface going down. We need to reopen new
672 * connections in this case.
674 * @param sap Server address.
677 ForceNewConnections(struct srvAddr *sap)
680 struct afs_conn *tc = NULL;
681 struct sa_conn_vector *tcv = NULL;
684 return; /* defensive check */
686 ObtainWriteLock(&afs_xconn, 413);
687 for (tcv = sap->conns; tcv; tcv = tcv->next) {
688 for(cix = 0; cix < CVEC_LEN; ++cix) {
689 tc = &(tcv->cvec[cix]);
691 tc->forceConnectFS = 1;
694 ReleaseWriteLock(&afs_xconn);