freelance-updates-20011031
[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, CM_CONN_CONNDEADTIME);
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         tsp->allNextp = cm_allServersp;
207         cm_allServersp = tsp;
208         lock_InitializeMutex(&tsp->mx, "cm_server_t mutex");
209         tsp->addr = *socketp;
210
211         cm_SetServerPrefs(tsp); 
212         return tsp;
213 }
214
215 /* find a server based on its properties */
216 cm_server_t *cm_FindServer(struct sockaddr_in *addrp, int type)
217 {
218         cm_server_t *tsp;
219
220         osi_assert(addrp->sin_family == AF_INET);
221         
222         lock_ObtainWrite(&cm_serverLock);
223         for(tsp = cm_allServersp; tsp; tsp=tsp->allNextp) {
224                 if (tsp->type == type &&
225                         tsp->addr.sin_addr.s_addr == addrp->sin_addr.s_addr) break;
226         }
227
228         /* bump ref count if we found the server */
229         if (tsp) tsp->refCount++;
230
231         /* drop big table lock */
232         lock_ReleaseWrite(&cm_serverLock);
233         
234         /* return what we found */
235         return tsp;
236 }
237
238 cm_serverRef_t *cm_NewServerRef(cm_server_t *serverp)
239 {
240         cm_serverRef_t *tsrp;
241
242         lock_ObtainWrite(&cm_serverLock);
243         serverp->refCount++;
244         lock_ReleaseWrite(&cm_serverLock);
245         tsrp = malloc(sizeof(*tsrp));
246         tsrp->server = serverp;
247         tsrp->status = not_busy;
248         tsrp->next = NULL;
249
250         return tsrp;
251 }
252
253 long cm_ChecksumServerList(cm_serverRef_t *serversp)
254 {
255         long sum = 0;
256         int first = 1;
257         cm_serverRef_t *tsrp;
258
259         for (tsrp = serversp; tsrp; tsrp=tsrp->next) {
260                 if (first)
261                         first = 0;
262                 else
263                         sum <<= 1;
264                 sum ^= (long) tsrp->server;
265         }
266
267         return sum;
268 }
269
270 /*
271 ** Insert a server into the server list keeping the list sorted in 
272 ** asending order of ipRank. 
273 */
274 void cm_InsertServerList(cm_serverRef_t** list, cm_serverRef_t* element)
275 {
276         cm_serverRef_t  *current=*list;
277         unsigned short ipRank = element->server->ipRank;
278
279         /* insertion into empty list  or at the beginning of the list */
280         if ( !current || (current->server->ipRank > ipRank) )
281         {
282                 element->next = *list;
283                 *list = element;
284                 return ;        
285         }
286         
287         while ( current->next ) /* find appropriate place to insert */
288         {
289                 if ( current->next->server->ipRank > ipRank )
290                         break;
291                 else current = current->next;
292         }
293         element->next = current->next;
294         current->next = element;
295 }
296 /*
297 ** Re-sort the server list with the modified rank
298 ** returns 0 if element was changed successfully. 
299 ** returns 1 if  list remained unchanged.
300 */
301 long cm_ChangeRankServer(cm_serverRef_t** list, cm_server_t*    server)
302 {
303         cm_serverRef_t  **current=list;
304         cm_serverRef_t  *element=0;
305
306         /* if there is max of one element in the list, nothing to sort */
307         if ( (!*current) || !((*current)->next)  )
308                 return 1;               /* list unchanged: return success */
309
310         /* if the server is on the list, delete it from list */
311         while ( *current )
312         {
313                 if ( (*current)->server == server)
314                 {
315                         element = (*current);
316                         *current = (*current)->next; /* delete it */
317                         break;
318                 }
319                 current = & ( (*current)->next);        
320         }
321         /* if this volume is not replicated on this server  */
322         if ( !element)
323                 return 1;       /* server is not on list */
324
325         /* re-insert deleted element into the list with modified rank*/
326         cm_InsertServerList(list, element);
327         return 0;
328 }
329 /*
330 ** If there are more than one server on the list and the first n servers on 
331 ** the list have the same rank( n>1), then randomise among the first n servers.
332 */
333 void cm_RandomizeServer(cm_serverRef_t** list)
334 {
335         int             count, picked;
336         cm_serverRef_t* tsrp = *list, *lastTsrp;
337         unsigned short  lowestRank;
338
339         /* an empty list or a list with only one element */
340         if ( !tsrp || ! tsrp->next )
341                 return ; 
342
343         /* count the number of servers with the lowest rank */
344         lowestRank = tsrp->server->ipRank;
345         for ( count=1, tsrp=tsrp->next; tsrp; tsrp=tsrp->next)
346         {
347                 if ( tsrp->server->ipRank != lowestRank)
348                         break;
349                 else
350                         count++;
351         }       
352
353         /* if there is only one server with the lowest rank, we are done */
354         if ( count <= 1 )               
355                 return ;
356
357         picked = rand() % count;
358         if ( !picked )
359                 return ;
360         tsrp = *list;
361         while (--picked >= 0)
362         {
363                 lastTsrp = tsrp;
364                 tsrp = tsrp->next;
365         }
366         lastTsrp->next = tsrp->next;  /* delete random element from list*/
367         tsrp->next     = *list; /* insert element at the beginning of list */
368         *list          = tsrp;
369 }