manage NetDevice objects with refcounts

Mathieu Lacage 2007-05-02 10:25:06 +02:00
parent 5f7f31e7c9
commit 1eeab81136
14 changed files with 54 additions and 39 deletions

View File

@ -37,8 +37,10 @@ namespace ns3 {
// Application Methods
// \brief Application Constructor
Application::Application(const Node& n) : m_startVar(nil), m_stopVar(nil),
m_start(false), m_stop(false)
Application::Application(const Node& n)
: m_node (0),
m_startVar(nil), m_stopVar(nil),
m_start(false), m_stop(false)
{
SetNode(n);
}

View File

@ -34,15 +34,18 @@ ArpCache::ArpCache (NetDevice *device, Ipv4Interface *interface)
m_aliveTimeout (Seconds (120)),
m_deadTimeout (Seconds (100)),
m_waitReplyTimeout (Seconds (1))
{}
{
m_device->Ref ();
}
ArpCache::~ArpCache ()
{
m_device->Unref ();
Flush ();
}
NetDevice *
ArpCache::GetDevice (void) const
ArpCache::PeekDevice (void) const
{
return m_device;
}

View File

@ -24,6 +24,7 @@
#include <stdint.h>
#include "ns3/packet.h"
#include "ns3/nstime.h"
#include "net-device.h"
#include "ipv4-address.h"
#include "mac-address.h"
#include "sgi-hashmap.h"
@ -40,7 +41,7 @@ public:
ArpCache (NetDevice *device, Ipv4Interface *interface);
~ArpCache ();
NetDevice *GetDevice (void) const;
NetDevice *PeekDevice (void) const;
Ipv4Interface *GetInterface (void) const;
void SetAliveTimeout (Time aliveTimeout);

View File

@ -42,10 +42,10 @@ TraceResolver *
ArpIpv4Interface::DoCreateTraceResolver (TraceContext const &context)
{
CompositeTraceResolver *resolver = new CompositeTraceResolver (context);
if (GetDevice () != 0)
if (PeekDevice () != 0)
{
resolver->Add ("netdevice",
MakeCallback (&NetDevice::CreateTraceResolver, GetDevice ()),
MakeCallback (&NetDevice::CreateTraceResolver, PeekDevice ()),
ArpIpv4Interface::NETDEVICE);
}
@ -55,20 +55,20 @@ ArpIpv4Interface::DoCreateTraceResolver (TraceContext const &context)
void
ArpIpv4Interface::SendTo (Packet p, Ipv4Address dest)
{
NS_ASSERT (GetDevice () != 0);
if (GetDevice ()->NeedsArp ())
NS_ASSERT (PeekDevice () != 0);
if (PeekDevice ()->NeedsArp ())
{
Arp * arp = m_node->GetArp ();
MacAddress hardwareDestination;
bool found = arp->Lookup (p, dest, GetDevice (), &hardwareDestination);
bool found = arp->Lookup (p, dest, PeekDevice (), &hardwareDestination);
if (found)
{
GetDevice ()->Send (p, hardwareDestination, Ipv4::PROT_NUMBER);
PeekDevice ()->Send (p, hardwareDestination, Ipv4::PROT_NUMBER);
}
}
else
{
GetDevice ()->Send (p, GetDevice ()->GetBroadcast (), Ipv4::PROT_NUMBER);
PeekDevice ()->Send (p, PeekDevice ()->GetBroadcast (), Ipv4::PROT_NUMBER);
}
}

View File

@ -65,7 +65,7 @@ Arp::FindCache (NetDevice *device)
{
for (CacheList::const_iterator i = m_cacheList.begin (); i != m_cacheList.end (); i++)
{
if ((*i)->GetDevice () == device)
if ((*i)->PeekDevice () == device)
{
return *i;
}
@ -79,9 +79,9 @@ Arp::FindCache (NetDevice *device)
}
void
Arp::Receive(Packet& packet, NetDevice &device)
Arp::Receive(Packet& packet, NetDevice *device)
{
ArpCache *cache = FindCache (&device);
ArpCache *cache = FindCache (device);
ArpHeader arp;
packet.RemoveHeader (arp);
if (arp.IsRequest () &&
@ -94,7 +94,7 @@ Arp::Receive(Packet& packet, NetDevice &device)
}
else if (arp.IsReply () &&
arp.GetDestinationIpv4Address ().IsEqual (cache->GetInterface ()->GetAddress ()) &&
arp.GetDestinationHardwareAddress ().IsEqual (device.GetAddress ()))
arp.GetDestinationHardwareAddress ().IsEqual (device->GetAddress ()))
{
Ipv4Address from = arp.GetSourceIpv4Address ();
ArpCache::Entry *entry = cache->Lookup (from);
@ -200,25 +200,25 @@ void
Arp::SendArpRequest (ArpCache const *cache, Ipv4Address to)
{
ArpHeader arp;
arp.SetRequest (cache->GetDevice ()->GetAddress (),
arp.SetRequest (cache->PeekDevice ()->GetAddress (),
cache->GetInterface ()->GetAddress (),
cache->GetDevice ()->GetBroadcast (),
cache->PeekDevice ()->GetBroadcast (),
to);
Packet packet;
packet.AddHeader (arp);
cache->GetDevice ()->Send (packet, cache->GetDevice ()->GetBroadcast (), PROT_NUMBER);
cache->PeekDevice ()->Send (packet, cache->PeekDevice ()->GetBroadcast (), PROT_NUMBER);
}
void
Arp::SendArpReply (ArpCache const *cache, Ipv4Address toIp, MacAddress toMac)
{
ArpHeader arp;
arp.SetReply (cache->GetDevice ()->GetAddress (),
arp.SetReply (cache->PeekDevice ()->GetAddress (),
cache->GetInterface ()->GetAddress (),
toMac, toIp);
Packet packet;
packet.AddHeader (arp);
cache->GetDevice ()->Send (packet, toMac, PROT_NUMBER);
cache->PeekDevice ()->Send (packet, toMac, PROT_NUMBER);
}
}//namespace ns3

View File

@ -46,7 +46,7 @@ public:
virtual TraceResolver *CreateTraceResolver (TraceContext const &context);
virtual void Receive(Packet& p, NetDevice &device);
virtual void Receive(Packet& p, NetDevice *device);
bool Lookup (Packet &p, Ipv4Address destination,
NetDevice *device,
MacAddress *hardwareDestination);

View File

@ -160,7 +160,7 @@ InternetNode::ReceiveFromDevice (NetDevice *device, const Packet &p, uint16_t pr
if (target != 0)
{
Packet packet = p;
target->Receive(packet, *device);
target->Receive(packet, device);
return true;
}
return false;

View File

@ -34,13 +34,23 @@ namespace ns3 {
Ipv4Interface::Ipv4Interface (NetDevice *nd)
: m_netdevice (nd),
m_ifup(false)
{}
{
if (m_netdevice != 0)
{
m_netdevice->Ref ();
}
}
Ipv4Interface::~Ipv4Interface ()
{}
{
if (m_netdevice != 0)
{
m_netdevice->Unref ();
}
}
NetDevice*
Ipv4Interface::GetDevice (void) const
Ipv4Interface::PeekDevice (void) const
{
return m_netdevice;
}

View File

@ -87,7 +87,7 @@ public:
* \returns the underlying NetDevice. This method can return
* zero if this interface has no associated NetDevice.
*/
NetDevice *GetDevice (void) const;
NetDevice *PeekDevice (void) const;
/**
* \param a set the ipv4 address of this interface.

View File

@ -33,9 +33,7 @@ Ipv4LoopbackInterface::Ipv4LoopbackInterface (Node *node)
{
}
Ipv4LoopbackInterface::~Ipv4LoopbackInterface ()
{
delete GetDevice ();
}
{}
Node *
Ipv4LoopbackInterface::GetNode (void) const
@ -52,7 +50,7 @@ Ipv4LoopbackInterface::DoCreateTraceResolver (TraceContext const &context)
void
Ipv4LoopbackInterface::SendTo (Packet packet, Ipv4Address dest)
{
m_node->GetIpv4 ()->Receive (packet, *(this->GetDevice ()));
m_node->GetIpv4 ()->Receive (packet, PeekDevice ());
}
}//namespace ns3

View File

@ -338,7 +338,7 @@ Ipv4::FindInterfaceForDevice (NetDevice const*device)
{
for (Ipv4InterfaceList::const_iterator i = m_interfaces.begin (); i != m_interfaces.end (); i++)
{
if ((*i)->GetDevice () == device)
if ((*i)->PeekDevice () == device)
{
return *i;
}
@ -354,12 +354,12 @@ Ipv4::Copy(Node *node) const
return ipv4;
}
void
Ipv4::Receive(Packet& packet, NetDevice &device)
Ipv4::Receive(Packet& packet, NetDevice *device)
{
uint32_t index = 0;
for (Ipv4InterfaceList::const_iterator i = m_interfaces.begin (); i != m_interfaces.end (); i++)
{
if ((*i)->GetDevice () == &device)
if ((*i)->PeekDevice () == device)
{
m_rxTrace (packet, index);
break;
@ -439,7 +439,7 @@ Ipv4::SendRealOut (Packet const &p, Ipv4Header const &ip, Ipv4Route const &route
bool
Ipv4::Forwarding (Packet const &packet, Ipv4Header &ipHeader, NetDevice &device)
Ipv4::Forwarding (Packet const &packet, Ipv4Header &ipHeader, NetDevice *device)
{
for (Ipv4InterfaceList::const_iterator i = m_interfaces.begin ();
i != m_interfaces.end (); i++)
@ -455,7 +455,7 @@ Ipv4::Forwarding (Packet const &packet, Ipv4Header &ipHeader, NetDevice &device)
i != m_interfaces.end (); i++)
{
Ipv4Interface *interface = *i;
if (interface->GetDevice () == &device)
if (interface->PeekDevice () == device)
{
if (ipHeader.GetDestination ().IsEqual (interface->GetBroadcast ()))
{

View File

@ -193,7 +193,7 @@ public:
* - implement a per-NetDevice ARP cache
* - send back arp replies on the right device
*/
virtual void Receive(Packet& p, NetDevice &device);
virtual void Receive(Packet& p, NetDevice *device);
/**
* \param packet packet to send
@ -219,7 +219,7 @@ public:
private:
void SendRealOut (Packet const &packet, Ipv4Header const &ip, Ipv4Route const &route);
bool Forwarding (Packet const &packet, Ipv4Header &ipHeader, NetDevice &device);
bool Forwarding (Packet const &packet, Ipv4Header &ipHeader, NetDevice *device);
void ForwardUp (Packet p, Ipv4Header const&ip);
uint32_t AddIpv4Interface (Ipv4Interface *interface);
void SetupLoopback (void);

View File

@ -54,7 +54,7 @@ public:
* - implement a per-NetDevice ARP cache
* - send back arp replies on the right device
*/
virtual void Receive(Packet& p, NetDevice &device) = 0;
virtual void Receive(Packet& p, NetDevice *device) = 0;
private:
int m_protocolNumber;

View File

@ -72,6 +72,7 @@ Node::SetSystemId(uint32_t s )
uint32_t
Node::AddDevice (NetDevice *device)
{
device->Ref ();
uint32_t index = m_devices.size ();
m_devices.push_back (device);
DoAddDevice (device);