doxygen-comments-20081010
[openafs.git] / src / afs / afs_conn.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  */
9
10 /*
11  * Implements:
12  */
13 #include <afsconfig.h>
14 #include "afs/param.h"
15
16 RCSID
17     ("$Header$");
18
19 #include "afs/stds.h"
20 #include "afs/sysincludes.h"    /* Standard vendor system headers */
21
22 #if !defined(UKERNEL)
23 #if !defined(AFS_LINUX20_ENV)
24 #include <net/if.h>
25 #endif
26 #include <netinet/in.h>
27
28 #ifdef AFS_SGI62_ENV
29 #include "h/hashing.h"
30 #endif
31 #if !defined(AFS_HPUX110_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_DARWIN60_ENV)
32 #include <netinet/in_var.h>
33 #endif /* ! AFS_HPUX110_ENV */
34 #endif /* !defined(UKERNEL) */
35
36 #include "afsincludes.h"        /* Afs-based standard headers */
37 #include "afs/afs_stats.h"      /* afs statistics */
38
39 #if     defined(AFS_SUN56_ENV)
40 #include <inet/led.h>
41 #include <inet/common.h>
42 #if     defined(AFS_SUN58_ENV)
43 #include <netinet/ip6.h>
44 #endif
45 #include <inet/ip.h>
46 #endif
47
48 /* Exported variables */
49 afs_rwlock_t afs_xconn;         /* allocation lock for new things */
50 afs_rwlock_t afs_xinterface;    /* for multiple client address */
51 afs_int32 cryptall = 0;         /* encrypt all communications */
52
53
54 unsigned int VNOSERVERS = 0;
55
56 /**
57  * Try setting up a connection to the server containing the specified fid.
58  * Gets the volume, checks if it's up and does the connection by server address.
59  *
60  * @param afid 
61  * @param areq Request filled in by the caller.
62  * @param locktype Type of lock that will be used.
63  *
64  * @return The conn struct, or NULL.
65  */
66 struct conn *
67 afs_Conn(register struct VenusFid *afid, register struct vrequest *areq,
68          afs_int32 locktype)
69 {
70     u_short fsport = AFS_FSPORT;
71     struct volume *tv;
72     struct conn *tconn = NULL;
73     struct srvAddr *lowp = NULL;
74     struct unixuser *tu;
75     int notbusy;
76     int i;
77     struct srvAddr *sa1p;
78
79     AFS_STATCNT(afs_Conn);
80     /* Get fid's volume. */
81     tv = afs_GetVolume(afid, areq, READ_LOCK);
82     if (!tv) {
83         if (areq) {
84             afs_FinalizeReq(areq);
85             areq->volumeError = 1;
86         }
87         return NULL;
88     }
89
90     if (tv->serverHost[0] && tv->serverHost[0]->cell) {
91         fsport = tv->serverHost[0]->cell->fsport;
92     } else {
93         VNOSERVERS++;
94     }
95
96     /* First is always lowest rank, if it's up */
97     if ((tv->status[0] == not_busy) && tv->serverHost[0]
98         && !(tv->serverHost[0]->addr->sa_flags & SRVR_ISDOWN) &&
99         !(((areq->idleError > 0) || (areq->tokenError > 0))
100           && (areq->skipserver[0] == 1)))
101         lowp = tv->serverHost[0]->addr;
102
103     /* Otherwise we look at all of them. There are seven levels of
104      * not_busy. This means we will check a volume seven times before it
105      * is marked offline. Ideally, we only need two levels, but this
106      * serves a second purpose of waiting some number of seconds before
107      * the client decides the volume is offline (ie: a clone could finish
108      * in this time).
109      */
110     for (notbusy = not_busy; (!lowp && (notbusy <= end_not_busy)); notbusy++) {
111         for (i = 0; i < MAXHOSTS && tv->serverHost[i]; i++) {
112             if (((areq->tokenError > 0)||(areq->idleError > 0)) 
113                 && (areq->skipserver[i] == 1))
114                 continue;
115             if (tv->status[i] != notbusy) {
116                 if (tv->status[i] == rd_busy || tv->status[i] == rdwr_busy) {
117                     if (!areq->busyCount)
118                         areq->busyCount++;
119                 } else if (tv->status[i] == offline) {
120                     if (!areq->volumeError)
121                         areq->volumeError = VOLMISSING;
122                 }
123                 continue;
124             }
125             for (sa1p = tv->serverHost[i]->addr; sa1p; sa1p = sa1p->next_sa) {
126                 if (sa1p->sa_flags & SRVR_ISDOWN)
127                     continue;
128                 if (!lowp || (lowp->sa_iprank > sa1p->sa_iprank))
129                     lowp = sa1p;
130             }
131         }
132     }
133     afs_PutVolume(tv, READ_LOCK);
134
135     if (lowp) {
136         tu = afs_GetUser(areq->uid, afid->Cell, SHARED_LOCK);
137         tconn = afs_ConnBySA(lowp, fsport, afid->Cell, tu, 0 /*!force */ ,
138                              1 /*create */ , locktype);
139
140         afs_PutUser(tu, SHARED_LOCK);
141     }
142
143     return tconn;
144 }                               /*afs_Conn */
145
146
147 /**
148  * Connects to a server by it's server address.
149  *
150  * @param sap Server address.
151  * @param aport Server port.
152  * @param acell
153  * @param tu Connect as this user.
154  * @param force_if_down
155  * @param create
156  * @param locktype Specifies type of lock to be used for this function.
157  *
158  * @return The new connection.
159  */
160 struct conn *
161 afs_ConnBySA(struct srvAddr *sap, unsigned short aport, afs_int32 acell,
162              struct unixuser *tu, int force_if_down, afs_int32 create,
163              afs_int32 locktype)
164 {
165     struct conn *tc = 0;
166     struct rx_securityClass *csec;      /*Security class object */
167     int isec;                   /*Security index */
168     int service;
169
170     if (!sap || ((sap->sa_flags & SRVR_ISDOWN) && !force_if_down)) {
171         /* sa is known down, and we don't want to force it.  */
172         return NULL;
173     }
174
175     ObtainSharedLock(&afs_xconn, 15);
176     /* Get conn by port and user. */
177     for (tc = sap->conns; tc; tc = tc->next) {
178         if (tc->user == tu && tc->port == aport) {
179             break;
180         }
181     }
182
183     if (!tc && !create) {
184         /* Not found and can't create a new one. */
185         ReleaseSharedLock(&afs_xconn);
186         return NULL;
187     }
188     
189     if (AFS_IS_DISCONNECTED && !AFS_IN_SYNC) {
190         afs_warnuser("afs_ConnBySA: disconnected\n");
191         ReleaseSharedLock(&afs_xconn);
192         return NULL;
193     }
194
195     if (!tc) {
196         /* No such connection structure exists.  Create one and splice it in.
197          * Make sure the server record has been marked as used (for the purposes
198          * of calculating up & down times, it's now considered to be an
199          * ``active'' server).  Also make sure the server's lastUpdateEvalTime
200          * gets set, marking the time of its ``birth''.
201          */
202         UpgradeSToWLock(&afs_xconn, 37);
203         tc = (struct conn *)afs_osi_Alloc(sizeof(struct conn));
204         memset((char *)tc, 0, sizeof(struct conn));
205
206         tc->user = tu;
207         tc->port = aport;
208         tc->srvr = sap;
209         tc->refCount = 0;       /* bumped below */
210         tc->forceConnectFS = 1;
211         tc->id = (struct rx_connection *)0;
212         tc->next = sap->conns;
213         sap->conns = tc;
214         afs_ActivateServer(sap);
215
216         ConvertWToSLock(&afs_xconn);
217     } /* end of if (!tc) */
218     tc->refCount++;
219
220     if (tu->states & UTokensBad) {
221         /* we may still have an authenticated RPC connection here,
222          * we'll have to create a new, unauthenticated, connection.
223          * Perhaps a better way to do this would be to set
224          * conn->forceConnectFS on all conns when the token first goes
225          * bad, but that's somewhat trickier, due to locking
226          * constraints (though not impossible).
227          */
228         if (tc->id && (rx_SecurityClassOf(tc->id) != 0)) {
229             tc->forceConnectFS = 1;     /* force recreation of connection */
230         }
231         tu->vid = UNDEFVID;     /* forcibly disconnect the authentication info */
232     }
233
234     if (tc->forceConnectFS) {
235         UpgradeSToWLock(&afs_xconn, 38);
236         csec = (struct rx_securityClass *)0;
237         if (tc->id) {
238             AFS_GUNLOCK();
239             rx_DestroyConnection(tc->id);
240             AFS_GLOCK();
241         }
242         /*
243          * Stupid hack to determine if using vldb service or file system
244          * service.
245          */
246         if (aport == sap->server->cell->vlport)
247             service = 52;
248         else
249             service = 1;
250         isec = 0;
251         if (tu->vid != UNDEFVID) {
252             int level;
253
254             if (cryptall) {
255                 level = rxkad_crypt;
256             } else {
257                 level = rxkad_clear;
258             }
259             isec = 2;
260             /* kerberos tickets on channel 2 */
261             csec = rxkad_NewClientSecurityObject(level,
262                                                  (struct ktc_encryptionKey *)tu->ct.HandShakeKey,
263                                                  /* kvno */
264                                                  tu->ct.AuthHandle, tu->stLen,
265                                                  tu->stp);
266         }
267         if (isec == 0)
268             csec = rxnull_NewClientSecurityObject();
269         AFS_GUNLOCK();
270         tc->id = rx_NewConnection(sap->sa_ip, aport, service, csec, isec);
271         AFS_GLOCK();
272         if (service == 52) {
273             rx_SetConnHardDeadTime(tc->id, afs_rx_harddead);
274         }
275         rx_SetConnIdleDeadTime(tc->id, afs_rx_idledead);
276
277         tc->forceConnectFS = 0; /* apparently we're appropriately connected now */
278         if (csec)
279             rxs_Release(csec);
280         ConvertWToSLock(&afs_xconn);
281     } /* end of if (tc->forceConnectFS)*/
282
283     ReleaseSharedLock(&afs_xconn);
284     return tc;
285 }
286
287 /**
288  * forceConnectFS is set whenever we must recompute the connection. UTokensBad
289  * is true only if we know that the tokens are bad.  We thus clear this flag
290  * when we get a new set of tokens..
291  * Having force... true and UTokensBad true simultaneously means that the tokens
292  * went bad and we're supposed to create a new, unauthenticated, connection.
293  *
294  * @param aserver Server to connect to.
295  * @param aport Connection port.
296  * @param acell The cell where all of this happens.
297  * @param areq The request.
298  * @param aforce Force connection?
299  * @param locktype Type of lock to be used.
300  *
301  * @return The established connection.
302  */
303 struct conn *
304 afs_ConnByHost(struct server *aserver, unsigned short aport, afs_int32 acell,
305                struct vrequest *areq, int aforce, afs_int32 locktype)
306 {
307     struct unixuser *tu;
308     struct conn *tc = 0;
309     struct srvAddr *sa = 0;
310
311     AFS_STATCNT(afs_ConnByHost);
312
313     if (AFS_IS_DISCONNECTED && !AFS_IN_SYNC) {
314         afs_warnuser("afs_ConnByHost: disconnected\n");
315         return NULL;
316     }
317
318 /* 
319   1.  look for an existing connection
320   2.  create a connection at an address believed to be up
321       (if aforce is true, create a connection at the first address)
322 */
323
324     tu = afs_GetUser(areq->uid, acell, SHARED_LOCK);
325
326     for (sa = aserver->addr; sa; sa = sa->next_sa) {
327         tc = afs_ConnBySA(sa, aport, acell, tu, aforce,
328                           0 /*don't create one */ ,
329                           locktype);
330         if (tc)
331             break;
332     }
333
334     if (!tc) {
335         for (sa = aserver->addr; sa; sa = sa->next_sa) {
336             tc = afs_ConnBySA(sa, aport, acell, tu, aforce,
337                               1 /*create one */ ,
338                               locktype);
339             if (tc)
340                 break;
341         }
342     }
343
344     afs_PutUser(tu, SHARED_LOCK);
345     return tc;
346
347 }                               /*afs_ConnByHost */
348
349
350 /**
351  * Connect by multiple hosts.
352  * Try to connect to one of the hosts from the ahosts array.
353  *
354  * @param ahosts Multiple hosts to connect to.
355  * @param aport Connection port.
356  * @param acell The cell where all of this happens.
357  * @param areq The request.
358  * @param locktype Type of lock to be used.
359  *
360  * @return The established connection or NULL.
361  */
362 struct conn *
363 afs_ConnByMHosts(struct server *ahosts[], unsigned short aport,
364                  afs_int32 acell, register struct vrequest *areq,
365                  afs_int32 locktype)
366 {
367     register afs_int32 i;
368     register struct conn *tconn;
369     register struct server *ts;
370
371     /* try to find any connection from the set */
372     AFS_STATCNT(afs_ConnByMHosts);
373     for (i = 0; i < MAXCELLHOSTS; i++) {
374         if ((ts = ahosts[i]) == NULL)
375             break;
376         tconn = afs_ConnByHost(ts, aport, acell, areq, 0, locktype);
377         if (tconn) {
378             return tconn;
379         }
380     }
381     return NULL;
382
383 }                               /*afs_ConnByMHosts */
384
385
386 /**
387  * Decrement reference count to this connection.
388  * @param ac
389  * @param locktype
390  */
391 void
392 afs_PutConn(register struct conn *ac, afs_int32 locktype)
393 {
394     AFS_STATCNT(afs_PutConn);
395     ac->refCount--;
396 }                               /*afs_PutConn */
397
398
399 /** 
400  * For multi homed clients, a RPC may timeout because of a 
401  * client network interface going down. We need to reopen new 
402  * connections in this case.
403  *
404  * @param sap Server address.
405  */
406 void
407 ForceNewConnections(struct srvAddr *sap)
408 {
409     struct conn *tc = 0;
410
411     if (!sap)
412         return;                 /* defensive check */
413
414     ObtainWriteLock(&afs_xconn, 413);
415     for (tc = sap->conns; tc; tc = tc->next)
416         tc->forceConnectFS = 1;
417     ReleaseWriteLock(&afs_xconn);
418 }