WAF: add a --command-template option to e.g. allow running programs with valgrind, gdb, etc.
parent
52049833fd
commit
a694ccb39e
|
@ -60,6 +60,15 @@ with --enable-gcov)
|
|||
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.
|
||||
|
||||
|
|
|
@ -109,6 +109,8 @@ ArpCache::Lookup (Ipv4Address to)
|
|||
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;
|
||||
|
|
|
@ -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 @@ ArpIpv4Interface::SendTo (Packet p, Ipv4Address dest)
|
|||
{
|
||||
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);
|
||||
|
|
|
@ -87,6 +87,13 @@ ArpL3Protocol::Receive(Packet& packet, Ptr<NetDevice> device)
|
|||
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 @@ ArpL3Protocol::Receive(Packet& packet, Ptr<NetDevice> device)
|
|||
// 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 @@ void
|
|||
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 @@ void
|
|||
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);
|
||||
|
|
|
@ -404,23 +404,40 @@ Ipv4L3Protocol::Send (Packet const &packet,
|
|||
|
||||
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 ())
|
||||
{
|
||||
NS_DEBUG ("not for me -- forwarding but no route to host. drop.");
|
||||
m_dropTrace (packet);
|
||||
return;
|
||||
}
|
||||
uint32_t ifaceIndex = 0;
|
||||
for (Ipv4InterfaceList::iterator ifaceIter = m_interfaces.begin ();
|
||||
ifaceIter != m_interfaces.end (); ifaceIter++, ifaceIndex++)
|
||||
{
|
||||
Ipv4Interface *outInterface = *ifaceIter;
|
||||
Packet packetCopy = packet;
|
||||
|
||||
SendRealOut (packet, ipHeader, *route);
|
||||
NS_ASSERT (packetCopy.GetSize () <= outInterface->GetMtu ());
|
||||
packetCopy.AddHeader (ipHeader);
|
||||
m_txTrace (packetCopy, ifaceIndex);
|
||||
outInterface->Send (packetCopy, destination);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
|
@ -470,7 +487,7 @@ Ipv4L3Protocol::Forwarding (Packet const &packet, Ipv4Header &ipHeader, Ptr<NetD
|
|||
}
|
||||
}
|
||||
|
||||
if (ipHeader.GetDestination ().IsEqual (Ipv4Address::GetBroadcast ()))
|
||||
if (ipHeader.GetDestination ().IsBroadcast ())
|
||||
{
|
||||
NS_DEBUG ("for me 3");
|
||||
return false;
|
||||
|
|
|
@ -145,6 +145,18 @@ Ipv4Address::IsEqual (Ipv4Address other) const
|
|||
}
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
|
|
|
@ -27,6 +27,8 @@
|
|||
|
||||
namespace ns3 {
|
||||
|
||||
class Ipv4Mask;
|
||||
|
||||
/** Ipv4 addresses are stored in host order in
|
||||
* this class.
|
||||
*/
|
||||
|
@ -80,8 +82,19 @@ public:
|
|||
*/
|
||||
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);
|
||||
|
|
68
wscript
68
wscript
|
@ -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 @@ def set_options(opt):
|
|||
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 @@ def shutdown():
|
|||
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 @@ def _run_argv(argv):
|
|||
|
||||
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]
|
||||
|
||||
try:
|
||||
program_obj = _find_program(program_name, env)
|
||||
except ValueError, ex:
|
||||
Params.fatal(str(ex))
|
||||
if command_template is None:
|
||||
argv = shlex.split(program_string)
|
||||
program_name = argv[0]
|
||||
|
||||
try:
|
||||
program_node, = program_obj.m_linktask.m_outputs
|
||||
except AttributeError:
|
||||
Params.fatal("%s does not appear to be a program" % (program_name,))
|
||||
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:
|
||||
|
||||
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 = shlex.split(command_template % (program_node.abspath(env),))
|
||||
|
||||
execvec = [program_node.abspath(env)] + argv[1:]
|
||||
|
||||
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':
|
||||
|
|
Loading…
Reference in New Issue