[PATCH 2/2] lib: Add random allocation of bitmap identifiers

  • From: Dimitri Staessens <dimitri.staessens@xxxxxxxx>
  • To: ouroboros@xxxxxxxxxxxxx
  • Date: Fri, 19 Oct 2018 00:59:21 +0200

The bitmap now adds an allocate_random function that randomly assigns
an identifier. This can be disabled at build time (not recommended).
---
 include/ouroboros/bitmap.h |  2 ++
 src/irmd/main.c            |  4 ++--
 src/lib/CMakeLists.txt     |  2 ++
 src/lib/bitmap.c           | 28 ++++++++++++++++++++++++++++
 src/lib/config.h.in        |  1 +
 src/lib/dev.c              |  2 +-
 6 files changed, 36 insertions(+), 3 deletions(-)

diff --git a/include/ouroboros/bitmap.h b/include/ouroboros/bitmap.h
index 02e4de4..32e027d 100644
--- a/include/ouroboros/bitmap.h
+++ b/include/ouroboros/bitmap.h
@@ -36,6 +36,8 @@ void         bmp_destroy(struct bmp * bmp);
 
 ssize_t      bmp_allocate(struct bmp * bmp);
 
+ssize_t      bmp_allocate_random(struct bmp * bmp);
+
 int          bmp_release(struct bmp * bmp,
                          ssize_t      id);
 
diff --git a/src/irmd/main.c b/src/irmd/main.c
index d3f7a09..6432c9f 100644
--- a/src/irmd/main.c
+++ b/src/irmd/main.c
@@ -1281,7 +1281,7 @@ static int flow_alloc(pid_t              pid,
         }
 
         pthread_rwlock_wrlock(&irmd.flows_lock);
-        flow_id = bmp_allocate(irmd.flow_ids);
+        flow_id = bmp_allocate_random(irmd.flow_ids);
         if (!bmp_is_id_valid(irmd.flow_ids, flow_id)) {
                 pthread_rwlock_unlock(&irmd.flows_lock);
                 log_err("Could not allocate flow_id.");
@@ -1515,7 +1515,7 @@ static struct irm_flow * flow_req_arr(pid_t           pid,
 
         pthread_rwlock_unlock(&irmd.reg_lock);
         pthread_rwlock_wrlock(&irmd.flows_lock);
-        flow_id = bmp_allocate(irmd.flow_ids);
+        flow_id = bmp_allocate_random(irmd.flow_ids);
         if (!bmp_is_id_valid(irmd.flow_ids, flow_id)) {
                 pthread_rwlock_unlock(&irmd.flows_lock);
                 return NULL;
diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt
index 42164fa..2d06cdc 100644
--- a/src/lib/CMakeLists.txt
+++ b/src/lib/CMakeLists.txt
@@ -178,6 +178,8 @@ set(SHM_RBUFF_LOCKLESS 0 CACHE BOOL
   "Enable shared memory lockless rbuff support")
 set(QOS_DISABLE_CRC TRUE CACHE BOOL
   "Ignores ber setting on all QoS cubes")
+set(BITMAP_DISABLE_RANDOM FALSE CACHE BOOL
+  "Disable randomized allocation of identifiers")
 
 set(SOURCE_FILES_DEV
   # Add source files here
diff --git a/src/lib/bitmap.c b/src/lib/bitmap.c
index dfec08d..d0aef7e 100644
--- a/src/lib/bitmap.c
+++ b/src/lib/bitmap.c
@@ -20,7 +20,12 @@
  * Foundation, Inc., http://www.fsf.org/about/contact/.
  */
 
+#include "config.h"
+
 #include <ouroboros/bitmap.h>
+#ifndef BITMAP_DISABLE_RANDOM
+#include <ouroboros/random.h>
+#endif
 
 #include <assert.h>
 #include <stdlib.h>
@@ -153,6 +158,29 @@ ssize_t bmp_allocate(struct bmp * bmp)
         return id + bmp->offset;
 }
 
+ssize_t bmp_allocate_random(struct bmp * bmp)
+{
+        size_t id;
+
+        assert(bmp);
+
+#ifndef BITMAP_DISABLE_RANDOM
+        random_buffer(&id, sizeof(id));
+        id %= bmp->size;
+        if (bmp_is_id_used(bmp, id + bmp->offset)) {
+#endif
+                id = find_next_zero_bit(bmp->bitmap, bmp->size);
+                if (id >= bmp->size)
+                        return bad_id(bmp);
+#ifndef BITMAP_DISABLE_RANDOM
+        }
+#endif
+
+        bitmap_set(bmp->bitmap, id);
+
+        return id + bmp->offset;
+}
+
 static bool is_id_valid(struct bmp * bmp,
                         ssize_t      id)
 {
diff --git a/src/lib/config.h.in b/src/lib/config.h.in
index e8cfeba..9e58bec 100644
--- a/src/lib/config.h.in
+++ b/src/lib/config.h.in
@@ -29,6 +29,7 @@
 #cmakedefine                SHM_RBUFF_LOCKLESS
 #cmakedefine                SHM_RDRB_MULTI_BLOCK
 #cmakedefine                QOS_DISABLE_CRC
+#cmakedefine                BITMAP_DISABLE_RANDOM
 
 #define SHM_RBUFF_PREFIX    "@SHM_RBUFF_PREFIX@"
 #define SHM_LOCKFILE_NAME   "@SHM_LOCKFILE_NAME@"
diff --git a/src/lib/dev.c b/src/lib/dev.c
index 6dbb925..1fad6b9 100644
--- a/src/lib/dev.c
+++ b/src/lib/dev.c
@@ -280,7 +280,7 @@ static int flow_init(int       flow_id,
 
         pthread_rwlock_wrlock(&ai.lock);
 
-        fd = bmp_allocate(ai.fds);
+        fd = bmp_allocate_random(ai.fds);
         if (!bmp_is_id_valid(ai.fds, fd)) {
                 err = -EBADF;
                 goto fail_fds;
-- 
2.19.1


Other related posts: