WAF: add a --command-template option to e.g. allow running programs with valgrind, gdb, etc.
authorGustavo J. A. M. Carneiro <gjc@inescporto.pt>
Wed, 18 Jul 2007 11:43:39 +0100
changeset 933 df68dad55087
parent 932 249039a9ce73
child 934 1bfcc65213c7
WAF: add a --command-template option to e.g. allow running programs with valgrind, gdb, etc.
doc/build.txt
src/internet-node/arp-cache.cc
src/internet-node/arp-ipv4-interface.cc
src/internet-node/arp-l3-protocol.cc
src/internet-node/ipv4-l3-protocol.cc
src/node/ipv4-address.cc
src/node/ipv4-address.h
wscript
--- a/doc/build.txt	Mon Jul 16 22:49:48 2007 -0700
+++ b/doc/build.txt	Wed Jul 18 11:43:39 2007 +0100
@@ -60,6 +60,15 @@
     before running the program.  Note: the "program [args]" string is
     parsed using POSIX shell rules.
 
+ 4.1 waf --run programname --command-template "... %s ..."
+
+    Same as --run, but uses a command template with %s replaced by the
+    actual program (whose name is given by --run).  This can be use to
+    run ns-3 programs with helper tools.  For example, to run unit
+    tests with valgrind, use the command:
+
+         waf --run run-tests --command-template "valgrind %s"
+
  5. waf --shell
     Starts a nested system shell with modified environment to run ns3 programs.
 
--- a/src/internet-node/arp-cache.cc	Mon Jul 16 22:49:48 2007 -0700
+++ b/src/internet-node/arp-cache.cc	Wed Jul 18 11:43:39 2007 +0100
@@ -109,6 +109,8 @@
 ArpCache::Entry *
 ArpCache::Add (Ipv4Address to)
 {
+  NS_ASSERT (m_arpCache.find (to) == m_arpCache.end ());
+
   ArpCache::Entry *entry = new ArpCache::Entry (this);
   m_arpCache[to] = entry;  
   return entry;
--- a/src/internet-node/arp-ipv4-interface.cc	Mon Jul 16 22:49:48 2007 -0700
+++ b/src/internet-node/arp-ipv4-interface.cc	Wed Jul 18 11:43:39 2007 +0100
@@ -21,6 +21,7 @@
  */
 
 #include "ns3/packet.h"
+#include "ns3/debug.h"
 #include "ns3/composite-trace-resolver.h"
 #include "ns3/node.h"
 #include "ns3/net-device.h"
@@ -60,7 +61,19 @@
     {
       Ptr<ArpPrivate> arp = m_node->QueryInterface<ArpPrivate> (ArpPrivate::iid);
       MacAddress hardwareDestination;
-      bool found = arp->Lookup (p, dest, GetDevice (), &hardwareDestination);
+      bool found;
+
+      if (dest.IsBroadcast ())
+        {
+           hardwareDestination = GetDevice ()->GetBroadcast ();
+           found = true;
+        }
+      else
+        {
+          Ptr<ArpPrivate> arp = m_node->QueryInterface<ArpPrivate> (ArpPrivate::iid);
+          found = arp->Lookup (p, dest, GetDevice (), &hardwareDestination);
+        }
+
       if (found)
         {
           GetDevice ()->Send (p, hardwareDestination, Ipv4L3Protocol::PROT_NUMBER);
--- a/src/internet-node/arp-l3-protocol.cc	Mon Jul 16 22:49:48 2007 -0700
+++ b/src/internet-node/arp-l3-protocol.cc	Wed Jul 18 11:43:39 2007 +0100
@@ -87,6 +87,13 @@
   ArpCache *cache = FindCache (device);
   ArpHeader arp;
   packet.RemoveHeader (arp);
+  
+  NS_DEBUG ("ARP: received "<< (arp.IsRequest ()? "request" : "reply") <<
+            " node="<<m_node->GetId ()<<", got request from " <<
+            arp.GetSourceIpv4Address () << " for address " <<
+            arp.GetDestinationIpv4Address () << "; we have address " <<
+            cache->GetInterface ()->GetAddress ());
+
   if (arp.IsRequest () && 
       arp.GetDestinationIpv4Address () == cache->GetInterface ()->GetAddress ()) 
     {
@@ -128,6 +135,12 @@
 	  // XXX report packet as dropped.
         }
     }
+  else
+    {
+      NS_DEBUG ("node="<<m_node->GetId ()<<", got request from " <<
+                arp.GetSourceIpv4Address () << " for unknown address " <<
+                arp.GetDestinationIpv4Address () << " -- drop");
+    }
 }
 bool 
 ArpL3Protocol::Lookup (Packet &packet, Ipv4Address destination, 
@@ -203,6 +216,11 @@
 ArpL3Protocol::SendArpRequest (ArpCache const *cache, Ipv4Address to)
 {
   ArpHeader arp;
+  NS_DEBUG ("ARP: sending request from node "<<m_node->GetId ()<<
+            " || src: " << cache->GetDevice ()->GetAddress () <<
+            " / " << cache->GetInterface ()->GetAddress () <<
+            " || dst: " << cache->GetDevice ()->GetBroadcast () <<
+            " / " << to);
   arp.SetRequest (cache->GetDevice ()->GetAddress (),
 		  cache->GetInterface ()->GetAddress (), 
                   cache->GetDevice ()->GetBroadcast (),
@@ -216,6 +234,10 @@
 ArpL3Protocol::SendArpReply (ArpCache const *cache, Ipv4Address toIp, MacAddress toMac)
 {
   ArpHeader arp;
+  NS_DEBUG ("ARP: sending reply from node "<<m_node->GetId ()<<
+            "|| src: " << cache->GetDevice ()->GetAddress () << 
+            " / " << cache->GetInterface ()->GetAddress () <<
+            " || dst: " << toMac << " / " << toIp);
   arp.SetReply (cache->GetDevice ()->GetAddress (),
                 cache->GetInterface ()->GetAddress (),
                 toMac, toIp);
--- a/src/internet-node/ipv4-l3-protocol.cc	Mon Jul 16 22:49:48 2007 -0700
+++ b/src/internet-node/ipv4-l3-protocol.cc	Wed Jul 18 11:43:39 2007 +0100
@@ -404,23 +404,40 @@
 
   m_identification ++;
 
-  // XXX Note here that in most ipv4 stacks in the world,
-  // the route calculation for an outgoing packet is not
-  // done in the ip layer. It is done within the application
-  // socket when the first packet is sent to avoid this
-  // costly lookup on a per-packet basis.
-  // That would require us to get the route from the packet,
-  // most likely with a packet tag. The higher layers do not
-  // do this yet for us.
-  Ipv4Route *route = Lookup (ipHeader.GetDestination ());
-  if (route == 0) 
+  if (destination.IsBroadcast ())
+    {
+      uint32_t ifaceIndex = 0;
+      for (Ipv4InterfaceList::iterator ifaceIter = m_interfaces.begin ();
+           ifaceIter != m_interfaces.end (); ifaceIter++, ifaceIndex++)
+        {
+          Ipv4Interface *outInterface = *ifaceIter;
+          Packet packetCopy = packet;
+
+          NS_ASSERT (packetCopy.GetSize () <= outInterface->GetMtu ());
+          packetCopy.AddHeader (ipHeader);
+          m_txTrace (packetCopy, ifaceIndex);
+          outInterface->Send (packetCopy, destination);
+        }
+    }
+  else
     {
-      NS_DEBUG ("not for me -- forwarding but no route to host. drop.");
-      m_dropTrace (packet);
-      return;
+      // XXX Note here that in most ipv4 stacks in the world,
+      // the route calculation for an outgoing packet is not
+      // done in the ip layer. It is done within the application
+      // socket when the first packet is sent to avoid this
+      // costly lookup on a per-packet basis.
+      // That would require us to get the route from the packet,
+      // most likely with a packet tag. The higher layers do not
+      // do this yet for us.
+      Ipv4Route *route = Lookup (ipHeader.GetDestination ());
+      if (route == 0) 
+        {
+          NS_DEBUG ("not for me -- forwarding but no route to host. drop.");
+          m_dropTrace (packet);
+          return;
+        }
+      SendRealOut (packet, ipHeader, *route);
     }
-
-  SendRealOut (packet, ipHeader, *route);
 }
 
 void
@@ -470,7 +487,7 @@
 	}
     }
       
-  if (ipHeader.GetDestination ().IsEqual (Ipv4Address::GetBroadcast ())) 
+  if (ipHeader.GetDestination ().IsBroadcast ()) 
     {
       NS_DEBUG ("for me 3");
       return false;
--- a/src/node/ipv4-address.cc	Mon Jul 16 22:49:48 2007 -0700
+++ b/src/node/ipv4-address.cc	Wed Jul 18 11:43:39 2007 +0100
@@ -145,6 +145,18 @@
   }
 }
 
+bool
+Ipv4Address::IsBroadcast (void) const
+{
+  return (m_address == 0xffffffffU);
+}
+
+Ipv4Address
+Ipv4Address::CombineMask (Ipv4Mask const &mask) const
+{
+  return Ipv4Address (GetHostOrder () & mask.GetHostOrder ());
+}
+
 bool 
 Ipv4Address::IsMulticast (void)
 {
--- a/src/node/ipv4-address.h	Mon Jul 16 22:49:48 2007 -0700
+++ b/src/node/ipv4-address.h	Wed Jul 18 11:43:39 2007 +0100
@@ -27,6 +27,8 @@
 
 namespace ns3 {
 
+class Ipv4Mask;
+
 /** Ipv4 addresses are stored in host order in
   * this class.
   */
@@ -80,8 +82,19 @@
    */
   void Print (std::ostream &os) const;
 
-  bool IsBroadcast (void);
+  bool IsBroadcast (void) const;
   bool IsMulticast (void);
+  
+  /**
+   * \brief Combine this address with a network mask
+   *
+   * This method returns an IPv4 address that is this address combined
+   * (bitwise and) with a network mask, yielding an IPv4 network
+   * address.
+   *
+   * \param a network mask 
+   */
+  Ipv4Address CombineMask (Ipv4Mask const &mask) const;
 
   static Ipv4Address GetZero (void);
   static Ipv4Address GetAny (void);
--- a/wscript	Mon Jul 16 22:49:48 2007 -0700
+++ b/wscript	Wed Jul 18 11:43:39 2007 +0100
@@ -1,5 +1,4 @@
 ## -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
-import os
 import sys
 import shlex
 import shutil
@@ -66,8 +65,14 @@
                    dest='doxygen')
 
     opt.add_option('--run',
-                   help=('Run a locally built program'),
+                   help=('Run a locally built program; argument can be a program name,'
+                         ' or a command starting with the program name.'),
                    type="string", default='', dest='run')
+    opt.add_option('--command-template',
+                   help=('Template of the command used to run the program given by --run;'
+                         ' It should be a shell command string containing %s inside,'
+                         ' which will be replaced by the actual program.'),
+                   type="string", default=None, dest='command_template')
 
     opt.add_option('--shell',
                    help=('Run a shell with an environment suitably modified to run locally built programs'),
@@ -163,7 +168,11 @@
         doxygen()
 
     if Params.g_options.run:
-        run_program(Params.g_options.run)
+        run_program(Params.g_options.run, Params.g_options.command_template)
+        raise SystemExit(0)
+
+    if Params.g_options.command_template:
+        Params.fatal("Option --command-template requires the option --run to be given")
 
 def _find_program(program_name, env):
     launch_dir = os.path.abspath(Params.g_cwd_launch)
@@ -212,33 +221,58 @@
 
     retval = subprocess.Popen(argv, env=os_env).wait()
     if retval:
-        raise SystemExit(retval)
+        Params.fatal("Command %s exited with code %i" % (argv, retval))
 
 
-def run_program(program_string):
+def run_program(program_string, command_template=None):
+    """
+    if command_template is not None, then program_string == program
+    name and argv is given by command_template with %s replaced by the
+    full path to the program.  Else, program_string is interpreted as
+    a shell command with first name being the program name.
+    """
     env = Params.g_build.env_of_name('default')
-    argv = shlex.split(program_string)
-    program_name = argv[0]
+
+    if command_template is None:
+        argv = shlex.split(program_string)
+        program_name = argv[0]
+
+        try:
+            program_obj = _find_program(program_name, env)
+        except ValueError, ex:
+            Params.fatal(str(ex))
 
-    try:
-        program_obj = _find_program(program_name, env)
-    except ValueError, ex:
-        Params.fatal(str(ex))
+        try:
+            program_node, = program_obj.m_linktask.m_outputs
+        except AttributeError:
+            Params.fatal("%s does not appear to be a program" % (program_name,))
+
+        execvec = [program_node.abspath(env)] + argv[1:]
+
+    else:
 
-    try:
-        program_node, = program_obj.m_linktask.m_outputs
-    except AttributeError:
-        Params.fatal("%s does not appear to be a program" % (program_name,))
+        program_name = program_string
+        try:
+            program_obj = _find_program(program_name, env)
+        except ValueError, ex:
+            Params.fatal(str(ex))
+        try:
+            program_node, = program_obj.m_linktask.m_outputs
+        except AttributeError:
+            Params.fatal("%s does not appear to be a program" % (program_name,))
 
-    execvec = [program_node.abspath(env)] + argv[1:]
+        execvec = shlex.split(command_template % (program_node.abspath(env),))
+
 
     former_cwd = os.getcwd()
     os.chdir(Params.g_cwd_launch)
     try:
-        return _run_argv(execvec)
+        retval = _run_argv(execvec)
     finally:
         os.chdir(former_cwd)
 
+    return retval
+
 
 def run_shell():
     if sys.platform == 'win32':