Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Unified server and client helper and added TC initialization.
  • Loading branch information
TommyPec committed Jul 2, 2017
commit a2921d8316c7d0146ae9346f05b4de38a1f3a16b
37 changes: 12 additions & 25 deletions src/internet-apps/examples/dhcp-example.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ main (int argc, char *argv[])
csma.SetChannelAttribute ("DataRate", StringValue ("5Mbps"));
csma.SetChannelAttribute ("Delay", StringValue ("2ms"));
csma.SetDeviceAttribute ("Mtu", UintegerValue (1500));
NetDeviceContainer dev_net = csma.Install (net);
NetDeviceContainer devNet = csma.Install (net);

NodeContainer p2pNodes;
p2pNodes.Add (net.Get (4));
Expand Down Expand Up @@ -103,42 +103,29 @@ main (int argc, char *argv[])
Ipv4Address ("172.30.1.1"), 1);

uint32_t ifIndex;
Ptr<Ipv4> ipv4Router = net.Get (3)->GetObject<Ipv4> ();
ifIndex = ipv4Router->AddInterface (dev_net.Get (3));
ipv4Router->AddAddress (ifIndex, Ipv4InterfaceAddress (Ipv4Address ("172.30.0.12"), Ipv4Mask ("/24")));
// ipv4Router->SetForwarding (ifIndex, true);
ipv4Router->SetUp (ifIndex);

Ptr<Ipv4> ipv4Router1 = net.Get (4)->GetObject<Ipv4> ();
ifIndex = ipv4Router1->AddInterface (dev_net.Get (4));
ifIndex = ipv4Router1->AddInterface (devNet.Get (4));
ipv4Router1->AddAddress (ifIndex, Ipv4InterfaceAddress (Ipv4Address ("172.30.0.17"), Ipv4Mask ("/24")));
ipv4Router1->SetForwarding (ifIndex, true);
ipv4Router1->SetUp (ifIndex);

NS_LOG_INFO ("Create Applications.");

/*
* DhcpServerHelper (Ipv4Address pool_addr, Ipv4Mask pool_mask,
* Ipv4Address serv_addr,
* Ipv4Address min_addr, Ipv4Address max_addr,
* Ipv4Address gateway = Ipv4Address ());
*
*/
DhcpServerHelper dhcpServerHelper (Ipv4Address ("172.30.0.0"), Ipv4Mask ("/24"),
Ipv4Address ("172.30.0.10"), Ipv4Address ("172.30.0.15"),
Ipv4Address ("172.30.0.17"));

ApplicationContainer dhcpServerApp = dhcpServerHelper.Install (router.Get (0));
DhcpHelper dhcpHelper;

ApplicationContainer dhcpServerApp = dhcpHelper.InstallDhcpServer (devNet.Get (3), Ipv4Address ("172.30.0.12"),
Ipv4Address ("172.30.0.0"), Ipv4Mask ("/24"),
Ipv4Address ("172.30.0.10"), Ipv4Address ("172.30.0.15"),
Ipv4Address ("172.30.0.17"));
dhcpServerApp.Start (Seconds (1.0));
dhcpServerApp.Stop (stopTime);

DhcpClientHelper dhcpClientHelper;
NetDeviceContainer dhcpClientNetDevs;
dhcpClientNetDevs.Add (dev_net.Get (0));
dhcpClientNetDevs.Add (dev_net.Get (1));
dhcpClientNetDevs.Add (dev_net.Get (2));
dhcpClientNetDevs.Add (devNet.Get (0));
dhcpClientNetDevs.Add (devNet.Get (1));
dhcpClientNetDevs.Add (devNet.Get (2));

ApplicationContainer dhcpClients = dhcpClientHelper.Install (dhcpClientNetDevs);
ApplicationContainer dhcpClients = dhcpHelper.InstallDhcpClient (dhcpClientNetDevs);
dhcpClients.Start (Seconds (10.0));
dhcpClients.Stop (stopTime);

Expand Down
96 changes: 59 additions & 37 deletions src/internet-apps/helper/dhcp-helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,64 +37,48 @@ namespace ns3 {

NS_LOG_COMPONENT_DEFINE ("DhcpHelper");

DhcpClientHelper::DhcpClientHelper ()
DhcpHelper::DhcpHelper ()
{
m_factory.SetTypeId (DhcpClient::GetTypeId ());
m_clientFactory.SetTypeId (DhcpClient::GetTypeId ());
m_serverFactory.SetTypeId (DhcpServer::GetTypeId ());
}

DhcpServerHelper::DhcpServerHelper (Ipv4Address pool_addr, Ipv4Mask pool_mask,
Ipv4Address min_addr, Ipv4Address max_addr,
Ipv4Address gateway)
{
m_factory.SetTypeId (DhcpServer::GetTypeId ());
SetAttribute ("PoolAddresses", Ipv4AddressValue (pool_addr));
SetAttribute ("PoolMask", Ipv4MaskValue (pool_mask));
SetAttribute ("FirstAddress", Ipv4AddressValue (min_addr));
SetAttribute ("LastAddress", Ipv4AddressValue (max_addr));
SetAttribute ("Gateway", Ipv4AddressValue (gateway));
}

void DhcpClientHelper::SetAttribute (
void DhcpHelper::SetClientAttribute (
std::string name,
const AttributeValue &value)
{
m_factory.Set (name, value);
m_clientFactory.Set (name, value);
}

void DhcpServerHelper::SetAttribute (
void DhcpHelper::SetServerAttribute (
std::string name,
const AttributeValue &value)
{
m_factory.Set (name, value);
m_serverFactory.Set (name, value);
}

ApplicationContainer DhcpClientHelper::Install (Ptr<NetDevice> netDevice) const
ApplicationContainer DhcpHelper::InstallDhcpClient (Ptr<NetDevice> netDevice) const
{
return ApplicationContainer (InstallPriv (netDevice));
return ApplicationContainer (InstallDhcpClientPriv (netDevice));
}

ApplicationContainer DhcpClientHelper::Install (NetDeviceContainer netDevices) const
ApplicationContainer DhcpHelper::InstallDhcpClient (NetDeviceContainer netDevices) const
{
ApplicationContainer apps;
for (NetDeviceContainer::Iterator i = netDevices.Begin (); i != netDevices.End (); ++i)
{
apps.Add (InstallPriv (*i));
apps.Add (InstallDhcpClientPriv (*i));
}
return apps;
}

ApplicationContainer DhcpServerHelper::Install (Ptr<Node> node) const
{
return ApplicationContainer (InstallPriv (node));
}

Ptr<Application> DhcpClientHelper::InstallPriv (Ptr<NetDevice> netDevice) const
Ptr<Application> DhcpHelper::InstallDhcpClientPriv (Ptr<NetDevice> netDevice) const
{
Ptr<Node> node = netDevice->GetNode ();
NS_ASSERT_MSG (node != 0, "DhcpClientHelper: NetDevice is not not associated with any node -> fail");

Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> ();
NS_ASSERT_MSG (ipv4, "DhcpClientHelper: NetDevice is associated"
NS_ASSERT_MSG (ipv4, "DhcpHelper: NetDevice is associated"
" with a node without IPv4 stack installed -> fail "
"(maybe need to use InternetStackHelper?)");

Expand All @@ -103,8 +87,7 @@ Ptr<Application> DhcpClientHelper::InstallPriv (Ptr<NetDevice> netDevice) const
{
interface = ipv4->AddInterface (netDevice);
}
NS_ASSERT_MSG (interface >= 0, "Ipv4AddressHelper::Assign(): "
"Interface index not found");
NS_ASSERT_MSG (interface >= 0, "DhcpHelper Interface index not found");

ipv4->SetMetric (interface, 1);
ipv4->SetUp (interface);
Expand All @@ -115,24 +98,63 @@ Ptr<Application> DhcpClientHelper::InstallPriv (Ptr<NetDevice> netDevice) const
Ptr<TrafficControlLayer> tc = node->GetObject<TrafficControlLayer> ();
if (tc && DynamicCast<LoopbackNetDevice> (netDevice) == 0 && tc->GetRootQueueDiscOnDevice (netDevice) == 0)
{
NS_LOG_LOGIC ("DhcpClienHelper - Installing default traffic control configuration");
NS_LOG_LOGIC ("DhcpHelper - Installing default traffic control configuration");
TrafficControlHelper tcHelper = TrafficControlHelper::Default ();
tcHelper.Install (netDevice);
}

Ptr<DhcpClient> app = DynamicCast <DhcpClient> (m_factory.Create<DhcpClient> ());
Ptr<DhcpClient> app = DynamicCast <DhcpClient> (m_clientFactory.Create<DhcpClient> ());
app->SetDhcpClientNetDevice (netDevice);
node->AddApplication (app);

return app;
}

Ptr<Application> DhcpServerHelper::InstallPriv (Ptr<Node> node) const
ApplicationContainer DhcpHelper::InstallDhcpServer (Ptr<NetDevice> netDevice, Ipv4Address serverAddr,
Ipv4Address poolAddr, Ipv4Mask poolMask,
Ipv4Address minAddr, Ipv4Address maxAddr,
Ipv4Address gateway)
{
Ptr<Application> app = m_factory.Create<DhcpServer> ();
node->AddApplication (app);
m_serverFactory.Set ("PoolAddresses", Ipv4AddressValue (poolAddr));
m_serverFactory.Set ("PoolMask", Ipv4MaskValue (poolMask));
m_serverFactory.Set ("FirstAddress", Ipv4AddressValue (minAddr));
m_serverFactory.Set ("LastAddress", Ipv4AddressValue (maxAddr));
m_serverFactory.Set ("Gateway", Ipv4AddressValue (gateway));

return app;
Ptr<Node> node = netDevice->GetNode ();
NS_ASSERT_MSG (node != 0, "DhcpHelper: NetDevice is not not associated with any node -> fail");

Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> ();
NS_ASSERT_MSG (ipv4, "DhcpHelper: NetDevice is associated"
" with a node without IPv4 stack installed -> fail "
"(maybe need to use InternetStackHelper?)");

int32_t interface = ipv4->GetInterfaceForDevice (netDevice);
if (interface == -1)
{
interface = ipv4->AddInterface (netDevice);
}
NS_ASSERT_MSG (interface >= 0, "DhcpHelper: Interface index not found");

Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (serverAddr, poolMask);
ipv4->AddAddress (interface, ipv4Addr);
ipv4->SetMetric (interface, 1);
ipv4->SetUp (interface);

// Install the default traffic control configuration if the traffic
// control layer has been aggregated, if this is not
// a loopback interface, and there is no queue disc installed already
Ptr<TrafficControlLayer> tc = node->GetObject<TrafficControlLayer> ();
if (tc && DynamicCast<LoopbackNetDevice> (netDevice) == 0 && tc->GetRootQueueDiscOnDevice (netDevice) == 0)
{
NS_LOG_LOGIC ("DhcpHelper - Installing default traffic control configuration");
TrafficControlHelper tcHelper = TrafficControlHelper::Default ();
tcHelper.Install (netDevice);
}

Ptr<Application> app = m_serverFactory.Create<DhcpServer> ();
node->AddApplication (app);
return ApplicationContainer (app);
}

} // namespace ns3
94 changes: 37 additions & 57 deletions src/internet-apps/helper/dhcp-helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,91 +36,71 @@ namespace ns3 {
/**
* \ingroup dhcp
*
* \class DhcpClientHelper
* \brief The helper class used to configure and install the client application on nodes
* \class DhcpHelper
* \brief The helper class used to configure and install DHCP applications on nodes
*/
class DhcpClientHelper
class DhcpHelper
{
public:
DhcpHelper ();

/**
* \brief Constructor of client helper
* \brief Set DHCP client attributes
* \param name Name of the attribute
* \param value Value to be set
*/
DhcpClientHelper ();
void SetClientAttribute (std::string name, const AttributeValue &value);

/**
* \brief Function to set DHCP client attributes
* \brief Set DHCP server attributes
* \param name Name of the attribute
* \param value Value to be set
*/
void SetAttribute (std::string name, const AttributeValue &value);
void SetServerAttribute (std::string name, const AttributeValue &value);

/**
* \brief Function to install DHCP client of a node
* \brief Install DHCP client of a nodes / NetDevice
* \param netDevice The NetDevice on which DHCP client application has to be installed
* \return The application container with DHCP client installed
*/
ApplicationContainer Install (Ptr<NetDevice> netDevice) const;
ApplicationContainer InstallDhcpClient (Ptr<NetDevice> netDevice) const;

/**
* \brief Function to install DHCP client of a node
* \brief Install DHCP client of a set of nodes / NetDevices
* \param netDevices The NetDevices on which DHCP client application has to be installed
* \return The application container with DHCP client installed
*/
ApplicationContainer Install (NetDeviceContainer netDevices) const;

private:
/**
* \brief Function to install DHCP client on a node
* \param node The node on which DHCP client application has to be installed
* \return The pointer to the installed DHCP client
*/
Ptr<Application> InstallPriv (Ptr<NetDevice> netDevice) const;
ObjectFactory m_factory; //!< The subset of ns3::object for setting attributes
};

/**
* \ingroup dhcp
*
* \class DhcpServerHelper
* \brief The helper class used to configure and install the server application on nodes
*/
class DhcpServerHelper
{
public:
/**
* \brief Constructor of server helper
* \param pool_addr The Ipv4Address of the allocated pool
* \param pool_mask The mask of the allocated pool
* \param min_addr The lower bound of the Ipv4Address pool
* \param max_addr The upper bound of the Ipv4Address pool
* \param gateway The Ipv4Address of default gateway
*/
DhcpServerHelper (Ipv4Address pool_addr, Ipv4Mask pool_mask,
Ipv4Address min_addr, Ipv4Address max_addr,
Ipv4Address gateway = Ipv4Address ());

/**
* \brief Function to set DHCP server attributes
* \param name Name of the attribute
* \param value Value to be set
*/
void SetAttribute (std::string name, const AttributeValue &value);
ApplicationContainer InstallDhcpClient (NetDeviceContainer netDevices) const;

/**
* \brief Function to install DHCP server on a node
* \param node The node on which DHCP server application has to be installed
* \brief Install DHCP server of a node / NetDevice
*
* Note: the server address must be coherent with the pool address, because
* DHCP relays are not yet supported.
*
* \param netDevice The NetDevice on which DHCP server application has to be installed
* \param serverAddr The Ipv4Address of the server
* \param poolAddr The Ipv4Address (network part) of the allocated pool
* \param poolMask The mask of the allocated pool
* \param minAddr The lower bound of the Ipv4Address pool
* \param maxAddr The upper bound of the Ipv4Address pool
* \param gateway The Ipv4Address of default gateway (optional)
* \return The application container with DHCP server installed
*/
ApplicationContainer Install (Ptr<Node> node) const;
ApplicationContainer InstallDhcpServer (Ptr<NetDevice> netDevice, Ipv4Address serverAddr,
Ipv4Address poolAddr, Ipv4Mask poolMask,
Ipv4Address minAddr, Ipv4Address maxAddr,
Ipv4Address gateway = Ipv4Address ());

private:
/**
* \brief Function to install DHCP server of a node
* \param node The node on which DHCP server application has to be installed
* \return The pointer to the installed DHCP server
* \brief Function to install DHCP client on a node
* \param node The node on which DHCP client application has to be installed
* \return The pointer to the installed DHCP client
*/
Ptr<Application> InstallPriv (Ptr<Node> node) const;
ObjectFactory m_factory; //!< The subset of ns3::object for setting attributes
Ptr<Application> InstallDhcpClientPriv (Ptr<NetDevice> netDevice) const;
ObjectFactory m_clientFactory; //!< DHCP client factory
ObjectFactory m_serverFactory; //!< DHCP client factory
};

} // namespace ns3
Expand Down
27 changes: 10 additions & 17 deletions src/internet-apps/test/dhcp-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,26 +94,19 @@ DhcpTestCase::DoRun (void)
tcpip.Install (nodes);
tcpip.Install (routers);

uint32_t ifIndex;

// Setup IPv4 addresses and forwarding
Ptr<Ipv4> ipv4Router = net.Get (1)->GetObject<Ipv4> ();
ifIndex = ipv4Router->AddInterface (devNet.Get (1));
ipv4Router->AddAddress (ifIndex, Ipv4InterfaceAddress (Ipv4Address ("172.30.0.1"), Ipv4Mask ("/24")));
ipv4Router->SetForwarding (ifIndex, true);
ipv4Router->SetUp (ifIndex);

DhcpServerHelper dhcpServerHelper (Ipv4Address ("172.30.0.0"), Ipv4Mask ("/24"),
Ipv4Address ("172.30.0.10"), Ipv4Address ("172.30.0.100"));
ApplicationContainer dhcpServerApps = dhcpServerHelper.Install (routers.Get (0));
dhcpServerApps.Start (Seconds (1.0));
dhcpServerApps.Stop (Seconds (20.0));

DhcpClientHelper dhcpClientHelper;
DhcpHelper dhcpHelper;

ApplicationContainer dhcpServerApp = dhcpHelper.InstallDhcpServer (devNet.Get (1), Ipv4Address ("172.30.0.12"),
Ipv4Address ("172.30.0.0"), Ipv4Mask ("/24"),
Ipv4Address ("172.30.0.10"), Ipv4Address ("172.30.0.15"),
Ipv4Address ("172.30.0.17"));
dhcpServerApp.Start (Seconds (1.0));
dhcpServerApp.Stop (Seconds (20.0));

NetDeviceContainer dhcpClientNetDevs;
dhcpClientNetDevs.Add (devNet.Get (0));

ApplicationContainer dhcpClientApps = dhcpClientHelper.Install (dhcpClientNetDevs);
ApplicationContainer dhcpClientApps = dhcpHelper.InstallDhcpClient (dhcpClientNetDevs);
dhcpClientApps.Start (Seconds (1.0));
dhcpClientApps.Stop (Seconds (20.0));

Expand Down