[Click] Randomness in NSClick

Sascha Alexander Jopen jopen at informatik.uni-bonn.de
Mon Jul 9 06:13:53 EDT 2012


Hey,

Sometime ago i proposed a connection of the random number generators
provided by ns3 with the click_random() call, see
http://www.mail-archive.com/click@amsterdam.lcs.mit.edu/msg05657.html

The attached patches implement a new simclick command to retrieve a
random number from the simulator. The first call of click_random checks
for support of the new command by the simulator. Until now there is no
fallback in case of lack of random number support by the simulator. This
is needed for ns2 functionality as long as this command is not
implemented in ns2.

For now i use a single ns3::UniformVariable to draw random numbers from.
This corresponds to the old behaviour of click_random(). It is easily
possible to change this to one ns3::UniformVariable per click instance.
I have no clue, if this is statistically relevant.


click-ns3-rng.diff adds support on the click side, while
ns3-nsclick-rng.diff is for ns3 side. Both are diffs against current
git/mercurial tips.

Maybe someone can test and comment on this?

Regards,
Sascha
-------------- next part --------------
diff --git a/include/click/glue.hh b/include/click/glue.hh
index a908496..eed522b 100644
--- a/include/click/glue.hh
+++ b/include/click/glue.hh
@@ -70,6 +70,7 @@ CLICK_CXX_UNPROTECT
 # include <sys/time.h>
 # if CLICK_NS
 extern "C" int simclick_gettimeofday(struct timeval *);
+extern "C" uint32_t simclick_random();
 # endif
 # if HAVE_MULTITHREAD
 #  include <pthread.h>
@@ -132,6 +133,8 @@ void click_random_srandom();
 
 #if CLICK_BSDMODULE
 # define CLICK_RAND_MAX 0x7FFFFFFFU
+#elif CLICK_NS
+# define CLICK_RAND_MAX 0xFFFFFFFEU
 #elif !CLICK_LINUXMODULE && RAND_MAX >= 0x7FFFFFFFU
 # define CLICK_RAND_MAX RAND_MAX
 #else
@@ -145,6 +148,8 @@ inline uint32_t click_random() {
 #elif CLICK_LINUXMODULE
     click_random_seed = click_random_seed * 69069L + 5;
     return (click_random_seed ^ jiffies) & CLICK_RAND_MAX; // XXX jiffies??
+#elif CLICK_NS
+    return simclick_random();
 #elif HAVE_RANDOM && CLICK_RAND_MAX == RAND_MAX
     return random();
 #else
@@ -155,6 +160,9 @@ inline uint32_t click_random() {
 inline void click_srandom(uint32_t seed) {
 #if CLICK_BSDMODULE
     srandom(seed);
+#elif CLICK_NS
+    // do nothing, seeding is done by the ns seed manager
+    (void) seed;
 #elif !CLICK_LINUXMODULE && HAVE_RANDOM && CLICK_RAND_MAX == RAND_MAX
     srandom(seed);
 #elif !CLICK_LINUXMODULE && CLICK_RAND_MAX == RAND_MAX
diff --git a/include/click/simclick.h b/include/click/simclick.h
index 972a977..863d827 100644
--- a/include/click/simclick.h
+++ b/include/click/simclick.h
@@ -106,6 +106,7 @@ int simclick_gettimeofday(struct timeval* tv);
 #define SIMCLICK_CHANGE_CHANNEL		11 // int ifid, int channelid
 #define SIMCLICK_IF_PROMISC		12 // int ifid
 #define SIMCLICK_IPPREFIX_FROM_NAME	13 // const char *ifname, char *buf, int len
+#define SIMCLICK_RANDOM				14 // none
 
 int simclick_sim_command(simclick_node_t *sim, int cmd, ...);
 int simclick_click_command(simclick_node_t *sim, int cmd, ...);
diff --git a/ns/nsclick.cc b/ns/nsclick.cc
index 1e00b32..a2b54fc 100644
--- a/ns/nsclick.cc
+++ b/ns/nsclick.cc
@@ -68,6 +68,7 @@ CLICK_USING_DECLS
 
 
 static simclick_node_t *cursimnode = NULL;
+static char sim_rng_support = simclick_click_command(NULL, SIMCLICK_SUPPORTS, SIMCLICK_RANDOM);
 
 //
 // XXX
@@ -153,6 +154,23 @@ int simclick_gettimeofday(struct timeval* tv) {
   }
 }
 
+uint32_t simclick_random() {
+	static bool sim_rng_support = simclick_sim_command(NULL, SIMCLICK_SUPPORTS, SIMCLICK_RANDOM);
+
+	if (sim_rng_support) {
+		if (cursimnode) {
+			uint32_t random_number;
+			simclick_sim_command(cursimnode, SIMCLICK_RANDOM, &random_number);
+			return random_number;
+		} else {
+		    click_chatter("simclick_random: call without simstate set");
+		}
+	} else {
+	    click_chatter("simclick_random: simulator has no random number support");
+	}
+	return 0;
+}
+
 int simclick_click_send(simclick_node_t *simnode,
 			int ifid,int type,const unsigned char* data,int len,
 			simclick_simpacketinfo* pinfo) {
-------------- next part --------------
diff -r f43017db07ec src/click/model/ipv4-click-routing.cc
--- a/src/click/model/ipv4-click-routing.cc	Mon Jun 25 11:52:17 2012 -0700
+++ b/src/click/model/ipv4-click-routing.cc	Mon Jul 09 11:10:39 2012 +0200
@@ -27,6 +27,7 @@
 #include "ns3/mac48-address.h"
 #include "ns3/ipv4-interface.h"
 #include "ns3/ipv4-l3-click-protocol.h"
+#include "ns3/random-variable.h"
 
 #include "ipv4-click-routing.h"
 #include <string>
@@ -47,6 +48,7 @@
 NS_OBJECT_ENSURE_REGISTERED (Ipv4ClickRouting);
 
 std::map < simclick_node_t *, Ptr<Ipv4ClickRouting> > Ipv4ClickRouting::m_clickInstanceFromSimNode;
+static UniformVariable g_random;
 
 TypeId
 Ipv4ClickRouting::GetTypeId (void)
@@ -606,7 +608,7 @@
     case SIMCLICK_SUPPORTS:
       {
         int othercmd = va_arg (val, int);
-        retval = (othercmd >= SIMCLICK_VERSION && othercmd <= SIMCLICK_GET_NODE_ID);
+        retval = (othercmd >= SIMCLICK_VERSION && othercmd <= SIMCLICK_RANDOM);
         break;
       }
 
@@ -738,6 +740,16 @@
         NS_LOG_DEBUG (clickInstance->GetNodeName () << " Received a call for SIMCLICK_GET_NODE_ID");
         break;
       }
+
+    case SIMCLICK_RANDOM:
+      {
+        uint32_t *random_value = va_arg (val, uint32_t *);
+
+        *random_value = ns3::g_random.GetInteger (0, 0xFFFFFFFEU);
+        retval = 0;
+        NS_LOG_DEBUG (clickInstance->GetNodeName () << " SIMCLICK_RANDOM: " << *random_value << " " << ns3::Simulator::Now ());
+        break;
+      }
     }
   return retval;
 }


More information about the click mailing list