Blame SOURCES/net-snmp-5.7.2-ipAddress-faster-load.patch

b5ae06
diff -urNp old/agent/mibgroup/ip-mib/data_access/ipaddress_common.c new/agent/mibgroup/ip-mib/data_access/ipaddress_common.c
b5ae06
--- old/agent/mibgroup/ip-mib/data_access/ipaddress_common.c	2012-10-10 00:28:58.000000000 +0200
b5ae06
+++ new/agent/mibgroup/ip-mib/data_access/ipaddress_common.c	2017-04-04 11:02:42.391951747 +0200
b5ae06
@@ -67,6 +67,7 @@ netsnmp_container *
b5ae06
 netsnmp_access_ipaddress_container_init(u_int flags)
b5ae06
 {
b5ae06
     netsnmp_container *container1;
b5ae06
+    int rc;
b5ae06
 
b5ae06
     DEBUGMSGTL(("access:ipaddress:container", "init\n"));
b5ae06
 
b5ae06
@@ -80,6 +81,7 @@ netsnmp_access_ipaddress_container_init(
b5ae06
         return NULL;
b5ae06
     }
b5ae06
     container1->container_name = strdup("ia_index");
b5ae06
+    CONTAINER_SET_OPTIONS(container1, CONTAINER_KEY_ALLOW_DUPLICATES, rc);
b5ae06
 
b5ae06
     if (flags & NETSNMP_ACCESS_IPADDRESS_INIT_ADDL_IDX_BY_ADDR) {
b5ae06
         netsnmp_container *container2 =
b5ae06
@@ -92,6 +94,13 @@ netsnmp_access_ipaddress_container_init(
b5ae06
         
b5ae06
         container2->compare = _access_ipaddress_entry_compare_addr;
b5ae06
         container2->container_name = strdup("ia_addr");
b5ae06
+
b5ae06
+        /*
b5ae06
+         * With allowed duplicates, CONTAINER_INSERT does not need to sort whole
b5ae06
+         * container and check for duplicates. We remove duplicates manually in
b5ae06
+         * netsnmp_access_ipaddress_container_load.
b5ae06
+         */
b5ae06
+        CONTAINER_SET_OPTIONS(container2, CONTAINER_KEY_ALLOW_DUPLICATES, rc);
b5ae06
         
b5ae06
         netsnmp_container_add_index(container1, container2);
b5ae06
     }
b5ae06
@@ -100,6 +109,53 @@ netsnmp_access_ipaddress_container_init(
b5ae06
 }
b5ae06
 
b5ae06
 /**
b5ae06
+ * Remove duplicate entries from the container.
b5ae06
+ * This function returns new copy of the container and destroys
b5ae06
+ * the original one. Use like this:
b5ae06
+ *   c = _remove_duplicates(c, flags);
b5ae06
+ */
b5ae06
+static netsnmp_container *
b5ae06
+_remove_duplicates(netsnmp_container *container, u_int container_flags)
b5ae06
+{
b5ae06
+       netsnmp_container *c;
b5ae06
+       netsnmp_iterator *it;
b5ae06
+       netsnmp_container *ret;
b5ae06
+       netsnmp_ipaddress_entry *entry, *prev_entry;
b5ae06
+
b5ae06
+       if (! (container_flags & NETSNMP_ACCESS_IPADDRESS_INIT_ADDL_IDX_BY_ADDR)) {
b5ae06
+               /* We don't have address index, we can't detect duplicates */
b5ae06
+               return container;
b5ae06
+       }
b5ae06
+
b5ae06
+       ret = netsnmp_access_ipaddress_container_init(container_flags);
b5ae06
+
b5ae06
+       /* use the IpAddress index */
b5ae06
+       c = container->next;
b5ae06
+       it = CONTAINER_ITERATOR(c);
b5ae06
+       /* Sort the address index */
b5ae06
+       CONTAINER_FIND(c, ITERATOR_FIRST(it));
b5ae06
+
b5ae06
+
b5ae06
+       /*
b5ae06
+        * Sequentially iterate over sorted container and add only unique entries
b5ae06
+        * to 'ret'
b5ae06
+        */
b5ae06
+       prev_entry = NULL;
b5ae06
+       for (entry = ITERATOR_FIRST(it); entry; entry = ITERATOR_NEXT(it)) {
b5ae06
+               if (prev_entry && _access_ipaddress_entry_compare_addr(prev_entry, entry) == 0) {
b5ae06
+                       /* 'entry' is duplicate of the previous one -> delete it */
b5ae06
+                       netsnmp_access_ipaddress_entry_free(entry);
b5ae06
+               } else {
b5ae06
+                       CONTAINER_INSERT(ret, entry);
b5ae06
+                       prev_entry = entry;
b5ae06
+               }
b5ae06
+       }
b5ae06
+       CONTAINER_FREE(container);
b5ae06
+       free(it);
b5ae06
+       return ret;
b5ae06
+}
b5ae06
+
b5ae06
+/**
b5ae06
  * @retval NULL  error
b5ae06
  * @retval !NULL pointer to container
b5ae06
  */
b5ae06
@@ -112,9 +168,10 @@ netsnmp_access_ipaddress_container_load(
b5ae06
 
b5ae06
     DEBUGMSGTL(("access:ipaddress:container", "load\n"));
b5ae06
 
b5ae06
+    if (load_flags & NETSNMP_ACCESS_IPADDRESS_LOAD_ADDL_IDX_BY_ADDR)
b5ae06
+        container_flags |= NETSNMP_ACCESS_IPADDRESS_INIT_ADDL_IDX_BY_ADDR;
b5ae06
+
b5ae06
     if (NULL == container) {
b5ae06
-        if (load_flags & NETSNMP_ACCESS_IPADDRESS_LOAD_ADDL_IDX_BY_ADDR)
b5ae06
-            container_flags |= NETSNMP_ACCESS_IPADDRESS_INIT_ADDL_IDX_BY_ADDR;
b5ae06
         container = netsnmp_access_ipaddress_container_init(container_flags);
b5ae06
     }
b5ae06
     if (NULL == container) {
b5ae06
@@ -129,6 +186,9 @@ netsnmp_access_ipaddress_container_load(
b5ae06
         container = NULL;
b5ae06
     }
b5ae06
 
b5ae06
+    if (container)
b5ae06
+        container = _remove_duplicates(container, container_flags);
b5ae06
+
b5ae06
     return container;
b5ae06
 }
b5ae06
 
b5ae06
diff -urNp old/agent/mibgroup/ip-mib/ipAddressTable/ipAddressTable_data_access.c new/agent/mibgroup/ip-mib/ipAddressTable/ipAddressTable_data_access.c
b5ae06
--- old/agent/mibgroup/ip-mib/ipAddressTable/ipAddressTable_data_access.c	2012-10-10 00:28:58.000000000 +0200
b5ae06
+++ new/agent/mibgroup/ip-mib/ipAddressTable/ipAddressTable_data_access.c	2017-04-04 13:26:34.332529808 +0200
b5ae06
@@ -137,6 +137,13 @@ ipAddressTable_container_init(netsnmp_co
b5ae06
     *container_ptr_ptr =
b5ae06
         netsnmp_container_find("ipAddressTable:table_container");
b5ae06
     if (NULL != *container_ptr_ptr) {
b5ae06
+        /*
b5ae06
+         * The container has ALLOW_DUPLICATES flag to speed up CONTAINER_INSERT
b5ae06
+         * operations (it does not need to check for duplicates), however we
b5ae06
+         * (manually) ensure that we won't insert any duplicates there.
b5ae06
+         */
b5ae06
+        int rc;
b5ae06
+        CONTAINER_SET_OPTIONS(*container_ptr_ptr, CONTAINER_KEY_ALLOW_DUPLICATES, rc);
b5ae06
         (*container_ptr_ptr)->container_name = strdup("ipAddressTable");
b5ae06
         ipAddressTable_container_load(*container_ptr_ptr);
b5ae06
         CONTAINER_FOR_EACH(*container_ptr_ptr,
b5ae06
@@ -205,8 +212,9 @@ static void
b5ae06
 _check_entry_for_updates(ipAddressTable_rowreq_ctx * rowreq_ctx,
b5ae06
                          void **magic)
b5ae06
 {
b5ae06
-    netsnmp_container *ipaddress_container = (netsnmp_container*)magic[0];
b5ae06
+    netsnmp_container *ipaddress_container = magic[0];
b5ae06
     netsnmp_container *to_delete           = (netsnmp_container*)magic[1];
b5ae06
+    netsnmp_container *to_ignore =  (netsnmp_container *) magic[2];
b5ae06
 
b5ae06
     /*
b5ae06
      * check for matching entry using secondary index.
b5ae06
@@ -234,10 +242,21 @@ _check_entry_for_updates(ipAddressTable_
b5ae06
             rowreq_ctx->ipAddressLastChanged = netsnmp_get_agent_uptime();
b5ae06
 
b5ae06
         /*
b5ae06
-         * remove entry from ifcontainer
b5ae06
+         * Remember not to add this entry from 'ipaddress_container' to 'container' later.
b5ae06
+         * Simple CONTAINER_REMOVE(ipaddress_container, ..) would be slow.
b5ae06
          */
b5ae06
-        CONTAINER_REMOVE(ipaddress_container, ipaddress_entry);
b5ae06
-        netsnmp_access_ipaddress_entry_free(ipaddress_entry);
b5ae06
+        if (NULL == to_ignore) {
b5ae06
+            magic[2] = to_ignore = netsnmp_container_find("access_ipaddress:table_container");
b5ae06
+            if (NULL == to_ignore) {
b5ae06
+                snmp_log(LOG_ERR, "couldn't create ignore container\n");
b5ae06
+            } else {
b5ae06
+                /* to speed up insertion */
b5ae06
+                int rc;
b5ae06
+                CONTAINER_SET_OPTIONS(to_ignore, CONTAINER_KEY_ALLOW_DUPLICATES, rc);
b5ae06
+            }
b5ae06
+        }
b5ae06
+        if (NULL != to_ignore)
b5ae06
+            CONTAINER_INSERT(to_ignore, ipaddress_entry);
b5ae06
     }
b5ae06
 }
b5ae06
 
b5ae06
@@ -246,8 +265,11 @@ _check_entry_for_updates(ipAddressTable_
b5ae06
  */
b5ae06
 static void
b5ae06
 _add_new_entry(netsnmp_ipaddress_entry *ipaddress_entry,
b5ae06
-               netsnmp_container *container)
b5ae06
+               void **magic)
b5ae06
 {
b5ae06
+    netsnmp_container *container = magic[0];
b5ae06
+    netsnmp_container *to_ignore = magic[2];
b5ae06
+
b5ae06
     ipAddressTable_rowreq_ctx *rowreq_ctx;
b5ae06
 
b5ae06
     DEBUGMSGTL(("ipAddressTable:access", "creating new entry\n"));
b5ae06
@@ -255,6 +277,11 @@ _add_new_entry(netsnmp_ipaddress_entry *
b5ae06
     netsnmp_assert(NULL != ipaddress_entry);
b5ae06
     netsnmp_assert(NULL != container);
b5ae06
 
b5ae06
+    if (to_ignore && CONTAINER_FIND(to_ignore, ipaddress_entry)) {
b5ae06
+        /* this entry already is in 'container', skip it */
b5ae06
+        return;
b5ae06
+    }
b5ae06
+
b5ae06
     /*
b5ae06
      * allocate an row context and set the index(es)
b5ae06
      */
b5ae06
@@ -329,36 +356,44 @@ int
b5ae06
 ipAddressTable_container_load(netsnmp_container *container)
b5ae06
 {
b5ae06
     netsnmp_container *ipaddress_container;
b5ae06
-    void           *tmp_ptr[2];
b5ae06
+    void           *tmp_ptr[3];
b5ae06
 
b5ae06
     DEBUGMSGTL(("verbose:ipAddressTable:ipAddressTable_cache_load",
b5ae06
                 "called\n"));
b5ae06
 
b5ae06
     /*
b5ae06
-     * TODO:351:M: |-> Load/update data in the ipAddressTable container.
b5ae06
+     * Load/update data in the ipAddressTable container.
b5ae06
      * loop over your ipAddressTable data, allocate a rowreq context,
b5ae06
      * set the index(es) [and data, optionally] and insert into
b5ae06
      * the container.
b5ae06
      */
b5ae06
+    /*
b5ae06
+     * netsnmp_access_ipaddress_container_load makes sure that
b5ae06
+     * ipaddress_container does not contain any duplicate entries,
b5ae06
+     */
b5ae06
+
b5ae06
     ipaddress_container =
b5ae06
         netsnmp_access_ipaddress_container_load(NULL,
b5ae06
                                                 NETSNMP_ACCESS_IPADDRESS_LOAD_ADDL_IDX_BY_ADDR);
b5ae06
     /*
b5ae06
      * we just got a fresh copy of interface data. compare it to
b5ae06
      * what we've already got, and make any adjustments, saving
b5ae06
-     * missing addresses to be deleted.
b5ae06
+     * missing addresses to be deleted. Also, prune interfaces in
b5ae06
+     * ipaddress_container, so only the new interfaces remain.
b5ae06
      */
b5ae06
     tmp_ptr[0] = ipaddress_container->next;
b5ae06
-    tmp_ptr[1] = NULL;
b5ae06
+    tmp_ptr[1] = NULL; /* list of interfaces to be removed from 'container' */
b5ae06
+    tmp_ptr[2] = NULL; /* list of interfaces to be ignored in ipaddress_container */
b5ae06
     CONTAINER_FOR_EACH(container, (netsnmp_container_obj_func *)
b5ae06
                        _check_entry_for_updates, tmp_ptr);
b5ae06
 
b5ae06
     /*
b5ae06
      * now add any new interfaces
b5ae06
      */
b5ae06
+    tmp_ptr[0] = container;
b5ae06
     CONTAINER_FOR_EACH(ipaddress_container,
b5ae06
                        (netsnmp_container_obj_func *) _add_new_entry,
b5ae06
-                       container);
b5ae06
+                       tmp_ptr);
b5ae06
 
b5ae06
     /*
b5ae06
      * free the container. we've either claimed each entry, or released it,
b5ae06
@@ -396,6 +431,19 @@ ipAddressTable_container_load(netsnmp_co
b5ae06
              */
b5ae06
             CONTAINER_REMOVE(tmp_container, NULL);
b5ae06
         }
b5ae06
+        CONTAINER_FREE(tmp_container);
b5ae06
+    }
b5ae06
+
b5ae06
+    if (NULL != tmp_ptr[2]) {
b5ae06
+        /* list of interfaces to be ignored in ipaddress_container - free it */
b5ae06
+        netsnmp_container *to_ignore = (netsnmp_container *) tmp_ptr[2];
b5ae06
+        netsnmp_ipaddress_entry *ipaddress_entry;
b5ae06
+        while (CONTAINER_SIZE(to_ignore)) {
b5ae06
+            ipaddress_entry = (netsnmp_ipaddress_entry*)CONTAINER_FIRST(to_ignore);
b5ae06
+            CONTAINER_REMOVE(to_ignore, ipaddress_entry);
b5ae06
+            netsnmp_access_ipaddress_entry_free(ipaddress_entry);
b5ae06
+        }
b5ae06
+        CONTAINER_FREE(to_ignore);
b5ae06
     }
b5ae06
 
b5ae06
     DEBUGMSGT(("verbose:ipAddressTable:ipAddressTable_cache_load",
b5ae06
diff -urNp old/agent/mibgroup/mibII/ipAddr.c new/agent/mibgroup/mibII/ipAddr.c
b5ae06
--- old/agent/mibgroup/mibII/ipAddr.c	2012-10-10 00:28:58.000000000 +0200
b5ae06
+++ new/agent/mibgroup/mibII/ipAddr.c	2017-04-04 13:28:56.547268946 +0200
b5ae06
@@ -493,14 +493,16 @@ Address_Scan_Next(Index, Retin_ifaddr)
b5ae06
 }
b5ae06
 
b5ae06
 #elif defined(linux)
b5ae06
+#include <errno.h>
b5ae06
 static struct ifreq *ifr;
b5ae06
 static int ifr_counter;
b5ae06
 
b5ae06
 static void
b5ae06
 Address_Scan_Init(void)
b5ae06
 {
b5ae06
-    int num_interfaces = 0;
b5ae06
+    int i;
b5ae06
     int fd;
b5ae06
+    int lastlen = 0;
b5ae06
 
b5ae06
     /* get info about all interfaces */
b5ae06
 
b5ae06
@@ -508,30 +510,45 @@ Address_Scan_Init(void)
b5ae06
     SNMP_FREE(ifc.ifc_buf);
b5ae06
     ifr_counter = 0;
b5ae06
 
b5ae06
-    do
b5ae06
+    if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
b5ae06
     {
b5ae06
-	if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
b5ae06
-	{
b5ae06
-	    DEBUGMSGTL(("snmpd", "socket open failure in Address_Scan_Init\n"));
b5ae06
-	    return;
b5ae06
-	}
b5ae06
-	num_interfaces += 16;
b5ae06
-
b5ae06
-	ifc.ifc_len = sizeof(struct ifreq) * num_interfaces;
b5ae06
-	ifc.ifc_buf = (char*) realloc(ifc.ifc_buf, ifc.ifc_len);
b5ae06
-	
b5ae06
-	    if (ioctl(fd, SIOCGIFCONF, &ifc) < 0)
b5ae06
-	    {
b5ae06
-		ifr=NULL;
b5ae06
-		close(fd);
b5ae06
-	   	return;
b5ae06
-	    }
b5ae06
-	    close(fd);
b5ae06
+        DEBUGMSGTL(("snmpd", "socket open failure in Address_Scan_Init\n"));
b5ae06
+        return;	   
b5ae06
+    }
b5ae06
+
b5ae06
+    /*
b5ae06
+     * Cope with lots of interfaces and brokenness of ioctl SIOCGIFCONF
b5ae06
+     * on some platforms; see W. R. Stevens, ``Unix Network Programming
b5ae06
+     * Volume I'', p.435...
b5ae06
+     */
b5ae06
+
b5ae06
+    for (i = 8;; i *= 2) {
b5ae06
+        ifc.ifc_len = sizeof(struct ifreq) * i;
b5ae06
+        ifc.ifc_req = calloc(i, sizeof(struct ifreq));
b5ae06
+
b5ae06
+        if (ioctl(fd, SIOCGIFCONF, &ifc) < 0) {
b5ae06
+            if (errno != EINVAL || lastlen != 0) {
b5ae06
+                /*
b5ae06
+                 * Something has gone genuinely wrong...
b5ae06
+                 */
b5ae06
+                snmp_log(LOG_ERR, "bad rc from ioctl, errno %d", errno);
b5ae06
+                SNMP_FREE(ifc.ifc_buf);
b5ae06
+                close(fd);
b5ae06
+                return;
b5ae06
+            }
b5ae06
+        } else {
b5ae06
+            if (ifc.ifc_len == lastlen) {
b5ae06
+                /*
b5ae06
+                 * The length is the same as the last time; we're done...
b5ae06
+                 */
b5ae06
+                break;
b5ae06
+            }
b5ae06
+            lastlen = ifc.ifc_len;
b5ae06
+        }
b5ae06
+        free(ifc.ifc_buf); /* no SNMP_FREE, getting ready to reassign */
b5ae06
     }
b5ae06
-    while (ifc.ifc_len >= (sizeof(struct ifreq) * num_interfaces));
b5ae06
-    
b5ae06
-    ifr = ifc.ifc_req;
b5ae06
     close(fd);
b5ae06
+    ifr = ifc.ifc_req;
b5ae06
 }
b5ae06
 
b5ae06
 /*