Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <sstream>
- #include <string>
- #include <vector>
- #include <cstdlib>
- #include "ns3/core-module.h"
- #include "ns3/network-module.h"
- #include "ns3/csma-module.h"
- #include "ns3/internet-module.h"
- #include "ns3/point-to-point-module.h"
- #include "ns3/applications-module.h"
- #include "ns3/ipv4-global-routing-helper.h"
- #include "ns3/bridge-helper.h"
- #include "ns3/netanim-module.h"
- #include "ns3/global-route-manager.h"
- #include "ns3/mobility-module.h"
- #include "ns3/assert.h"
- #include "ns3/flow-monitor-module.h"
- using namespace ns3;
- //For colorful console printing
- /*
- * Usage example :
- * std::cout << BOLD_CODE << "some bold text << END_CODE << std::endl;
- *
- * std::cout << YELLOW_CODE << BOLD_CODE << "some bold yellow text << END_CODE << std::endl;
- *
- */
- NS_LOG_COMPONENT_DEFINE ("Project");
- #define YELLOW_CODE "\033[33m"
- #define TEAL_CODE "\033[36m"
- #define BOLD_CODE "\033[1m"
- #define RED_CODE "\033[91m"
- #define END_CODE "\033[0m"
- double AppStartTime = 2.0001;
- double AppStopTime = 19.80001;
- std::string AppPacketRate ("40Kbps");
- uint16_t port = 9;
- void setFlow(NodeContainer a, NodeContainer b, int i, int j)
- {
- Ptr<UniformRandomVariable> x = CreateObject<UniformRandomVariable> ();
- x->SetAttribute ("Min", DoubleValue (0));
- x->SetAttribute ("Max", DoubleValue (1));
- double rn = x->GetValue ();
- Ptr<Node> n = b.Get (j);
- Ptr<Ipv4> ipv4 = n->GetObject<Ipv4> ();
- Ipv4InterfaceAddress ipv4_int_addr = ipv4->GetAddress (1, 0);
- Ipv4Address ip_addr = ipv4_int_addr.GetLocal ();
- OnOffHelper onoff ("ns3::UdpSocketFactory", InetSocketAddress (ip_addr, port)); // traffic flows from node[i] to node[j]
- onoff.SetConstantRate (DataRate (AppPacketRate));
- ApplicationContainer apps = onoff.Install (a.Get (i)); // traffic sources are installed on all nodes
- apps.Start (Seconds (AppStartTime + rn));
- apps.Stop (Seconds (AppStopTime));
- }
- uint32_t SentPackets = 0;
- uint32_t ReceivedPackets = 0;
- uint32_t LostPackets = 0;
- int
- main (int argc, char *argv[])
- {
- CommandLine cmd;
- uint32_t n1 = 2;
- uint32_t n2 = 2;
- uint32_t n3 = 2;
- double SimTime = 20.00;
- double SinkStartTime = 1.0001;
- double SinkStopTime = 19.90001;
- cmd.AddValue ("n1", "Number of LAN 1 nodes", n1);
- cmd.AddValue ("n2", "Number of LAN 2 nodes", n2);
- cmd.AddValue ("n3", "Number of LAN 3 nodes", n3);
- cmd.Parse (argc, argv);
- //LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
- //LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO);
- //For the first network
- NodeContainer lan1_nodes;
- NodeContainer switch1_nodes;
- //For the second network
- NodeContainer lan2_nodes;
- NodeContainer switch2_nodes;
- //For the third network
- NodeContainer lan3_nodes;
- NodeContainer switch3_nodes;
- //for the nodes in the middle.
- NodeContainer router_nodes;
- lan1_nodes.Create (n1);
- lan2_nodes.Create (n2);
- lan3_nodes.Create (n3);
- switch1_nodes.Create (1);
- switch2_nodes.Create (1);
- switch3_nodes.Create (1);
- router_nodes.Create (2);
- //Let's create LAN 1 by attaching a CsmaNetDevice to all the nodes on the LAN
- CsmaHelper csma1;
- csma1.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));
- csma1.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));
- //Actually attaching CsmaNetDevice to all LAN 1 nodes.
- lan1_nodes.Add (router_nodes.Get (0));
- NetDeviceContainer lan1Devices;
- NetDeviceContainer switch1Devices;
- for (uint32_t i = 0; i < n1+1; i++)
- {
- NetDeviceContainer link = csma1.Install(NodeContainer(lan1_nodes.Get (i), switch1_nodes));
- lan1Devices.Add(link.Get (0)); switch1Devices.Add(link.Get (1));
- }
- //Let's create LAN 2 by attaching a CsmaNetDevice to all the nodes on the LAN
- CsmaHelper csma2;
- csma2.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));
- csma2.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));
- //Actually attaching CsmaNetDevice to all LAN 2 nodes.
- lan2_nodes.Add (router_nodes.Get (0));
- NetDeviceContainer lan2Devices;
- NetDeviceContainer switch2Devices;
- for (uint32_t i = 0; i < n2+1; i++)
- {
- NetDeviceContainer link = csma2.Install(NodeContainer(lan2_nodes.Get (i), switch2_nodes));
- lan2Devices.Add(link.Get (0)); switch2Devices.Add(link.Get (1));
- }
- //Let's create LAN 3 by attaching a CsmaNetDevice to all the nodes on the LAN
- CsmaHelper csma3;
- csma3.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));
- csma3.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));
- //Actually attaching CsmaNetDevice to all LAN 3 nodes.
- lan3_nodes.Add (router_nodes.Get (1));
- NetDeviceContainer lan3Devices;
- NetDeviceContainer switch3Devices;
- for (uint32_t i = 0; i < n3+1; i++)
- {
- NetDeviceContainer link = csma3.Install(NodeContainer(lan3_nodes.Get (i), switch3_nodes));
- lan3Devices.Add(link.Get (0)); switch3Devices.Add(link.Get (1));
- }
- /* So far our two LANs are disjoint, r1 and r2 need to be connected */
- //A PointToPoint connection between the two routers
- PointToPointHelper pointToPoint;
- pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("10Mbps"));
- pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
- NetDeviceContainer routerDevices;
- routerDevices = pointToPoint.Install (router_nodes);
- //Setting IP addresses. Notice that router 1 & 2 are in LAN 1 & 2 respectively.
- InternetStackHelper stack;
- stack.Install (lan1_nodes);
- stack.Install (lan2_nodes.Get(0));
- stack.Install (lan2_nodes.Get(1));
- stack.Install (lan3_nodes);
- stack.Install (switch1_nodes);
- stack.Install (switch2_nodes);
- stack.Install (switch3_nodes);
- Ipv4AddressHelper address;
- //For LAN 1
- address.SetBase ("10.1.1.0", "255.255.255.0");
- Ipv4InterfaceContainer lan1interfaces;
- lan1interfaces = address.Assign (lan1Devices);
- //For LAN 2
- address.SetBase ("10.1.2.0", "255.255.255.0");
- Ipv4InterfaceContainer lan2interfaces;
- lan2interfaces = address.Assign (lan2Devices);
- //For LAN 3
- address.SetBase ("10.1.3.0", "255.255.255.0");
- Ipv4InterfaceContainer lan3interfaces;
- lan3interfaces = address.Assign (lan3Devices);
- //For PointToPoint
- address.SetBase ("10.1.100.0", "255.255.255.0");
- Ipv4InterfaceContainer routerInterfaces;
- routerInterfaces = address.Assign (routerDevices);
- BridgeHelper bridge1;
- bridge1.Install(switch1_nodes.Get(0), switch1Devices);
- BridgeHelper bridge2;
- bridge2.Install(switch2_nodes.Get(0), switch2Devices);
- BridgeHelper bridge3;
- bridge3.Install(switch3_nodes.Get(0), switch3Devices);
- for (int i = 0; i < int(n1); i++)
- {
- PacketSinkHelper sink ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), port));
- ApplicationContainer apps_sink = sink.Install (lan1_nodes.Get (i)); // sink is installed on all nodes
- apps_sink.Start (Seconds (SinkStartTime));
- apps_sink.Stop (Seconds (SinkStopTime));
- }
- for (int i = 0; i < int(n2); i++)
- {
- PacketSinkHelper sink ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), port));
- ApplicationContainer apps_sink = sink.Install (lan2_nodes.Get (i)); // sink is installed on all nodes
- apps_sink.Start (Seconds (SinkStartTime));
- apps_sink.Stop (Seconds (SinkStopTime));
- }
- for (int i = 0; i < int(n3); i++)
- {
- PacketSinkHelper sink ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), port));
- ApplicationContainer apps_sink = sink.Install (lan3_nodes.Get (i)); // sink is installed on all nodes
- apps_sink.Start (Seconds (SinkStartTime));
- apps_sink.Stop (Seconds (SinkStopTime));
- }
- for (int i = 0; i < int(n1); i++)
- {
- for (int j = 0; j < int(n1); j++)
- {
- if (i != j)
- setFlow(lan1_nodes, lan1_nodes, i, j);
- }
- for (int j = 0; j < int(n2); j++)
- {
- setFlow(lan1_nodes, lan2_nodes, i, j);
- }
- for (int j = 0; j < int(n3); j++)
- {
- setFlow(lan1_nodes, lan3_nodes, i, j);
- }
- }
- for (int i = 0; i < int(n2); i++)
- {
- for (int j = 0; j < int(n1); j++)
- {
- setFlow(lan2_nodes, lan1_nodes, i, j);
- }
- for (int j = 0; j < int(n2); j++)
- {
- if (i != j)
- setFlow(lan2_nodes, lan2_nodes, i, j);
- }
- for (int j = 0; j < int(n3); j++)
- {
- setFlow(lan2_nodes, lan3_nodes, i, j);
- }
- }
- for (int i = 0; i < int(n3); i++)
- {
- for (int j = 0; j < int(n1); j++)
- {
- setFlow(lan3_nodes, lan1_nodes, i, j);
- }
- for (int j = 0; j < int(n2); j++)
- {
- setFlow(lan3_nodes, lan2_nodes, i, j);
- }
- for (int j = 0; j < int(n3); j++)
- {
- if (i != j)
- setFlow(lan3_nodes, lan3_nodes, i, j);
- }
- }
- //For routers to be able to forward packets, they need to have routing rules.
- Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
- csma1.EnablePcap("lan1", lan1Devices);
- csma2.EnablePcap("lan2", lan2Devices);
- csma3.EnablePcap("lan3", lan3Devices);
- pointToPoint.EnablePcapAll("routers");
- pointToPoint.EnableAscii("ascii-p2p", router_nodes);
- AnimationInterface anim ("project_v3.xml");
- anim.SetConstantPosition(router_nodes.Get(0), 60.0, 45.0);
- anim.SetConstantPosition(router_nodes.Get(1), 60.0, 55.0);
- anim.SetConstantPosition(lan1_nodes.Get(0), 30.0, 25.0);
- anim.SetConstantPosition(lan1_nodes.Get(1), 50.0, 25.0);
- anim.SetConstantPosition(switch1_nodes.Get(0), 40.0, 35.0);
- anim.SetConstantPosition(lan2_nodes.Get(0), 70.0, 25.0);
- anim.SetConstantPosition(lan2_nodes.Get(1), 90.0, 25.0);
- anim.SetConstantPosition(switch2_nodes.Get(0), 80.0, 35.0);
- anim.SetConstantPosition(lan3_nodes.Get(0), 30.0, 75.0);
- anim.SetConstantPosition(lan3_nodes.Get(1), 50.0, 75.0);
- anim.SetConstantPosition(switch3_nodes.Get(0), 40.0, 65.0);
- FlowMonitorHelper flowmon;
- Ptr<FlowMonitor> monitor = flowmon.InstallAll();
- NS_LOG_INFO ("Running Simulation...");
- Simulator::Stop (Seconds (SimTime));
- Simulator::Run ();
- // Performance Metric Calculations
- int j=0;
- float AvgThroughput = 0;
- Time Jitter;
- Time Delay;
- Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier> (flowmon.GetClassifier ());
- std::map<FlowId, FlowMonitor::FlowStats> stats = monitor->GetFlowStats ();
- for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator iter = stats.begin (); iter != stats.end (); ++iter)
- {
- Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (iter->first);
- NS_LOG_UNCOND("\n----- Flow ID: " << iter->first << " -----");
- NS_LOG_UNCOND("Src Addr: " << t.sourceAddress << " | Dst Addr: "<< t.destinationAddress);
- NS_LOG_UNCOND("Sent Packets = " << iter->second.txPackets);
- NS_LOG_UNCOND("Received Packets = " << iter->second.rxPackets);
- NS_LOG_UNCOND("Lost Packets = " << iter->second.txPackets-iter->second.rxPackets);
- NS_LOG_UNCOND("Packet delivery ratio = " << iter->second.rxPackets*100/iter->second.txPackets << " %");
- NS_LOG_UNCOND("Packet loss ratio = " << (iter->second.txPackets-iter->second.rxPackets)*100/iter->second.txPackets << " %");
- NS_LOG_UNCOND("Delay = " << iter->second.delaySum);
- NS_LOG_UNCOND("Jitter = " << iter->second.jitterSum);
- NS_LOG_UNCOND("Throughput = " << iter->second.rxBytes * 8.0/(iter->second.timeLastRxPacket.GetSeconds()-iter->second.timeFirstTxPacket.GetSeconds())/1024<<" Kbps");
- SentPackets = SentPackets +(iter->second.txPackets);
- ReceivedPackets = ReceivedPackets + (iter->second.rxPackets);
- LostPackets = LostPackets + (iter->second.txPackets-iter->second.rxPackets);
- AvgThroughput = AvgThroughput + (iter->second.rxBytes * 8.0/(iter->second.timeLastRxPacket.GetSeconds()-iter->second.timeFirstTxPacket.GetSeconds())/1024);
- Delay = Delay + (iter->second.delaySum);
- Jitter = Jitter + (iter->second.jitterSum);
- j = j + 1;
- }
- AvgThroughput = AvgThroughput/j;
- NS_LOG_UNCOND("\n-------- Total Results of the simulation --------" << std::endl);
- NS_LOG_UNCOND("Total sent packets = " << SentPackets);
- NS_LOG_UNCOND("Total Received Packets = " << ReceivedPackets);
- NS_LOG_UNCOND("Total Lost Packets = " << LostPackets);
- NS_LOG_UNCOND("Packet Loss ratio = " << ((LostPackets*100)/SentPackets)<< " %");
- NS_LOG_UNCOND("Packet delivery ratio = " << ((ReceivedPackets*100)/SentPackets)<< " %");
- NS_LOG_UNCOND("Average Throughput = " << AvgThroughput<< " Kbps");
- NS_LOG_UNCOND("End to End Delay = " << Delay);
- NS_LOG_UNCOND("End to End Jitter delay = " << Jitter);
- NS_LOG_UNCOND("Total Flow id = " << j);
- monitor->SerializeToXmlFile("project_v3_flow.xml", true, true);
- Simulator::Destroy ();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement