{%hackmd @ninoagus/rkG8Y3Mfp %} # NS3 Source Code Simulation ## Dependencies NS3 ```= sudo apt install g++ python3 cmake ninja-build git gir1.2-goocanvas-2.0 python3-gi python3-gi-cairo python3-pygraphviz gir1.2-gtk-3.0 ipython3 tcpdump wireshark sqlite sqlite3 libsqlite3-dev qtbase5-dev qtchooser qt5-qmake qtbase5-dev-tools openmpi-bin openmpi-common openmpi-doc libopenmpi-dev doxygen graphviz imagemagick python3-sphinx dia texlive dvipng latexmk texlive-extra-utils texlive-latex-extra texlive-font-utils libeigen3-dev gsl-bin libgsl-dev libgslcblas0 libxml2 libxml2-dev libgtk-3-dev lxc- utils lxc-templates vtun uml-utilities ebtables bridge-utils libboost-all-dev -y ``` ## BAB 7: Point to Point Simulation ```= /* * 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 */ #include "ns3/applications-module.h" #include "ns3/core-module.h" #include "ns3/internet-module.h" #include "ns3/network-module.h" #include "ns3/point-to-point-module.h" #include "ns3/netanim-module.h" // Default Network Topology // // 10.1.1.0 // n0 -------------- n1 // point-to-point // using namespace ns3; NS_LOG_COMPONENT_DEFINE("FirstScriptExample"); int main(int argc, char* argv[]) { CommandLine cmd(__FILE__); cmd.Parse(argc, argv); Time::SetResolution(Time::NS); LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO); LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO); NodeContainer nodes; nodes.Create(2); PointToPointHelper pointToPoint; pointToPoint.SetDeviceAttribute("DataRate", StringValue("5Mbps")); pointToPoint.SetChannelAttribute("Delay", StringValue("2ms")); NetDeviceContainer devices; devices = pointToPoint.Install(nodes); InternetStackHelper stack; stack.Install(nodes); Ipv4AddressHelper address; address.SetBase("10.1.1.0", "255.255.255.0"); Ipv4InterfaceContainer interfaces = address.Assign(devices); UdpEchoServerHelper echoServer(9); ApplicationContainer serverApps = echoServer.Install(nodes.Get(1)); serverApps.Start(Seconds(1.0)); serverApps.Stop(Seconds(10.0)); UdpEchoClientHelper echoClient(interfaces.GetAddress(1), 9); echoClient.SetAttribute("MaxPackets", UintegerValue(1)); echoClient.SetAttribute("Interval", TimeValue(Seconds(1.0))); echoClient.SetAttribute("PacketSize", UintegerValue(1024)); ApplicationContainer clientApps = echoClient.Install(nodes.Get(0)); clientApps.Start(Seconds(2.0)); clientApps.Stop(Seconds(10.0)); AnimationInterface anim ("p2p.xml"); anim.SetConstantPosition(nodes.Get(0),0.0, 0.0); anim.SetConstantPosition(nodes.Get(1),87.0, 87.0); anim.UpdateNodeSize(0, 3.0, 3.0); anim.UpdateNodeSize(1, 3.0, 3.0); Simulator::Run(); Simulator::Destroy(); return 0; } ``` ## BAB 8: CSMA (Carrier Sense Multiple Access) Simulation ```= /* NETWORK SIMULATOR 3 FOR BEGINNER For support simulation in "CSMA (LAN Topology)" simulation */ /* * ilustrasi topologi * * n0 n1 n2 n3 n5 * | | | | | * =============== * LAN 10.1.1.0 */ #include "ns3/core-module.h" #include "ns3/network-module.h" #include "ns3/csma-module.h" #include "ns3/internet-module.h" #include "ns3/applications-module.h" #include "ns3/netanim-module.h" // entered for animation configuration and output file using namespace ns3; NS_LOG_COMPONENT_DEFINE ("CsmaScript"); int main (int argc, char *argv[]) { bool verbose = true; uint32_t nCsma = 5; CommandLine cmd; cmd.AddValue ("nCsma", "Number of CSMA nodes/devices", nCsma); cmd.AddValue ("verbose", "Tell echo applications to log if true", verbose); cmd.Parse (argc,argv); if (verbose) { LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO); LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO); } NodeContainer csmaNodes; csmaNodes.Create (nCsma); CsmaHelper csma; csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps")); csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560))); NetDeviceContainer csmaDevices; csmaDevices = csma.Install (csmaNodes); InternetStackHelper stack; stack.Install (csmaNodes); Ipv4AddressHelper address; address.SetBase ("10.1.1.0", "255.255.255.0"); Ipv4InterfaceContainer csmaInterfaces; csmaInterfaces = address.Assign (csmaDevices); UdpEchoServerHelper echoServer (9); ApplicationContainer serverApps = echoServer.Install (csmaNodes.Get (4)); serverApps.Start (Seconds (1.0)); serverApps.Stop (Seconds (10.0)); UdpEchoClientHelper echoClient (csmaInterfaces.GetAddress (4), 9); echoClient.SetAttribute ("MaxPackets", UintegerValue (1)); echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0))); echoClient.SetAttribute ("PacketSize", UintegerValue (1024)); ApplicationContainer clientApps = echoClient.Install (csmaNodes.Get (0)); clientApps.Start (Seconds (2.0)); clientApps.Stop (Seconds (10.0)); csma.EnablePcap ("csma", csmaDevices.Get (0), true); // Animation configuration lines AnimationInterface anim ("csma.xml"); anim.SetConstantPosition (csmaNodes.Get(0), 0.0, 40.0 ); anim.SetConstantPosition (csmaNodes.Get(1), 24.0, 40.0 ); anim.SetConstantPosition (csmaNodes.Get(2), 48.0, 40.0 ); anim.SetConstantPosition (csmaNodes.Get(3), 72.0, 40.0 ); anim.SetConstantPosition (csmaNodes.Get(4), 96.0, 40.0 ); anim.UpdateNodeSize(0, 3.0, 3.0); anim.UpdateNodeSize(1, 3.0, 3.0); anim.UpdateNodeSize(2, 3.0, 3.0); anim.UpdateNodeSize(3, 3.0, 3.0); anim.UpdateNodeSize(4, 3.0, 3.0); // End of animation configuration Simulator::Run (); Simulator::Destroy (); return 0; } ``` ## BAB 9: Point to Point Topology and CSMA Simulation ```= /* * 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 */ #include "ns3/applications-module.h" #include "ns3/core-module.h" #include "ns3/csma-module.h" #include "ns3/internet-module.h" #include "ns3/ipv4-global-routing-helper.h" #include "ns3/network-module.h" #include "ns3/point-to-point-module.h" #include "ns3/netanim-module.h" // Default Network Topology // // 10.1.1.0 // n0 -------------- n1 n2 n3 n4 // point-to-point | | | | // ================ // LAN 10.1.2.0 using namespace ns3; NS_LOG_COMPONENT_DEFINE("SecondScriptExample"); int main(int argc, char* argv[]) { bool verbose = true; uint32_t nCsma = 3; CommandLine cmd(__FILE__); cmd.AddValue("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma); cmd.AddValue("verbose", "Tell echo applications to log if true", verbose); cmd.Parse(argc, argv); if (verbose) { LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO); LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO); } nCsma = nCsma == 0 ? 1 : nCsma; NodeContainer p2pNodes; p2pNodes.Create(2); NodeContainer csmaNodes; csmaNodes.Add(p2pNodes.Get(1)); csmaNodes.Create(nCsma); PointToPointHelper pointToPoint; pointToPoint.SetDeviceAttribute("DataRate", StringValue("5Mbps")); pointToPoint.SetChannelAttribute("Delay", StringValue("2ms")); NetDeviceContainer p2pDevices; p2pDevices = pointToPoint.Install(p2pNodes); CsmaHelper csma; csma.SetChannelAttribute("DataRate", StringValue("100Mbps")); csma.SetChannelAttribute("Delay", TimeValue(NanoSeconds(6560))); NetDeviceContainer csmaDevices; csmaDevices = csma.Install(csmaNodes); InternetStackHelper stack; stack.Install(p2pNodes.Get(0)); stack.Install(csmaNodes); Ipv4AddressHelper address; address.SetBase("10.1.1.0", "255.255.255.0"); Ipv4InterfaceContainer p2pInterfaces; p2pInterfaces = address.Assign(p2pDevices); address.SetBase("10.1.2.0", "255.255.255.0"); Ipv4InterfaceContainer csmaInterfaces; csmaInterfaces = address.Assign(csmaDevices); UdpEchoServerHelper echoServer(9); ApplicationContainer serverApps = echoServer.Install(csmaNodes.Get(nCsma)); serverApps.Start(Seconds(1.0)); serverApps.Stop(Seconds(10.0)); UdpEchoClientHelper echoClient(csmaInterfaces.GetAddress(nCsma), 9); echoClient.SetAttribute("MaxPackets", UintegerValue(1)); echoClient.SetAttribute("Interval", TimeValue(Seconds(1.0))); echoClient.SetAttribute("PacketSize", UintegerValue(1024)); ApplicationContainer clientApps = echoClient.Install(p2pNodes.Get(0)); clientApps.Start(Seconds(2.0)); clientApps.Stop(Seconds(10.0)); Ipv4GlobalRoutingHelper::PopulateRoutingTables(); pointToPoint.EnablePcapAll("second"); csma.EnablePcap("second", csmaDevices.Get(1), true); AnimationInterface anim ("csmap2p.xml"); anim.SetConstantPosition (p2pNodes.Get(0),0.0, 0.0); anim.SetConstantPosition (csmaNodes.Get(0),54.0, 61.0); anim.SetConstantPosition (csmaNodes.Get(1),54.0, 73.0); anim.SetConstantPosition (csmaNodes.Get(2),54.0, 85.0); anim.SetConstantPosition (csmaNodes.Get(3),54.0, 98.0); anim.UpdateNodeSize(0, 5.0, 5.0); anim.UpdateNodeSize(1, 5.0, 5.0); anim.UpdateNodeSize(2, 5.0, 5.0); anim.UpdateNodeSize(3, 5.0, 5.0); anim.UpdateNodeSize(4, 5.0, 5.0); Simulator::Run(); Simulator::Destroy(); return 0; } ``` ## BAB 10: Wireless (AP-STA) Network Simulation ```= /* -*- 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 */ #include "ns3/core-module.h" #include "ns3/network-module.h" #include "ns3/applications-module.h" #include "ns3/wifi-module.h" #include "ns3/mobility-module.h" #include "ns3/internet-module.h" #include "ns3/netanim-module.h" // Default Network Topology // // Number of wifi nodes can be increased up to 250 // Wifi 10.1.3.0 // AP // * * * * // | | | | 10.1.1.0node // n3 n2 n1 n0 using namespace ns3; NS_LOG_COMPONENT_DEFINE ("ThirdScriptExample"); int main (int argc, char *argv[]) { bool verbose = true; uint32_t nWifi = 3; bool tracing = false; CommandLine cmd; cmd.AddValue ("nWifi", "Number of wifi STA devices", nWifi); cmd.AddValue ("verbose", "Tell echo applications to log if true", verbose); cmd.AddValue ("tracing", "Enable pcap tracing", tracing); cmd.Parse (argc,argv); if (verbose) { LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO); LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO); } NodeContainer wifiStaNodes; wifiStaNodes.Create (nWifi); NodeContainer wifiApNode; wifiApNode.Create(1); YansWifiChannelHelper channel = YansWifiChannelHelper::Default (); YansWifiPhyHelper phy; phy.SetChannel (channel.Create ()); WifiHelper wifi; wifi.SetRemoteStationManager ("ns3::MinstrelHtWifiManager"); WifiMacHelper mac; Ssid ssid = Ssid ("ns-3-ssid"); mac.SetType ("ns3::StaWifiMac", "Ssid", SsidValue (ssid), "ActiveProbing", BooleanValue (false)); NetDeviceContainer staDevices; staDevices = wifi.Install (phy, mac, wifiStaNodes); mac.SetType ("ns3::ApWifiMac", "Ssid", SsidValue (ssid)); NetDeviceContainer apDevices; apDevices = wifi.Install (phy, mac, wifiApNode); MobilityHelper mobility; mobility.SetPositionAllocator ("ns3::GridPositionAllocator", "MinX", DoubleValue (0.0), "MinY", DoubleValue (0.0), "DeltaX", DoubleValue (5.0), "DeltaY", DoubleValue (10.0), "GridWidth", UintegerValue (3), "LayoutType", StringValue ("RowFirst")); mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel", "Bounds", RectangleValue (Rectangle (-50, 50, -50, 50))); mobility.Install (wifiStaNodes); mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); mobility.Install (wifiApNode); InternetStackHelper stack; stack.Install (wifiApNode); stack.Install (wifiStaNodes); Ipv4AddressHelper address; address.SetBase ("10.1.1.0", "255.255.255.0"); Ipv4InterfaceContainer wifiStaInterfaces; wifiStaInterfaces = address.Assign (staDevices); address.Assign (apDevices); UdpEchoServerHelper echoServer (9); ApplicationContainer serverApps = echoServer.Install (wifiStaNodes.Get (0)); serverApps.Start (Seconds (1.0)); serverApps.Stop (Seconds (10.0)); UdpEchoClientHelper echoClient (wifiStaInterfaces.GetAddress (2), 9); echoClient.SetAttribute ("MaxPackets", UintegerValue (10)); echoClient.SetAttribute ("Interval", TimeValue (Seconds (0.5))); echoClient.SetAttribute ("PacketSize", UintegerValue (1024)); ApplicationContainer clientApps = echoClient.Install (wifiStaNodes.Get (2)); clientApps.Start (Seconds (2.0)); clientApps.Stop (Seconds (10.0)); Ipv4GlobalRoutingHelper::PopulateRoutingTables (); Simulator::Stop (Seconds (10.0)); if (tracing == true) { phy.EnablePcap ("third", apDevices.Get (0)); } AnimationInterface anim ("third.xml"); anim.EnablePacketMetadata(true); Simulator::Run (); Simulator::Destroy (); return 0; } ``` ## BAB 11: Point to Point Topology, CSMA, and WIFI Simulation ```= /* * 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 */ #include "ns3/applications-module.h" #include "ns3/core-module.h" #include "ns3/csma-module.h" #include "ns3/internet-module.h" #include "ns3/mobility-module.h" #include "ns3/network-module.h" #include "ns3/point-to-point-module.h" #include "ns3/ssid.h" #include "ns3/yans-wifi-helper.h" #include "ns3/netanim-module.h" // Default Network Topology // // Wifi 10.1.3.0 // AP // * * * * // | | | | 10.1.1.0 // n5 n6 n7 n0 -------------- n1 n2 n3 n4 // point-to-point | | | | // ================ // LAN 10.1.2.0 using namespace ns3; NS_LOG_COMPONENT_DEFINE("ThirdScriptExample"); int main(int argc, char* argv[]) { bool verbose = true; uint32_t nCsma = 3; uint32_t nWifi = 3; bool tracing = false; CommandLine cmd(__FILE__); cmd.AddValue("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma); cmd.AddValue("nWifi", "Number of wifi STA devices", nWifi); cmd.AddValue("verbose", "Tell echo applications to log if true", verbose); cmd.AddValue("tracing", "Enable pcap tracing", tracing); cmd.Parse(argc, argv); // The underlying restriction of 18 is due to the grid position // allocator's configuration; the grid layout will exceed the // bounding box if more than 18 nodes are provided. if (nWifi > 18) { std::cout << "nWifi should be 18 or less; otherwise grid layout exceeds the bounding box" << std::endl; return 1; } if (verbose) { LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO); LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO); } NodeContainer p2pNodes; p2pNodes.Create(2); PointToPointHelper pointToPoint; pointToPoint.SetDeviceAttribute("DataRate", StringValue("5Mbps")); pointToPoint.SetChannelAttribute("Delay", StringValue("2ms")); NetDeviceContainer p2pDevices; p2pDevices = pointToPoint.Install(p2pNodes); NodeContainer csmaNodes; csmaNodes.Add(p2pNodes.Get(1)); csmaNodes.Create(nCsma); CsmaHelper csma; csma.SetChannelAttribute("DataRate", StringValue("100Mbps")); csma.SetChannelAttribute("Delay", TimeValue(NanoSeconds(6560))); NetDeviceContainer csmaDevices; csmaDevices = csma.Install(csmaNodes); NodeContainer wifiStaNodes; wifiStaNodes.Create(nWifi); NodeContainer wifiApNode = p2pNodes.Get(0); YansWifiChannelHelper channel = YansWifiChannelHelper::Default(); YansWifiPhyHelper phy; phy.SetChannel(channel.Create()); WifiMacHelper mac; Ssid ssid = Ssid("ns-3-ssid"); WifiHelper wifi; NetDeviceContainer staDevices; mac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid), "ActiveProbing", BooleanValue(false)); staDevices = wifi.Install(phy, mac, wifiStaNodes); NetDeviceContainer apDevices; mac.SetType("ns3::ApWifiMac", "Ssid", SsidValue(ssid)); apDevices = wifi.Install(phy, mac, wifiApNode); MobilityHelper mobility; mobility.SetPositionAllocator("ns3::GridPositionAllocator", "MinX", DoubleValue(0.0), "MinY", DoubleValue(0.0), "DeltaX", DoubleValue(5.0), "DeltaY", DoubleValue(10.0), "GridWidth", UintegerValue(3), "LayoutType", StringValue("RowFirst")); mobility.SetMobilityModel("ns3::RandomWalk2dMobilityModel", "Bounds", RectangleValue(Rectangle(-50, 50, -50, 50))); mobility.Install(wifiStaNodes); mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel"); mobility.Install(wifiApNode); InternetStackHelper stack; stack.Install(csmaNodes); stack.Install(wifiApNode); stack.Install(wifiStaNodes); Ipv4AddressHelper address; address.SetBase("10.1.1.0", "255.255.255.0"); Ipv4InterfaceContainer p2pInterfaces; p2pInterfaces = address.Assign(p2pDevices); address.SetBase("10.1.2.0", "255.255.255.0"); Ipv4InterfaceContainer csmaInterfaces; csmaInterfaces = address.Assign(csmaDevices); address.SetBase("10.1.3.0", "255.255.255.0"); address.Assign(staDevices); address.Assign(apDevices); UdpEchoServerHelper echoServer(9); ApplicationContainer serverApps = echoServer.Install(csmaNodes.Get(nCsma)); serverApps.Start(Seconds(1.0)); serverApps.Stop(Seconds(10.0)); UdpEchoClientHelper echoClient(csmaInterfaces.GetAddress(nCsma), 9); echoClient.SetAttribute("MaxPackets", UintegerValue(1)); echoClient.SetAttribute("Interval", TimeValue(Seconds(1.0))); echoClient.SetAttribute("PacketSize", UintegerValue(1024)); ApplicationContainer clientApps = echoClient.Install(wifiStaNodes.Get(nWifi - 1)); clientApps.Start(Seconds(2.0)); clientApps.Stop(Seconds(10.0)); Ipv4GlobalRoutingHelper::PopulateRoutingTables(); Simulator::Stop(Seconds(10.0)); if (tracing) { phy.SetPcapDataLinkType(WifiPhyHelper::DLT_IEEE802_11_RADIO); pointToPoint.EnablePcapAll("third"); phy.EnablePcap("third", apDevices.Get(0)); csma.EnablePcap("third", csmaDevices.Get(0), true); } AnimationInterface anim ("gabungan.xml"); anim.SetConstantPosition(p2pNodes.Get(0),0.0, 50.0); anim.SetConstantPosition(csmaNodes.Get(0),0.0, 100.0); anim.SetConstantPosition(csmaNodes.Get(1),25.0, 100.0); anim.SetConstantPosition(csmaNodes.Get(2),50.0, 100.0); anim.SetConstantPosition(csmaNodes.Get(3),75.0, 100.0); anim.UpdateNodeSize(0, 5.0, 5.0); anim.UpdateNodeSize(1, 5.0, 5.0); anim.UpdateNodeSize(2, 5.0, 5.0); anim.UpdateNodeSize(3, 5.0, 5.0); anim.UpdateNodeSize(4, 5.0, 5.0); anim.UpdateNodeSize(5, 5.0, 5.0); anim.UpdateNodeSize(6, 5.0, 5.0); anim.UpdateNodeSize(7, 5.0, 5.0); Simulator::Run(); Simulator::Destroy(); return 0; } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up