248050f4d954569fab1acf80bfb39ec35f6e3b15
[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                 tsp->refCount++;
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                 osi_assert(tsp->refCount-- > 0);
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_PutServer(cm_server_t *serverp)
139 {
140         lock_ObtainWrite(&cm_serverLock);
141         osi_assert(serverp->refCount-- > 0);
142         lock_ReleaseWrite(&cm_serverLock);
143 }
144
145 void cm_SetServerPrefs(cm_server_t * serverp)
146 {
147         unsigned long   serverAddr;     /* in host byte order */
148         unsigned long   myAddr, myNet, mySubnet;/* in host byte order */
149         unsigned long   netMask;
150         int             i;
151
152         /* implement server prefs for fileservers only */
153         if ( serverp->type == CM_SERVER_FILE )
154         {
155             serverAddr = ntohl(serverp->addr.sin_addr.s_addr);
156             serverp->ipRank  = CM_IPRANK_LOW;   /* default setings */
157
158             for ( i=0; i < cm_noIPAddr; i++)
159             {
160                 /* loop through all the client's IP address and compare
161                 ** each of them against the server's IP address */
162
163                 myAddr = cm_IPAddr[i];
164                 if ( IN_CLASSA(myAddr) )
165                     netMask = IN_CLASSA_NET;
166                 else if ( IN_CLASSB(myAddr) )
167                     netMask = IN_CLASSB_NET;
168                 else if ( IN_CLASSC(myAddr) )
169                     netMask = IN_CLASSC_NET;
170                 else
171                     netMask = 0;
172
173                 myNet    =  myAddr & netMask;
174                 mySubnet =  myAddr & cm_SubnetMask[i];
175
176                 if ( (serverAddr & netMask) == myNet ) 
177                 {
178                     if ( (serverAddr & cm_SubnetMask[i]) == mySubnet)
179                     {
180                         if ( serverAddr == myAddr ) 
181                             serverp->ipRank = min(serverp->ipRank,
182                                               CM_IPRANK_TOP);/* same machine */
183                         else serverp->ipRank = min(serverp->ipRank,
184                                               CM_IPRANK_HI); /* same subnet */
185                     }
186                     else serverp->ipRank = min(serverp->ipRank,CM_IPRANK_MED);
187                                                            /* same net */
188                 }       
189                                                  /* random between 0..15*/
190                 serverp->ipRank += min(serverp->ipRank, rand() % 0x000f);
191             } /* and of for loop */
192         }
193         else    serverp->ipRank = 10000 + (rand() % 0x00ff); /* VL server */
194 }
195
196 cm_server_t *cm_NewServer(struct sockaddr_in *socketp, int type, cm_cell_t *cellp) {
197         cm_server_t *tsp;
198
199         osi_assert(socketp->sin_family == AF_INET);
200
201         tsp = malloc(sizeof(*tsp));
202     memset(tsp, 0, sizeof(*tsp));
203         tsp->type = type;
204     tsp->cellp = cellp;
205     tsp->refCount = 1;
206         lock_InitializeMutex(&tsp->mx, "cm_server_t mutex");
207         tsp->addr = *socketp;
208
209         cm_SetServerPrefs(tsp); 
210
211     lock_ObtainWrite(&cm_serverLock); /* get server lock */
212         tsp->allNextp = cm_allServersp;
213     cm_allServersp = tsp;
214     lock_ReleaseWrite(&cm_serverLock); /* release server lock */
215
216     return tsp;
217 }
218
219 /* find a server based on its properties */
220 cm_server_t *cm_FindServer(struct sockaddr_in *addrp, int type)
221 {
222         cm_server_t *tsp;
223
224         osi_assert(addrp->sin_family == AF_INET);
225         
226         lock_ObtainWrite(&cm_serverLock);
227         for(tsp = cm_allServersp; tsp; tsp=tsp->allNextp) {
228                 if (tsp->type == type &&
229                         tsp->addr.sin_addr.s_addr == addrp->sin_addr.s_addr) break;
230         }
231
232         /* bump ref count if we found the server */
233         if (tsp) tsp->refCount++;
234
235         /* drop big table lock */
236         lock_ReleaseWrite(&cm_serverLock);
237         
238         /* return what we found */
239         return tsp;
240 }
241
242 cm_serverRef_t *cm_NewServerRef(cm_server_t *serverp)
243 {
244         cm_serverRef_t *tsrp;
245
246         lock_ObtainWrite(&cm_serverLock);
247         serverp->refCount++;
248         lock_ReleaseWrite(&cm_serverLock);
249         tsrp = malloc(sizeof(*tsrp));
250         tsrp->server = serverp;
251         tsrp->status = not_busy;
252         tsrp->next = NULL;
253
254         return tsrp;
255 }
256
257 long cm_ChecksumServerList(cm_serverRef_t *serversp)
258 {
259         long sum = 0;
260         int first = 1;
261         cm_serverRef_t *tsrp;
262
263     lock_ObtainWrite(&cm_serverLock);
264
265         for (tsrp = serversp; tsrp; tsrp=tsrp->next) {
266                 if (first)
267                         first = 0;
268                 else
269                         sum <<= 1;
270                 sum ^= (long) tsrp->server;
271         }
272
273     lock_ReleaseWrite(&cm_serverLock);
274         return sum;
275 }
276
277 /*
278 ** Insert a server into the server list keeping the list sorted in 
279 ** asending order of ipRank. 
280 */
281 void cm_InsertServerList(cm_serverRef_t** list, cm_serverRef_t* element)
282 {
283         cm_serverRef_t  *current=*list;
284         unsigned short ipRank = element->server->ipRank;
285
286     lock_ObtainWrite(&cm_serverLock);
287
288         /* insertion into empty list  or at the beginning of the list */
289         if ( !current || (current->server->ipRank > ipRank) )
290         {
291                 element->next = *list;
292                 *list = element;
293         lock_ReleaseWrite(&cm_serverLock);
294                 return ;        
295         }
296         
297         while ( current->next ) /* find appropriate place to insert */
298         {
299                 if ( current->next->server->ipRank > ipRank )
300                         break;
301                 else current = current->next;
302         }
303         element->next = current->next;
304         current->next = element;
305     lock_ReleaseWrite(&cm_serverLock);
306 }
307 /*
308 ** Re-sort the server list with the modified rank
309 ** returns 0 if element was changed successfully. 
310 ** returns 1 if  list remained unchanged.
311 */
312 long cm_ChangeRankServer(cm_serverRef_t** list, cm_server_t*    server)
313 {
314         cm_serverRef_t  **current=list;
315         cm_serverRef_t  *element=0;
316
317         /* if there is max of one element in the list, nothing to sort */
318         if ( (!*current) || !((*current)->next)  )
319                 return 1;               /* list unchanged: return success */
320
321     lock_ObtainWrite(&cm_serverLock);
322         /* if the server is on the list, delete it from list */
323         while ( *current )
324         {
325                 if ( (*current)->server == server)
326                 {
327                         element = (*current);
328                         *current = (*current)->next; /* delete it */
329                         break;
330                 }
331                 current = & ( (*current)->next);        
332         }
333     lock_ReleaseWrite(&cm_serverLock);
334
335     /* if this volume is not replicated on this server  */
336         if (!element)
337                 return 1;       /* server is not on list */
338
339         /* re-insert deleted element into the list with modified rank*/
340         cm_InsertServerList(list, element);
341         return 0;
342 }
343 /*
344 ** If there are more than one server on the list and the first n servers on 
345 ** the list have the same rank( n>1), then randomise among the first n servers.
346 */
347 void cm_RandomizeServer(cm_serverRef_t** list)
348 {
349         int             count, picked;
350         cm_serverRef_t* tsrp = *list, *lastTsrp;
351         unsigned short  lowestRank;
352
353         /* an empty list or a list with only one element */
354         if ( !tsrp || ! tsrp->next )
355                 return ; 
356
357     lock_ObtainWrite(&cm_serverLock);
358
359         /* count the number of servers with the lowest rank */
360         lowestRank = tsrp->server->ipRank;
361         for ( count=1, tsrp=tsrp->next; tsrp; tsrp=tsrp->next)
362         {
363                 if ( tsrp->server->ipRank != lowestRank)
364                         break;
365                 else
366                         count++;
367         }       
368
369         /* if there is only one server with the lowest rank, we are done */
370         if ( count <= 1 ) {
371         lock_ReleaseWrite(&cm_serverLock);
372                 return ;
373     }
374
375         picked = rand() % count;
376         if ( !picked ) {
377         lock_ReleaseWrite(&cm_serverLock);
378                 return ;
379     }
380
381         tsrp = *list;
382         while (--picked >= 0)
383         {
384                 lastTsrp = tsrp;
385                 tsrp = tsrp->next;
386         }
387         lastTsrp->next = tsrp->next;  /* delete random element from list*/
388         tsrp->next     = *list; /* insert element at the beginning of list */
389         *list          = tsrp;
390     lock_ReleaseWrite(&cm_serverLock);
391 }
392
393 /* call cm_FreeServer while holding a write lock on cm_serverLock */
394 void cm_FreeServer(cm_server_t* server)
395 {
396     if (--(server->refCount) == 0)
397     {
398         /* we need to check to ensure that all of the connections
399          * for this server have a 0 refCount; otherwise, they will
400          * not be garbage collected 
401          */
402         cm_GCConnections(server);  /* connsp */
403
404         lock_FinalizeMutex(&server->mx);
405         if ( cm_allServersp == server )
406             cm_allServersp = server->allNextp;
407         else {
408             cm_server_t *tsp;
409
410             for(tsp = cm_allServersp; tsp->allNextp; tsp=tsp->allNextp) {
411                 if ( tsp->allNextp == server ) {
412                     tsp->allNextp = server->allNextp;
413                     break;
414                 }
415             }
416         }
417     }
418  }
419
420 void cm_FreeServerList(cm_serverRef_t** list)
421 {
422     cm_serverRef_t  *current = *list;
423     cm_serverRef_t  *next = 0;
424
425     lock_ObtainWrite(&cm_serverLock);
426
427     while (current)
428     {
429         next = current->next;
430         cm_FreeServer(current->server);
431         free(current);
432         current = next;
433     }
434   
435     lock_ReleaseWrite(&cm_serverLock);
436 }
437