/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ /* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation; * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * Author: John Abraham * */ #include #include #include #include "ns3/core-module.h" #include "ns3/network-module.h" #include "ns3/internet-module.h" #include "ns3/point-to-point-module.h" #include "ns3/netanim-module.h" #include "ns3/applications-module.h" #include "ns3/point-to-point-layout-module.h" using namespace ns3; using namespace std; //Illustrates a Callback void QueueDropCallBack ( Ptr p) { NS_LOG_UNCOND ("Packet Id:" << p->GetUid() << " Dropped at:" << Simulator::Now().GetSeconds ()); } // Illustrates /* Use of Simulator::Schedule Ptr DynamicCast GetInstanceTypeId */ static void PrintTxQueue (Ptr n) { Ptr q; uint32_t deviceIndex = 0; // Its the device Index of the device connecting to the left router Ptr p2pnd = DynamicCast (n->GetDevice (deviceIndex)); if (!p2pnd) return; q = p2pnd->GetQueue(); Ptr pdq = 0; // Now try to obtain the specific queue type if (q->GetInstanceTypeId ().GetName () == "ns3::DropTailQueue") { pdq = DynamicCast (q); // Uncomment the below to see a usage of TraceConnectWithoutContext // pdq->TraceConnectWithoutContext("Drop",MakeCallback(&QueueDropCallBack)); } if (!pdq) // Could not obtain Ptr to DropTailQueue return; if (pdq->GetMode () == ns3::DropTailQueue::PACKETS) { NS_LOG_UNCOND (Simulator::Now().GetSeconds () << "\t" << q->GetNPackets () << "\t" << q->GetTotalDroppedPackets ()); } else { NS_LOG_UNCOND (Simulator::Now().GetSeconds () << "\t" << q->GetNBytes () << "\t" << q->GetTotalDroppedBytes ()); } if (!Simulator::IsFinished ()) Simulator::Schedule (Seconds (0.1), &PrintTxQueue, n); } int main (int argc, char *argv[]) { Config::SetDefault ("ns3::OnOffApplication::PacketSize", UintegerValue (512)); Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue ("10Mbps")); uint32_t nLeftLeaf = 5; uint32_t nRightLeaf = 5; uint32_t nLeaf = 0; // If non-zero, number of both left and right std::string animFile; // Name of file for animation output uint32_t maxPackets = 100; uint32_t maxBytes = 100000; uint32_t modeBytes = 0; std::string tcpVariant = "ns3::TcpTahoe"; CommandLine cmd; cmd.AddValue ("nLeftLeaf", "Number of left side leaf nodes", nLeftLeaf); cmd.AddValue ("nRightLeaf","Number of right side leaf nodes", nRightLeaf); cmd.AddValue ("nLeaf", "Number of left and right side leaf nodes", nLeaf); cmd.AddValue ("animFile", "File Name for Animation Output", animFile); cmd.AddValue ("maxPackets","Max Packets allowed in the DropTail queue", maxPackets); cmd.AddValue ("maxBytes", "Max Bytes allowed in the DropTail queue", maxBytes); cmd.AddValue ("modeBytes", "Change default Mode from Packets to bytes.Set it equal to 1", modeBytes); cmd.AddValue ("tcpVariant", "Set the TcpVariant to ns3::TcpTahoe or ns3::TcpNewReno", tcpVariant); cmd.Parse (argc,argv); if (!modeBytes) { Config::SetDefault ("ns3::DropTailQueue::Mode", EnumValue(DropTailQueue::PACKETS)); Config::SetDefault ("ns3::DropTailQueue::MaxPackets", UintegerValue (maxPackets)); } else { Config::SetDefault ("ns3::DropTailQueue::Mode", EnumValue(DropTailQueue::BYTES)); Config::SetDefault ("ns3::DropTailQueue::MaxBytes", UintegerValue (maxBytes)); } Config::SetDefault ("ns3::TcpL4Protocol::SocketType", StringValue(tcpVariant)); Config::SetDefault ("ns3::TcpSocket::DelAckTimeout", TimeValue (Seconds (0.1))); if (nLeaf > 0) { nLeftLeaf = nLeaf; nRightLeaf = nLeaf; } // Create the point-to-point link helpers PointToPointHelper pointToPointRouter; pointToPointRouter.SetDeviceAttribute ("DataRate", StringValue ("1Mbps")); pointToPointRouter.SetChannelAttribute ("Delay", StringValue ("50ms")); PointToPointHelper pointToPointLeaf; pointToPointLeaf.SetDeviceAttribute ("DataRate", StringValue ("10Mbps")); pointToPointLeaf.SetChannelAttribute ("Delay", StringValue ("1ms")); PointToPointDumbbellHelper d (nLeftLeaf, pointToPointLeaf, nRightLeaf, pointToPointLeaf, pointToPointRouter); // Install Stack InternetStackHelper stack; d.InstallStack (stack); // Assign IP Addresses d.AssignIpv4Addresses (Ipv4AddressHelper ("10.1.1.0", "255.255.255.0"), Ipv4AddressHelper ("10.2.1.0", "255.255.255.0"), Ipv4AddressHelper ("10.3.1.0", "255.255.255.0")); // Install on/off app on all right side nodes OnOffHelper clientHelper ("ns3::TcpSocketFactory", Address ()); clientHelper.SetAttribute ("OnTime", RandomVariableValue (UniformVariable (0, 1))); clientHelper.SetAttribute ("OffTime", RandomVariableValue (UniformVariable (0, 1))); ApplicationContainer clientApps; // // Create a packet sink on the star "hub" to receive packets. // uint16_t port = 50000; Address hubLocalAddress (InetSocketAddress (Ipv4Address::GetAny (), port)); PacketSinkHelper packetSinkHelper ("ns3::TcpSocketFactory", hubLocalAddress); ApplicationContainer sinkApps; for (uint32_t i = 0; i < d.LeftCount (); ++i) { sinkApps.Add (packetSinkHelper.Install (d.GetLeft (i))); } sinkApps.Start (Seconds (0.0)); sinkApps.Stop (Seconds (30.0)); for (uint32_t i = 0; i < d.RightCount (); ++i) { // Create an on/off app sending packets to the same leaf right side AddressValue remoteAddress (InetSocketAddress (d.GetLeftIpv4Address (i), port)); clientHelper.SetAttribute ("Remote", remoteAddress); clientApps.Add (clientHelper.Install (d.GetRight (i))); } clientApps.Start (Seconds (1.0));// Start 1 second after sink clientApps.Stop (Seconds (15.0));// Stop before the sink // Set the bounding box for animation d.BoundingBox (1, 1, 100, 100); // Create the animation object and configure for specified output AnimationInterface anim; if (!animFile.empty ()) { anim.SetOutputFile (animFile); } anim.SetXMLOutput (); anim.StartAnimation (); Ipv4GlobalRoutingHelper::PopulateRoutingTables (); Ptr rightRouter = d.GetRight (); NS_ASSERT (rightRouter); //Simulator::Schedule (Seconds (0.1), &PrintTxQueue, rightRouter); std::cout << "Running the simulation" << std::endl; Simulator::Run (); uint32_t totalRxBytesCounter = 0; for (uint32_t i = 0; i < sinkApps.GetN (); i++) { Ptr app = sinkApps.Get (0); Ptr pktSink = DynamicCast (app); totalRxBytesCounter += pktSink->GetTotalRx (); } NS_LOG_UNCOND ("----------------------------\nTcpVariant:" << tcpVariant << "\nGoodput Bytes/sec:" << totalRxBytesCounter/Simulator::Now ().GetSeconds ()); NS_LOG_UNCOND ("----------------------------"); std::cout << "Destroying the simulation" << std::endl; Simulator::Destroy (); return 0; }