windows-syscfg-getifinfo-20080308
authorJeffrey Altman <jaltman@secure-endpoints.com>
Sat, 8 Mar 2008 23:23:48 +0000 (23:23 +0000)
committerJeffrey Altman <jaltman@secure-endpoints.com>
Sat, 8 Mar 2008 23:23:48 +0000 (23:23 +0000)
LICENSE MIT

syscfg_GetIFInfo() obtains the current list of IP addresses.
Its a really expensive operation.  Call the function once
and cache the data until the next IP address change instead
of calling it everytime we receive a WhoAreYou or construct a
new server object.  Adds a new global rw lock, cm_syscfgLock

src/WINNT/afsd/cm_callback.c
src/WINNT/afsd/cm_daemon.c
src/WINNT/afsd/cm_server.c
src/WINNT/afsd/cm_server.h

index 49ffcd1..7a960dc 100644 (file)
@@ -575,13 +575,13 @@ SRXAFSCB_Probe(struct rx_call *callp)
 
 extern osi_rwlock_t cm_aclLock;
 extern osi_rwlock_t buf_globalLock;
-extern osi_rwlock_t cm_callbackLock;
 extern osi_rwlock_t cm_cellLock;
 extern osi_rwlock_t cm_connLock;
 extern osi_rwlock_t cm_daemonLock;
 extern osi_rwlock_t cm_dnlcLock;
 extern osi_rwlock_t cm_scacheLock;
 extern osi_rwlock_t cm_serverLock;
+extern osi_rwlock_t cm_syscfgLock;
 extern osi_rwlock_t cm_userLock;
 extern osi_rwlock_t cm_utilsLock;
 extern osi_rwlock_t cm_volumeLock;
@@ -607,6 +607,7 @@ static struct _ltable {
     {"buf_globalLock",   (char*)&buf_globalLock,        LOCKTYPE_RW},
     {"cm_serverLock",    (char*)&cm_serverLock,         LOCKTYPE_RW},
     {"cm_callbackLock",  (char*)&cm_callbackLock,       LOCKTYPE_RW},
+    {"cm_syscfgLock",    (char*)&cm_syscfgLock,         LOCKTYPE_RW},
     {"cm_aclLock",       (char*)&cm_aclLock,            LOCKTYPE_RW},
     {"cm_cellLock",      (char*)&cm_cellLock,           LOCKTYPE_RW},
     {"cm_connLock",      (char*)&cm_connLock,           LOCKTYPE_RW},
@@ -956,11 +957,6 @@ int
 SRXAFSCB_WhoAreYou(struct rx_call *callp, struct interfaceAddr* addr)
 {
     int i;
-    int cm_noIPAddr;         /* number of client network interfaces */
-    int cm_IPAddr[CM_MAXINTERFACE_ADDR];    /* client's IP address in host order */
-    int cm_SubnetMask[CM_MAXINTERFACE_ADDR];/* client's subnet mask in host order*/
-    int cm_NetMtu[CM_MAXINTERFACE_ADDR];    /* client's MTU sizes */
-    int cm_NetFlags[CM_MAXINTERFACE_ADDR];  /* network flags */
     long code;
     struct rx_connection *connp;
     struct rx_peer *peerp;
@@ -972,17 +968,25 @@ SRXAFSCB_WhoAreYou(struct rx_call *callp, struct interfaceAddr* addr)
         port = rx_PortOf(peerp);
     }
 
-    /* get network related info */
-    cm_noIPAddr = CM_MAXINTERFACE_ADDR;
-    code = syscfg_GetIFInfo(&cm_noIPAddr,
-                             cm_IPAddr, cm_SubnetMask,
-                             cm_NetMtu, cm_NetFlags);
-
-    /* return all network interface addresses */
     osi_Log2(afsd_logp, "SRXAFSCB_WhoAreYou from host 0x%x port %d",
               ntohl(host),
               ntohs(port));
 
+    lock_ObtainRead(&cm_syscfgLock);
+    if (cm_LanAdapterChangeDetected) {
+        lock_ConvertRToW(&cm_syscfgLock);
+        if (cm_LanAdapterChangeDetected) {
+            /* get network related info */
+            cm_noIPAddr = CM_MAXINTERFACE_ADDR;
+            code = syscfg_GetIFInfo(&cm_noIPAddr,
+                                     cm_IPAddr, cm_SubnetMask,
+                                     cm_NetMtu, cm_NetFlags);
+            cm_LanAdapterChangeDetected = 0;
+        }
+        lock_ConvertWToR(&cm_syscfgLock);
+    }
+
+    /* return all network interface addresses */
     addr->numberOfInterfaces = cm_noIPAddr;
     addr->uuid = cm_data.Uuid;
     for ( i=0; i < cm_noIPAddr; i++ ) {
@@ -992,6 +996,8 @@ SRXAFSCB_WhoAreYou(struct rx_call *callp, struct interfaceAddr* addr)
             cm_NetMtu[i] : rx_mtu;
     }
 
+    lock_ReleaseRead(&cm_syscfgLock);
+
     return 0;
 }
 
@@ -1117,11 +1123,6 @@ SRXAFSCB_TellMeAboutYourself( struct rx_call *callp,
     int i;
     afs_int32 *dataBuffP;
     afs_int32 dataBytes;
-    int cm_noIPAddr;         /* number of client network interfaces */
-    int cm_IPAddr[CM_MAXINTERFACE_ADDR];    /* client's IP address in host order */
-    int cm_SubnetMask[CM_MAXINTERFACE_ADDR];/* client's subnet mask in host order*/
-    int cm_NetMtu[CM_MAXINTERFACE_ADDR];    /* client's MTU sizes */
-    int cm_NetFlags[CM_MAXINTERFACE_ADDR];  /* network flags */
     long code;
     struct rx_connection *connp;
     struct rx_peer *peerp;
@@ -1133,16 +1134,24 @@ SRXAFSCB_TellMeAboutYourself( struct rx_call *callp,
         port = rx_PortOf(peerp);
     }
 
-    /* get network related info */
-    cm_noIPAddr = CM_MAXINTERFACE_ADDR;
-    code = syscfg_GetIFInfo(&cm_noIPAddr,
-                             cm_IPAddr, cm_SubnetMask,
-                             cm_NetMtu, cm_NetFlags);
-
     osi_Log2(afsd_logp, "SRXAFSCB_TellMeAboutYourself from host 0x%x port %d",
               ntohl(host),
               ntohs(port));
 
+    lock_ObtainRead(&cm_syscfgLock);
+    if (cm_LanAdapterChangeDetected) {
+        lock_ConvertRToW(&cm_syscfgLock);
+        if (cm_LanAdapterChangeDetected) {
+            /* get network related info */
+            cm_noIPAddr = CM_MAXINTERFACE_ADDR;
+            code = syscfg_GetIFInfo(&cm_noIPAddr,
+                                     cm_IPAddr, cm_SubnetMask,
+                                     cm_NetMtu, cm_NetFlags);
+            cm_LanAdapterChangeDetected = 0;
+        }
+        lock_ConvertWToR(&cm_syscfgLock);
+    }
+
     /* return all network interface addresses */
     addr->numberOfInterfaces = cm_noIPAddr;
     addr->uuid = cm_data.Uuid;
@@ -1152,6 +1161,7 @@ SRXAFSCB_TellMeAboutYourself( struct rx_call *callp,
         addr->mtu[i] = (rx_mtu == -1 || (rx_mtu != -1 && cm_NetMtu[i] < rx_mtu)) ? 
             cm_NetMtu[i] : rx_mtu;
     }
+    lock_ReleaseRead(&cm_syscfgLock);
 
     dataBytes = 1 * sizeof(afs_int32);
     dataBuffP = (afs_int32 *) osi_Alloc(dataBytes);
index ccd34a3..a49c289 100644 (file)
@@ -73,6 +73,7 @@ void cm_IpAddrDaemon(long parm)
         if (Result == NO_ERROR && daemon_ShutdownFlag == 0) {
             lastIPAddrChange = osi_Time();
             smb_SetLanAdapterChangeDetected();
+            cm_SetLanAdapterChangeDetected();
             thrd_ResetEvent(cm_IPAddrDaemon_ShutdownEvent);
        }       
     }
index 364ab7f..cc990dc 100644 (file)
@@ -23,6 +23,7 @@
 #include <rx/rx.h>
 
 osi_rwlock_t cm_serverLock;
+osi_rwlock_t cm_syscfgLock;
 
 cm_server_t *cm_allServersp;
 afs_uint32   cm_numFileServers = 0;
@@ -761,10 +762,26 @@ void cm_InitServer(void)
         
     if (osi_Once(&once)) {
         lock_InitializeRWLock(&cm_serverLock, "cm_serverLock");
+        lock_InitializeRWLock(&cm_syscfgLock, "cm_syscfgLock");
         osi_EndOnce(&once);
     }
 }
 
+/* Protected by cm_syscfgLock (rw) */
+int cm_noIPAddr;         /* number of client network interfaces */
+int cm_IPAddr[CM_MAXINTERFACE_ADDR];    /* client's IP address in host order */
+int cm_SubnetMask[CM_MAXINTERFACE_ADDR];/* client's subnet mask in host order*/
+int cm_NetMtu[CM_MAXINTERFACE_ADDR];    /* client's MTU sizes */
+int cm_NetFlags[CM_MAXINTERFACE_ADDR];  /* network flags */
+int cm_LanAdapterChangeDetected = 1;
+
+void cm_SetLanAdapterChangeDetected(void)
+{
+    lock_ObtainWrite(&cm_syscfgLock);
+    cm_LanAdapterChangeDetected = 1;
+    lock_ReleaseWrite(&cm_syscfgLock);
+}
+
 void cm_GetServer(cm_server_t *serverp)
 {
     lock_ObtainRead(&cm_serverLock);
@@ -818,19 +835,23 @@ void cm_SetServerPrefs(cm_server_t * serverp)
     unsigned long      myAddr, myNet, mySubnet;/* in host byte order */
     unsigned long      netMask;
     int                i;
-
-    int cm_noIPAddr;         /* number of client network interfaces */
-    int cm_IPAddr[CM_MAXINTERFACE_ADDR];    /* client's IP address in host order */
-    int cm_SubnetMask[CM_MAXINTERFACE_ADDR];/* client's subnet mask in host order*/
-    int cm_NetMtu[CM_MAXINTERFACE_ADDR];    /* client's MTU sizes */
-    int cm_NetFlags[CM_MAXINTERFACE_ADDR];  /* network flags */
     long code;
-
-    /* get network related info */
-    cm_noIPAddr = CM_MAXINTERFACE_ADDR;
-    code = syscfg_GetIFInfo(&cm_noIPAddr,
-                           cm_IPAddr, cm_SubnetMask,
-                           cm_NetMtu, cm_NetFlags);
+    int writeLock = 0;
+
+    lock_ObtainRead(&cm_syscfgLock);
+    if (cm_LanAdapterChangeDetected) {
+        lock_ConvertRToW(&cm_syscfgLock);
+        writeLock = 1;
+        if (cm_LanAdapterChangeDetected) {
+            /* get network related info */
+            cm_noIPAddr = CM_MAXINTERFACE_ADDR;
+            code = syscfg_GetIFInfo(&cm_noIPAddr,
+                                     cm_IPAddr, cm_SubnetMask,
+                                     cm_NetMtu, cm_NetFlags);
+            cm_LanAdapterChangeDetected = 0;
+        }
+        lock_ConvertWToR(&cm_syscfgLock);
+    }
 
     serverAddr = ntohl(serverp->addr.sin_addr.s_addr);
     serverp->ipRank  = CM_IPRANK_LOW;  /* default setings */
@@ -869,6 +890,7 @@ void cm_SetServerPrefs(cm_server_t * serverp)
        /* random between 0..15*/
        serverp->ipRank += min(serverp->ipRank, rand() % 0x000f);
     } /* and of for loop */
+    lock_ReleaseRead(&cm_syscfgLock);
 }
 
 cm_server_t *cm_NewServer(struct sockaddr_in *socketp, int type, cm_cell_t *cellp, afs_uint32 flags) {
index d100667..143ff4c 100644 (file)
@@ -96,6 +96,8 @@ extern cm_server_t *cm_FindServer(struct sockaddr_in *addrp, int type);
 
 extern osi_rwlock_t cm_serverLock;
 
+extern osi_rwlock_t cm_syscfgLock;
+
 extern void cm_InitServer(void);
 
 extern void cm_CheckServers(afs_uint32 flags, struct cm_cell *cellp);
@@ -124,4 +126,13 @@ extern void cm_SetServerNoInlineBulk(cm_server_t * serverp, int no);
 
 extern cm_server_t * cm_FindServerByIP(afs_uint32 addr, int type);
 
+extern void cm_SetLanAdapterChangeDetected(void);
+
+/* Protected by cm_syscfgLock (rw) */
+extern int cm_noIPAddr;         /* number of client network interfaces */
+extern int cm_IPAddr[CM_MAXINTERFACE_ADDR];    /* client's IP address in host order */
+extern int cm_SubnetMask[CM_MAXINTERFACE_ADDR];/* client's subnet mask in host order*/
+extern int cm_NetMtu[CM_MAXINTERFACE_ADDR];    /* client's MTU sizes */
+extern int cm_NetFlags[CM_MAXINTERFACE_ADDR];  /* network flags */
+extern int cm_LanAdapterChangeDetected;
 #endif /*  __CM_SERVER_H_ENV__ */