7b2b86b6151bc8f44b2c5ac543210a97b4e7ba55
[openafs.git] / src / WINNT / afsd / cm_server.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 #include <afs/param.h>
11 #include <afs/stds.h>
12
13 #ifndef DJGPP
14 #include <windows.h>
15 #include <winsock2.h>
16 #include <nb30.h>
17 #else
18 #include <sys/socket.h>
19 #endif /* !DJGPP */
20 #include <stdlib.h>
21 #include <malloc.h>
22 #include <string.h>
23
24 #include <osi.h>
25 #include <rx/rx.h>
26 #include "afsd.h"
27
28 osi_rwlock_t cm_serverLock;
29
30 cm_server_t *cm_allServersp;
31
32 int cm_noIPAddr;         /* number of client network interfaces */
33 int cm_IPAddr[CM_MAXINTERFACE_ADDR];    /* client's IP address in host order */
34 int cm_SubnetMask[CM_MAXINTERFACE_ADDR];/* client's subnet mask in host order*/
35 int cm_NetMtu[CM_MAXINTERFACE_ADDR];    /* client's MTU sizes */
36 int cm_NetFlags[CM_MAXINTERFACE_ADDR];  /* network flags */
37
38 void cm_CheckServers(long flags, cm_cell_t *cellp)
39 {
40         /* ping all file servers, up or down, with unauthenticated connection,
41          * to find out whether we have all our callbacks from the server still.
42          * Also, ping down VLDBs.
43          */
44         cm_server_t *tsp;
45         long code;
46         long secs;
47         long usecs;
48         int doPing;
49         int serverType;
50         long now;
51         int wasDown;
52         cm_conn_t *connp;
53
54         lock_ObtainWrite(&cm_serverLock);
55         for(tsp = cm_allServersp; tsp; tsp = tsp->allNextp) {
56         cm_GetServerNoLock(tsp);
57                 lock_ReleaseWrite(&cm_serverLock);
58
59                 /* now process the server */
60                 lock_ObtainMutex(&tsp->mx);
61
62                 /* what time is it? */
63                 now = osi_Time();
64
65                 serverType = tsp->type;
66                 doPing = 0;
67                 wasDown = tsp->flags & CM_SERVERFLAG_DOWN;
68
69                 /* only do the ping if the cell matches the requested cell, or we're
70                  * matching all cells (cellp == NULL), and if we've requested to ping
71                  * this type of {up, down} servers.
72                  */
73                 if ((cellp == NULL || cellp == tsp->cellp) &&
74                         ((wasDown && (flags & CM_FLAG_CHECKDOWNSERVERS)) ||
75                          (!wasDown && (flags & CM_FLAG_CHECKUPSERVERS)))) {
76
77                         doPing = 1;
78                 }       /* we're supposed to check this up/down server */
79                 lock_ReleaseMutex(&tsp->mx);
80                         
81                 /* at this point, we've adjusted the server state, so do the ping and
82                  * adjust things.
83                  */
84                 if (doPing) {
85                         code = cm_ConnByServer(tsp, cm_rootUserp, &connp);
86                         if (code == 0) {
87                                 /* now call the appropriate ping call.  Drop the timeout if
88                                  * the server is known to be down, so that we don't waste a
89                                  * lot of time retiming out down servers.
90                                  */
91                                 if (wasDown)
92                                         rx_SetConnDeadTime(connp->callp, 10);
93                                 if (serverType == CM_SERVER_VLDB) {
94                                         code = VL_ProbeServer(connp->callp);
95                                 }
96                                 else {
97                                         /* file server */
98                                         code = RXAFS_GetTime(connp->callp, &secs, &usecs);
99                                 }
100                                 if (wasDown)
101                                         rx_SetConnDeadTime(connp->callp, ConnDeadtimeout);
102                                 cm_PutConn(connp);
103                         }       /* got an unauthenticated connection to this server */
104
105                         lock_ObtainMutex(&tsp->mx);
106                         if (code == 0) {
107                                 /* mark server as up */
108                                 tsp->flags &= ~CM_SERVERFLAG_DOWN;
109                         }
110                         else {
111                                 /* mark server as down */
112                                 tsp->flags |= CM_SERVERFLAG_DOWN;
113                         }
114                         lock_ReleaseMutex(&tsp->mx);
115                 }
116                         
117                 /* also, run the GC function for connections on all of the
118                  * server's connections.
119                  */
120                 cm_GCConnections(tsp);
121
122                 lock_ObtainWrite(&cm_serverLock);
123         cm_PutServerNoLock(tsp);
124         }
125         lock_ReleaseWrite(&cm_serverLock);
126 }
127
128 void cm_InitServer(void)
129 {
130         static osi_once_t once;
131         
132         if (osi_Once(&once)) {
133                 lock_InitializeRWLock(&cm_serverLock, "cm_serverLock");
134                 osi_EndOnce(&once);
135         }
136 }
137
138 void cm_GetServer(cm_server_t *serverp)
139 {
140     lock_ObtainWrite(&cm_serverLock);
141     serverp->refCount++;
142     afsi_log("cm_GetServer       serverp=%x count=%d", serverp, serverp->refCount);
143     lock_ReleaseWrite(&cm_serverLock);
144 }
145
146 void cm_GetServerNoLock(cm_server_t *serverp)
147 {
148     serverp->refCount++;
149     afsi_log("cm_GetServerNoLock serverp=%x count=%d", serverp, serverp->refCount);
150 }
151
152 void cm_PutServer(cm_server_t *serverp)
153 {
154         lock_ObtainWrite(&cm_serverLock);
155     afsi_log("cm_PutServer       serverp=%x count=%d", serverp, serverp->refCount-1);
156         osi_assert(serverp->refCount-- > 0);
157         lock_ReleaseWrite(&cm_serverLock);
158 }
159
160 void cm_PutServerNoLock(cm_server_t *serverp)
161 {
162     afsi_log("cm_PutServerNoLock serverp=%x count=%d", serverp, serverp->refCount-1);
163         osi_assert(serverp->refCount-- > 0);
164 }
165
166 void cm_SetServerPrefs(cm_server_t * serverp)
167 {
168         unsigned long   serverAddr;     /* in host byte order */
169         unsigned long   myAddr, myNet, mySubnet;/* in host byte order */
170         unsigned long   netMask;
171         int             i;
172
173         /* implement server prefs for fileservers only */
174         if ( serverp->type == CM_SERVER_FILE )
175         {
176             serverAddr = ntohl(serverp->addr.sin_addr.s_addr);
177             serverp->ipRank  = CM_IPRANK_LOW;   /* default setings */
178
179             for ( i=0; i < cm_noIPAddr; i++)
180             {
181                 /* loop through all the client's IP address and compare
182                 ** each of them against the server's IP address */
183
184                 myAddr = cm_IPAddr[i];
185                 if ( IN_CLASSA(myAddr) )
186                     netMask = IN_CLASSA_NET;
187                 else if ( IN_CLASSB(myAddr) )
188                     netMask = IN_CLASSB_NET;
189                 else if ( IN_CLASSC(myAddr) )
190                     netMask = IN_CLASSC_NET;
191                 else
192                     netMask = 0;
193
194                 myNet    =  myAddr & netMask;
195                 mySubnet =  myAddr & cm_SubnetMask[i];
196
197                 if ( (serverAddr & netMask) == myNet ) 
198                 {
199                     if ( (serverAddr & cm_SubnetMask[i]) == mySubnet)
200                     {
201                         if ( serverAddr == myAddr ) 
202                             serverp->ipRank = min(serverp->ipRank,
203                                               CM_IPRANK_TOP);/* same machine */
204                         else serverp->ipRank = min(serverp->ipRank,
205                                               CM_IPRANK_HI); /* same subnet */
206                     }
207                     else serverp->ipRank = min(serverp->ipRank,CM_IPRANK_MED);
208                                                            /* same net */
209                 }       
210                                                  /* random between 0..15*/
211                 serverp->ipRank += min(serverp->ipRank, rand() % 0x000f);
212             } /* and of for loop */
213         }
214     else 
215         serverp->ipRank = 10000 + (rand() % 0x00ff); /* VL server */
216 }
217
218 cm_server_t *cm_NewServer(struct sockaddr_in *socketp, int type, cm_cell_t *cellp) {
219         cm_server_t *tsp;
220
221         osi_assert(socketp->sin_family == AF_INET);
222
223         tsp = malloc(sizeof(*tsp));
224     memset(tsp, 0, sizeof(*tsp));
225         tsp->type = type;
226     tsp->cellp = cellp;
227     tsp->refCount = 1;
228         lock_InitializeMutex(&tsp->mx, "cm_server_t mutex");
229         tsp->addr = *socketp;
230
231         cm_SetServerPrefs(tsp); 
232
233     lock_ObtainWrite(&cm_serverLock); /* get server lock */
234         tsp->allNextp = cm_allServersp;
235     cm_allServersp = tsp;
236     lock_ReleaseWrite(&cm_serverLock); /* release server lock */
237
238     return tsp;
239 }
240
241 /* find a server based on its properties */
242 cm_server_t *cm_FindServer(struct sockaddr_in *addrp, int type)
243 {
244         cm_server_t *tsp;
245
246         osi_assert(addrp->sin_family == AF_INET);
247         
248         lock_ObtainWrite(&cm_serverLock);
249         for(tsp = cm_allServersp; tsp; tsp=tsp->allNextp) {
250                 if (tsp->type == type &&
251                         tsp->addr.sin_addr.s_addr == addrp->sin_addr.s_addr) break;
252         }
253
254         /* bump ref count if we found the server */
255     if (tsp) 
256         cm_GetServerNoLock(tsp);
257
258         /* drop big table lock */
259         lock_ReleaseWrite(&cm_serverLock);
260         
261         /* return what we found */
262         return tsp;
263 }
264
265 cm_serverRef_t *cm_NewServerRef(cm_server_t *serverp)
266 {
267         cm_serverRef_t *tsrp;
268
269     cm_GetServer(serverp);
270         tsrp = malloc(sizeof(*tsrp));
271         tsrp->server = serverp;
272         tsrp->status = not_busy;
273         tsrp->next = NULL;
274     tsrp->refCount = 1;
275
276         return tsrp;
277 }
278
279 long cm_ChecksumServerList(cm_serverRef_t *serversp)
280 {
281         long sum = 0;
282         int first = 1;
283         cm_serverRef_t *tsrp;
284
285     lock_ObtainWrite(&cm_serverLock);
286         for (tsrp = serversp; tsrp; tsrp=tsrp->next) {
287                 if (first)
288                         first = 0;
289                 else
290                         sum <<= 1;
291                 sum ^= (long) tsrp->server;
292         }
293
294     lock_ReleaseWrite(&cm_serverLock);
295         return sum;
296 }
297
298 /*
299 ** Insert a server into the server list keeping the list sorted in 
300 ** asending order of ipRank. 
301 ** 
302 ** The refCount of the cm_serverRef_t is increased
303 */
304 void cm_InsertServerList(cm_serverRef_t** list, cm_serverRef_t* element)
305 {
306         cm_serverRef_t  *current=*list;
307         unsigned short ipRank = element->server->ipRank;
308
309     lock_ObtainWrite(&cm_serverLock);
310     element->refCount++;                /* increase refCount */
311
312     /* insertion into empty list  or at the beginning of the list */
313         if ( !current || (current->server->ipRank > ipRank) )
314         {
315                 element->next = *list;
316                 *list = element;
317         lock_ReleaseWrite(&cm_serverLock);
318                 return ;        
319         }
320         
321         while ( current->next ) /* find appropriate place to insert */
322         {
323                 if ( current->next->server->ipRank > ipRank )
324                         break;
325                 else current = current->next;
326         }
327         element->next = current->next;
328         current->next = element;
329     lock_ReleaseWrite(&cm_serverLock);
330 }
331 /*
332 ** Re-sort the server list with the modified rank
333 ** returns 0 if element was changed successfully. 
334 ** returns 1 if  list remained unchanged.
335 */
336 long cm_ChangeRankServer(cm_serverRef_t** list, cm_server_t*    server)
337 {
338         cm_serverRef_t  **current=list;
339         cm_serverRef_t  *element=0;
340
341         /* if there is max of one element in the list, nothing to sort */
342         if ( (!*current) || !((*current)->next)  )
343                 return 1;               /* list unchanged: return success */
344
345     lock_ObtainWrite(&cm_serverLock);
346         /* if the server is on the list, delete it from list */
347         while ( *current )
348         {
349                 if ( (*current)->server == server)
350                 {
351                         element = (*current);
352                         *current = (*current)->next; /* delete it */
353                         break;
354                 }
355                 current = & ( (*current)->next);        
356         }
357     lock_ReleaseWrite(&cm_serverLock);
358
359     /* if this volume is not replicated on this server  */
360         if (!element)
361                 return 1;       /* server is not on list */
362
363         /* re-insert deleted element into the list with modified rank*/
364         cm_InsertServerList(list, element);
365
366     /* reduce refCount which was increased by cm_InsertServerList */
367     lock_ObtainWrite(&cm_serverLock);
368     element->refCount--;
369     lock_ReleaseWrite(&cm_serverLock);
370         return 0;
371 }
372 /*
373 ** If there are more than one server on the list and the first n servers on 
374 ** the list have the same rank( n>1), then randomise among the first n servers.
375 */
376 void cm_RandomizeServer(cm_serverRef_t** list)
377 {
378         int             count, picked;
379         cm_serverRef_t* tsrp = *list, *lastTsrp;
380         unsigned short  lowestRank;
381
382         /* an empty list or a list with only one element */
383         if ( !tsrp || ! tsrp->next )
384                 return ; 
385
386     lock_ObtainWrite(&cm_serverLock);
387
388         /* count the number of servers with the lowest rank */
389         lowestRank = tsrp->server->ipRank;
390         for ( count=1, tsrp=tsrp->next; tsrp; tsrp=tsrp->next)
391         {
392                 if ( tsrp->server->ipRank != lowestRank)
393                         break;
394                 else
395                         count++;
396         }       
397
398         /* if there is only one server with the lowest rank, we are done */
399         if ( count <= 1 ) {
400         lock_ReleaseWrite(&cm_serverLock);
401                 return ;
402     }
403
404         picked = rand() % count;
405         if ( !picked ) {
406         lock_ReleaseWrite(&cm_serverLock);
407                 return ;
408     }
409
410         tsrp = *list;
411         while (--picked >= 0)
412         {
413                 lastTsrp = tsrp;
414                 tsrp = tsrp->next;
415         }
416         lastTsrp->next = tsrp->next;  /* delete random element from list*/
417         tsrp->next     = *list; /* insert element at the beginning of list */
418         *list          = tsrp;
419     lock_ReleaseWrite(&cm_serverLock);
420 }
421
422 /* call cm_FreeServer while holding a write lock on cm_serverLock */
423 void cm_FreeServer(cm_server_t* serverp)
424 {
425     cm_PutServerNoLock(serverp);
426     if (serverp->refCount == 0)
427     {
428         /* we need to check to ensure that all of the connections
429          * for this server have a 0 refCount; otherwise, they will
430          * not be garbage collected 
431          */
432         cm_GCConnections(serverp);  /* connsp */
433
434         lock_FinalizeMutex(&serverp->mx);
435         if ( cm_allServersp == serverp )
436             cm_allServersp = serverp->allNextp;
437         else {
438             cm_server_t *tsp;
439
440             for(tsp = cm_allServersp; tsp->allNextp; tsp=tsp->allNextp) {
441                 if ( tsp->allNextp == serverp ) {
442                     tsp->allNextp = serverp->allNextp;
443                     break;
444                 }
445             }
446         }
447     }
448  }
449
450 void cm_FreeServerList(cm_serverRef_t** list)
451 {
452     cm_serverRef_t  **current = list;
453     cm_serverRef_t  **nextp = 0;
454         cm_serverRef_t  * next = 0;
455
456     lock_ObtainWrite(&cm_serverLock);
457
458     while (*current)
459     {
460         nextp = &(*current)->next;
461         if (--((*current)->refCount) == 0) {
462                         next = *nextp;
463             cm_FreeServer((*current)->server);
464             free(*current);
465             *current = next;
466         } else {
467            current = nextp;
468         }
469     }
470   
471     lock_ReleaseWrite(&cm_serverLock);
472 }
473