[Click] Click defines in ns3

Sascha Alexander Jopen jopen at informatik.uni-bonn.de
Mon Jan 21 06:30:06 EST 2013


Hi again,

Both for userlevel and linuxmodule click drivers, it is possible to set
defines for the click configurations with commandline options. The
attached patchset adds this ability to the ns3 click driver, too. It is
possible to set the defines for each click node within ns3 by

Ipv4ClickRouting::SetDefines (std::map<std::string, std::string> defines);

with defines being a key/value mapping for the defines to be set.

click-ns3-defines.diff is a small patch for click side, adding a new
function to create a click node instance with defines set.

ns3-nsclick-defines.diff adds support to the ns3 simulator side.

ns2 should still run without modification. It simply doesn't use the new
function, until someone provides a patch.

If this extension is interesting to you, please review the patch and
apply it if its ok.

Regards,
Sascha

-- 
Dipl.-Inform. Sascha Jopen

University of Bonn                     Tel.:   +49-228-73-54219
Institute of Computer Science 4        Fax:    +49-228-73-4571
Friedrich-Ebert-Allee 144              E-mail: jopen at cs.uni-bonn.de
D-53113 Bonn, Germany
-------------- next part --------------
diff --git a/include/click/simclick.h b/include/click/simclick.h
index 972a977..5d896fa 100644
--- a/include/click/simclick.h
+++ b/include/click/simclick.h
@@ -48,6 +48,10 @@ typedef struct simclick_node {
 
 int simclick_click_create(simclick_node_t *sim, const char *router_file);
 
+int simclick_click_create_defines(simclick_node_t *simnode,
+	const char* router_file,
+	const char **defines);
+
 int simclick_click_send(simclick_node_t *sim,
 			int ifid,int type,const unsigned char* data,int len,
 			simclick_simpacketinfo* pinfo);
diff --git a/ns/nsclick.cc b/ns/nsclick.cc
index 1e00b32..de28bc2 100644
--- a/ns/nsclick.cc
+++ b/ns/nsclick.cc
@@ -79,20 +80,25 @@ static void setsimstate(simclick_node_t *newstate) {
     cursimnode = newstate;
 }
 
+static void init() {
+    static bool didinit = false;
+
+    if (!didinit) {
+    	click_static_initialize();
+    	didinit = true;
+    }
+}
+
 // functions for packages
 
 
 extern "C" {
 
 int simclick_click_create(simclick_node_t *simnode, const char* router_file) {
-    static bool didinit = false;
 
     setsimstate(simnode);
 
-    if (!didinit) {
-	click_static_initialize();
-	didinit = true;
-    }
+    init();
 
     bool warnings = true;
 
@@ -114,6 +120,21 @@ int simclick_click_create(simclick_node_t *simnode, const char* router_file) {
     return 0;
 }
 
+int simclick_click_create_defines(simclick_node_t *simnode, const char* router_file, const char **defines) {
+	setsimstate(simnode);
+	init();
+	if (defines) {
+		while (*defines) {
+			ErrorHandler *errh = ErrorHandler::default_handler();
+			if (!click_lexer()->global_scope().define(*defines, *(defines + 1), false)) {
+				errh->error("parameter %s multiply defined", *defines);
+			}
+			defines += 2;
+		}
+	}
+	return simclick_click_create(simnode, router_file);
+}
+
 /*
  * XXX Need to actually implement this a little more intelligenetly...
  */
-------------- next part --------------
diff -r 22bd9c407531 src/click/helper/click-internet-stack-helper.cc
--- a/src/click/helper/click-internet-stack-helper.cc	Mon Jan 21 10:43:46 2013 +0100
+++ b/src/click/helper/click-internet-stack-helper.cc	Mon Jan 21 10:46:30 2013 +0100
@@ -125,6 +125,21 @@
 }
 
 void
+ClickInternetStackHelper::SetDefines (NodeContainer c, std::map<std::string, std::string> defines)
+{
+  for (NodeContainer::Iterator i = c.Begin (); i != c.End (); ++i)
+    {
+	  SetDefines (*i, defines);
+    }
+}
+
+void
+ClickInternetStackHelper::SetDefines (Ptr<Node> node, std::map<std::string, std::string> defines)
+{
+  m_nodeToDefinesMap.insert (std::make_pair (node, defines));
+}
+
+void
 ClickInternetStackHelper::SetRoutingTableElement (NodeContainer c, std::string rt)
 {
   for (NodeContainer::Iterator i = c.Begin (); i != c.End (); ++i)
@@ -193,6 +208,13 @@
           ipv4Routing->SetClickFile (it->second);
         }
 
+      std::map<Ptr<Node>, std::map<std::string, std::string> >::const_iterator definesIt;
+      definesIt = m_nodeToDefinesMap.find (node);
+      if (definesIt != m_nodeToDefinesMap.end ())
+        {
+          ipv4Routing->SetDefines (definesIt->second);
+        }
+
       it = m_nodeToRoutingTableElementMap.find (node);
       if (it != m_nodeToRoutingTableElementMap.end ())
         {
diff -r 22bd9c407531 src/click/helper/click-internet-stack-helper.h
--- a/src/click/helper/click-internet-stack-helper.h	Mon Jan 21 10:43:46 2013 +0100
+++ b/src/click/helper/click-internet-stack-helper.h	Mon Jan 21 10:46:30 2013 +0100
@@ -143,6 +143,20 @@
   void SetClickFile (Ptr<Node> node, std::string clickfile);
 
   /**
+   * \brief Set defines to be used for a group of nodes.
+   * \param c NodeContainer of nodes
+   * \param defines Defines mapping to be used
+   */
+  void SetDefines (NodeContainer c, std::map<std::string, std::string> defines);
+
+  /**
+   * \brief Set defines to be used for a node.
+   * \param node Node for which the defines are to be set
+   * \param defines Defines mapping to be used
+   */
+  void SetDefines (Ptr<Node> node, std::map<std::string, std::string> defines);
+
+  /**
    * \brief Set a Click routing table element for a group of nodes.
    * \param c NodeContainer of nodes
    * \param rt Click Routing Table element name
@@ -219,6 +233,11 @@
   std::map < Ptr<Node>, std::string  > m_nodeToClickFileMap;
 
   /**
+   * \brief Node to Click defines mapping
+   */
+  std::map < Ptr<Node>, std::map<std::string, std::string>  > m_nodeToDefinesMap;
+
+  /**
    * \brief Node to Routing Table Element mapping
    */
   std::map < Ptr<Node>, std::string  > m_nodeToRoutingTableElementMap;
diff -r 22bd9c407531 src/click/model/ipv4-click-routing.cc
--- a/src/click/model/ipv4-click-routing.cc	Mon Jan 21 10:43:46 2013 +0100
+++ b/src/click/model/ipv4-click-routing.cc	Mon Jan 21 10:46:30 2013 +0100
@@ -90,9 +90,23 @@
 
   NS_ASSERT (m_clickFile.length () > 0);
 
+  // Create NULL-terminated array with click defines
+  const char **defines = new const char*[(m_defines.size () + 1) * 2];
+  unsigned int i = 0;
+  std::map<std::string, std::string>::const_iterator it = m_defines.begin ();
+  while (it != m_defines.end ())
+  {
+    defines[i] = it->first.c_str ();
+    defines[i+1] = it->second.c_str ();
+    i += 2;
+    it++;
+  }
+  defines[i] = 0;
+  defines[i+1] = 0;
+
   // Even though simclick_click_create() will halt programme execution
   // if it is unable to initialise a Click router, we play safe
-  if (simclick_click_create (m_simNode, m_clickFile.c_str ()) >= 0)
+  if (simclick_click_create_defines (m_simNode, m_clickFile.c_str (), defines) >= 0)
     {
       NS_LOG_DEBUG (m_nodeName << " has initialised a Click Router");
       m_clickInitialised = true;
@@ -103,6 +117,8 @@
       m_clickInitialised = false;
     }
 
+  delete[] defines;
+
   NS_ASSERT (m_clickInitialised == true);
   simclick_click_run (m_simNode);
 }
@@ -138,6 +154,12 @@
 }
 
 void
+Ipv4ClickRouting::SetDefines (std::map<std::string, std::string> defines)
+{
+  m_defines = defines;
+}
+
+void
 Ipv4ClickRouting::SetClickRoutingTableElement (std::string name)
 {
   m_clickRoutingTableElement = name;
diff -r 22bd9c407531 src/click/model/ipv4-click-routing.h
--- a/src/click/model/ipv4-click-routing.h	Mon Jan 21 10:43:46 2013 +0100
+++ b/src/click/model/ipv4-click-routing.h	Mon Jan 21 10:46:30 2013 +0100
@@ -78,6 +78,12 @@
   void SetClickFile (std::string clickfile);
 
   /**
+  * \brief Click defines to be used by the node's Click Instance.
+  * \param defines mapping of defines for .click configuration file parsing
+  */
+  void SetDefines (std::map<std::string, std::string> defines);
+
+  /**
    * \brief Name of the node as to be used by Click. Required for Click Dumps.
    * \param name Name to be assigned to the node.
    */
@@ -246,6 +252,7 @@
 
 private:
   std::string m_clickFile;
+  std::map < std::string, std::string > m_defines;
   std::string m_nodeName;
   std::string m_clickRoutingTableElement;
 


More information about the click mailing list