Topology Generator for NS3
Based on https://www.nsnam.org/wiki/Topology_Generator, I wrote this lab.
This tool can help you to create the p2p, csma, wifi, or combined networks. But for wifi networks, you need to change some codes before you run the simulation script.
[steps to use this tool]
1. Run the program
2. Create the network
3. Create the application
4. Generate the code (You can choose C++ or Python. Here we choose C++)
5. test-net1.cc
#include "ns3/core-module.h" #include "ns3/global-route-manager.h" #include "ns3/network-module.h" #include "ns3/internet-module.h" #include "ns3/bridge-module.h" #include "ns3/csma-module.h" #include "ns3/point-to-point-module.h" #include "ns3/applications-module.h" #include "ns3/v4ping-helper.h" #include "ns3/v4ping.h" using namespace ns3; int main(int argc, char *argv[]) { CommandLine cmd; cmd.Parse (argc, argv); /* Configuration. */ /* Build nodes. */ NodeContainer term_0; term_0.Create (3); NodeContainer term_1; term_1.Create (1); NodeContainer router_0; router_0.Create (1); NodeContainer term_2; term_2.Create (1); /* Build link. */ CsmaHelper csma_hub_0; csma_hub_0.SetChannelAttribute ("DataRate", DataRateValue (100000000)); csma_hub_0.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (10000))); CsmaHelper csma_hub_1; csma_hub_1.SetChannelAttribute ("DataRate", DataRateValue (100000000)); csma_hub_1.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (10000))); PointToPointHelper p2p_p2p_0; p2p_p2p_0.SetDeviceAttribute ("DataRate", DataRateValue (100000000)); p2p_p2p_0.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (10000))); /* Build link net device container. */ NodeContainer all_hub_0; all_hub_0.Add (router_0); all_hub_0.Add (term_0); NetDeviceContainer ndc_hub_0 = csma_hub_0.Install (all_hub_0); NodeContainer all_hub_1; all_hub_1.Add (router_0); all_hub_1.Add (term_1); NetDeviceContainer ndc_hub_1 = csma_hub_1.Install (all_hub_1); NodeContainer all_p2p_0; all_p2p_0.Add (router_0); all_p2p_0.Add (term_2); NetDeviceContainer ndc_p2p_0 = p2p_p2p_0.Install (all_p2p_0); /* Install the IP stack. */ InternetStackHelper internetStackH; internetStackH.Install (term_0); internetStackH.Install (term_1); internetStackH.Install (router_0); internetStackH.Install (term_2); /* IP assign. */ Ipv4AddressHelper ipv4; ipv4.SetBase ("10.0.0.0", "255.255.255.0"); Ipv4InterfaceContainer iface_ndc_hub_0 = ipv4.Assign (ndc_hub_0); ipv4.SetBase ("10.0.1.0", "255.255.255.0"); Ipv4InterfaceContainer iface_ndc_hub_1 = ipv4.Assign (ndc_hub_1); ipv4.SetBase ("10.0.2.0", "255.255.255.0"); Ipv4InterfaceContainer iface_ndc_p2p_0 = ipv4.Assign (ndc_p2p_0); /* Generate Route. */ Ipv4GlobalRoutingHelper::PopulateRoutingTables (); /* Generate Application. */ InetSocketAddress dst_ping_0 = InetSocketAddress (iface_ndc_p2p_0.GetAddress(1)); OnOffHelper onoff_ping_0 = OnOffHelper ("ns3::Ipv4RawSocketFactory", dst_ping_0); onoff_ping_0.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]")); onoff_ping_0.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]")); ApplicationContainer apps_ping_0 = onoff_ping_0.Install(term_1.Get(0)); apps_ping_0.Start (Seconds (1.1)); apps_ping_0.Stop (Seconds (10.1)); PacketSinkHelper sink_ping_0 = PacketSinkHelper ("ns3::Ipv4RawSocketFactory", dst_ping_0); apps_ping_0 = sink_ping_0.Install (term_2.Get(0)); apps_ping_0.Start (Seconds (1.0)); apps_ping_0.Stop (Seconds (10.2)); V4PingHelper ping_ping_0 = V4PingHelper(iface_ndc_p2p_0.GetAddress(1)); apps_ping_0 = ping_ping_0.Install(term_1.Get(0)); apps_ping_0.Start (Seconds (1.2)); apps_ping_0.Stop (Seconds (10.0)); /* Simulation. */ /* Pcap output. */ /* Stop the simulation after x seconds. */ uint32_t stopTime = 11; Simulator::Stop (Seconds (stopTime)); /* Start and clean simulation. */ Simulator::Run (); Simulator::Destroy (); } |
6. Run the program
[Wifi Scenario]
1. Create the network
2. Generate the code
3. test-net2.cc
#include "ns3/core-module.h" #include "ns3/global-route-manager.h" #include "ns3/network-module.h" #include "ns3/internet-module.h" #include "ns3/wifi-module.h" #include "ns3/mobility-module.h" using namespace ns3; int main(int argc, char *argv[]) { CommandLine cmd; cmd.Parse (argc, argv); /* Configuration. */ /* Build nodes. */ NodeContainer ap_0; ap_0.Create (1); NodeContainer station_0; station_0.Create (1); NodeContainer station_1; station_1.Create (1); /* Build link. */ YansWifiPhyHelper wifiPhy_ap_0 = YansWifiPhyHelper::Default (); YansWifiChannelHelper wifiChannel_ap_0 = YansWifiChannelHelper::Default (); wifiPhy_ap_0.SetChannel (wifiChannel_ap_0.Create ()); /* Build link net device container. */ NodeContainer all_ap_0; NetDeviceContainer ndc_ap_0; Ssid ssid_ap_0 = Ssid ("wifi-default-0"); WifiHelper wifi_ap_0 = WifiHelper::Default (); NqosWifiMacHelper wifiMac_ap_0 = NqosWifiMacHelper::Default (); wifi_ap_0.SetRemoteStationManager ("ns3::ArfWifiManager"); wifiMac_ap_0.SetType ("ns3::ApWifiMac", "Ssid", SsidValue (ssid_ap_0), "BeaconGeneration", BooleanValue (true), "BeaconInterval", TimeValue (Seconds (2.5))); ndc_ap_0.Add (wifi_ap_0.Install (wifiPhy_ap_0, wifiMac_ap_0, ap_0)); wifiMac_ap_0.SetType ("ns3::StaWifiMac", "Ssid", SsidValue (ssid_ap_0), "ActiveProbing", BooleanValue (false)); ndc_ap_0.Add (wifi_ap_0.Install (wifiPhy_ap_0, wifiMac_ap_0, all_ap_0 )); MobilityHelper mobility_ap_0; mobility_ap_0.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); mobility_ap_0.Install (ap_0); mobility_ap_0.Install(all_ap_0); /* Install the IP stack. */ InternetStackHelper internetStackH; internetStackH.Install (ap_0); internetStackH.Install (station_0); internetStackH.Install (station_1); /* IP assign. */ Ipv4AddressHelper ipv4; ipv4.SetBase ("10.0.0.0", "255.255.255.0"); Ipv4InterfaceContainer iface_ndc_ap_0 = ipv4.Assign (ndc_ap_0); /* Generate Route. */ Ipv4GlobalRoutingHelper::PopulateRoutingTables (); /* Generate Application. */ /* Simulation. */ /* Pcap output. */ /* Stop the simulation after x seconds. */ uint32_t stopTime = 1; Simulator::Stop (Seconds (stopTime)); /* Start and clean simulation. */ Simulator::Run (); Simulator::Destroy (); } |
4. Run the program. You can see the errors occur.
5. Change the code.
#include "ns3/core-module.h" #include "ns3/global-route-manager.h" #include "ns3/network-module.h" #include "ns3/internet-module.h" #include "ns3/wifi-module.h" #include "ns3/mobility-module.h" using namespace ns3; int main(int argc, char *argv[]) { CommandLine cmd; cmd.Parse (argc, argv); /* Configuration. */ /* Build nodes. */ NodeContainer ap_0; ap_0.Create (1); NodeContainer station_0; station_0.Create (1); NodeContainer station_1; station_1.Create (1); /* Build link. */ //YansWifiPhyHelper wifiPhy_ap_0 = YansWifiPhyHelper::Default (); //YansWifiChannelHelper wifiChannel_ap_0 = YansWifiChannelHelper::Default (); //wifiPhy_ap_0.SetChannel (wifiChannel_ap_0.Create ());
YansWifiChannelHelper wifiChannel_ap_0 = YansWifiChannelHelper::Default
();//added by smallko YansWifiPhyHelper
wifiPhy_ap_0; //added by smallko wifiPhy_ap_0.SetChannel (wifiChannel_ap_0.Create
()); //added by smallko /* Build link net device container. */ NodeContainer all_ap_0;
all_ap_0.Add(station_0); //added by smallko all_ap_0.Add(station_1); //added by
smallko NetDeviceContainer ndc_ap_0; Ssid ssid_ap_0 = Ssid ("wifi-default-0"); //WifiHelper wifi_ap_0 = WifiHelper::Default (); //NqosWifiMacHelper wifiMac_ap_0 = NqosWifiMacHelper::Default ();
WifiMacHelper wifiMac_ap_0; //added by smallko WifiHelper
wifi_ap_0; //added by smallko wifi_ap_0.SetRemoteStationManager ("ns3::ArfWifiManager"); wifiMac_ap_0.SetType ("ns3::ApWifiMac", "Ssid", SsidValue (ssid_ap_0), "BeaconGeneration", BooleanValue (true), "BeaconInterval", TimeValue (MicroSeconds
(1024))); //TimeValuse
changed ndc_ap_0.Add (wifi_ap_0.Install (wifiPhy_ap_0, wifiMac_ap_0, ap_0)); wifiMac_ap_0.SetType ("ns3::StaWifiMac", "Ssid", SsidValue (ssid_ap_0), "ActiveProbing", BooleanValue (false)); ndc_ap_0.Add (wifi_ap_0.Install (wifiPhy_ap_0, wifiMac_ap_0, all_ap_0 )); MobilityHelper mobility_ap_0; mobility_ap_0.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); mobility_ap_0.Install (ap_0); mobility_ap_0.Install(all_ap_0); /* Install the IP stack. */ InternetStackHelper internetStackH; internetStackH.Install (ap_0); internetStackH.Install (station_0); internetStackH.Install (station_1); /* IP assign. */ Ipv4AddressHelper ipv4; ipv4.SetBase ("10.0.0.0", "255.255.255.0"); Ipv4InterfaceContainer iface_ndc_ap_0 = ipv4.Assign (ndc_ap_0); /* Generate Route. */ Ipv4GlobalRoutingHelper::PopulateRoutingTables (); /* Generate Application. */ /* Simulation. */ /* Pcap output. */ /* Stop the simulation after x seconds. */ uint32_t stopTime = 1; Simulator::Stop (Seconds (stopTime)); /* Start and clean simulation. */ Simulator::Run (); Simulator::Destroy (); } |
6. Run the program again. It works.
Last
Modified: 2022/2/8 done
[Author]
Dr. Chih-Heng Ke
Department of Computer Science
and Information Engineering, National Quemoy University, Kinmen, Taiwan
Email: smallko@gmail.com