Quellcode durchsuchen

1、交换机接口增加锁保护
2、交换机ip数据处理有误

guohui vor 2 Jahren
Ursprung
Commit
39698c8441
1 geänderte Dateien mit 30 neuen und 27 gelöschten Zeilen
  1. 30 27
      pro/src/netswitch.c

+ 30 - 27
pro/src/netswitch.c

@@ -33,7 +33,9 @@ typedef struct {
 
     int              ipflag;
     char             host_ip[64];
-    int              port_start;         
+    int              port_start;
+    
+    pthread_mutex_t  mutex;
 }ns_handle_t;
 static ns_handle_t nsHandle;
 
@@ -317,6 +319,14 @@ static void sw_init(netswitch_info_t *sw)
         sw->port[i].id = -1;
     }
 }
+static void sw_init2(netswitch_info_t *sw)
+{
+    int i;
+    memset(sw, 0, sizeof(netswitch_info_t));
+    for(i=0; i<SW_PORT_MAX; i++) {
+        sw->port[i].id = i;
+    }
+}
 static void sw_sync_mac_by_id(netswitch_info_t *dst, netswitch_info_t *src)
 {
     int i,id;
@@ -349,16 +359,7 @@ static void sw_sync_ip_by_mac(netswitch_info_t *dst, netswitch_info_t *src)
     for(i=0; i<SW_PORT_MAX; i++) {
         id = get_id_by_mac(dst, &src->port[i]);
         if(id>=0) {
-            src->port[id].id = id;
-        }
-    }
-
-    for(i=0; i<SW_PORT_MAX; i++) {
-        if(src->port[i].id>=0) {
-            strcpy(dst->port[i].ip, src->port[i].ip);
-        }
-        else {
-            dst->port[i].ip[0] = 0;
+            strcpy(dst->port[id].ip, src->port[i].ip);
         }
     }
 }
@@ -499,6 +500,8 @@ static int sw_get(netswitch_info_t *sw)
     netsnmp_variable_list *var,*pvar;
     ns_handle_t *h=&nsHandle;
     
+    sw_init2(sw);
+
     if(h->ipflag==0) {
 #ifdef SW_STATIC_IP
         char hnet[100];
@@ -615,7 +618,7 @@ static int sw_get(netswitch_info_t *sw)
                     if(p) {
                         p += strlen(tok);
                         get_mac(p, tmp.port[i].mac);
-                        //LOGD("_____%d___mac:%s\n", i, tmp.port[i].mac);
+                        LOGD("__%d__ %s\n", i, tmp.port[i].mac);
 
                         i++;
                     }
@@ -627,9 +630,9 @@ static int sw_get(netswitch_info_t *sw)
                     if(p) {
                         p += strlen(tok);
                         strcpy(tmp.port[j].ip, p);
-                        j++;
+                        LOGD("__%d__ %s\n", j, p);
 
-                        //LOGD("__%d__ %s\n", j, p);
+                        j++;
                     }
                 }
 
@@ -781,26 +784,17 @@ static int sw_alarm(netswitch_evts_t *evts, netswitch_info_t *old, netswitch_inf
     for(i=0; i<SW_PORT_MAX; i++) {
         
         temp[0] = 0;
-        if(evts->evt[i].cab) {
-            flag = 1;
-            snprintf(temp, sizeof(temp), "port[%d] plug %s\n", i+1, (evts->evt[i].cab==EVT_CAB_IN)?"in":"out");
-            strcat(alarm_str, temp);
-        }
-         
         if(evts->evt[i].dev) {
             flag = 1;
-            if(evts->evt[i].dev==EVT_IP_CHG) {
-                snprintf(temp, sizeof(temp), "port[%d] device ip changed from %s to %s\n", i+1, old->port[i].ip, new->port[i].ip);
-            }
-            else {
-                //snprintf(temp, sizeof(temp), "port[%d] device mac changed from %s to %s\n", i+1, old->port[i].mac, new->port[i].mac);
+            if(evts->evt[i].dev==EVT_MAC_CHG) {
+                snprintf(temp, sizeof(temp), "port[%d] mac changed from %s to %s\n", i+1, old->port[i].mac, new->port[i].mac);
+                strcat(alarm_str, temp);
             }
-            strcat(alarm_str, temp);
         }
     }
 
     if(flag) {
-        r = dev_insert_alarm_info(get_db(), get_prod_id(), 0, time_str, alarm_str);
+        r = dev_insert_alarm_info(get_db(), get_prod_id(), 2, time_str, alarm_str);
     }
 
     return r;
@@ -818,7 +812,10 @@ static void* sw_thread(void *arg)
 
     SOCK_STARTUP;
     while(h->quit==0) {
+        pthread_mutex_lock(&nh->mutex);
         r = sw_get(&nh->sw2);
+        pthread_mutex_unlock(&nh->mutex);
+
         if(r==0 && sw_chk(&nh->sw, &nh->sw2, &evts)) {
             sw_alarm(&evts, &nh->sw, &nh->sw2);
             sw_save(db, &nh->sw, &nh->sw2);
@@ -840,6 +837,8 @@ int netswitch_init(void)
 
     memset(h, 0, sizeof(ns_handle_t));
     h->port_start = -1; //SW_PORT_START
+
+    pthread_mutex_init(&h->mutex, NULL);
     thread_start(THREAD_ID_SWITCH, sw_thread, h, 10*MB, 0);
 #endif
 
@@ -853,6 +852,7 @@ int netswitch_deinit(void)
     ns_handle_t *h=&nsHandle;
     
     thread_stop(THREAD_ID_SWITCH);
+    pthread_mutex_destroy(&h->mutex);
 #endif
 
     return 0;
@@ -867,7 +867,10 @@ int netswitch_get(netswitch_info_t *info)
     if(!info) {
         return -1;
     }
+
+    pthread_mutex_lock(&h->mutex);
     *info = h->sw2;
+    pthread_mutex_unlock(&h->mutex);
 #endif
 
     return 0;