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