Advertisement
MdSadmanSiraj

ns3_2_LANS_2_ROUTERS.cc

Nov 29th, 2022
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.27 KB | None | 0 0
  1. #include "ns3/core-module.h"
  2. #include "ns3/network-module.h"
  3. #include "ns3/csma-module.h"
  4. #include "ns3/internet-module.h"
  5. #include "ns3/point-to-point-module.h"
  6. #include "ns3/applications-module.h"
  7. #include "ns3/ipv4-global-routing-helper.h"
  8. #include "ns3/netanim-module.h"
  9.  
  10. using namespace ns3;
  11.  
  12. //For colorful console printing
  13. /*
  14. * Usage example :
  15. * std::cout << BOLD_CODE << "some bold text << END_CODE << std::endl;
  16. *
  17. * std::cout << YELLOW_CODE << BOLD_CODE << "some bold yellow text << END_CODE << std::endl;
  18. *
  19. */
  20.  
  21. NS_LOG_COMPONENT_DEFINE ("Project");
  22.  
  23. #define YELLOW_CODE "\033[33m"
  24. #define TEAL_CODE "\033[36m"
  25. #define BOLD_CODE "\033[1m"
  26. #define RED_CODE "\033[91m"
  27. #define END_CODE "\033[0m"
  28.  
  29. uint32_t total_client_tx = 0;
  30. uint32_t total_client_rx = 0;
  31. uint32_t total_server_tx = 0;
  32. uint32_t total_server_rx = 0;
  33.  
  34. void
  35. CheckQueueSize (std::string context, uint32_t before, uint32_t after)
  36. {
  37. std::cout << YELLOW_CODE << context << END_CODE << std::endl;
  38. std::cout << "\tTxQueue Size = " << after << std::endl;
  39. }
  40.  
  41. void
  42. BackoffTrace (std::string context, Ptr<const Packet> packet)
  43. {
  44. std::cout << YELLOW_CODE << context << END_CODE << std::endl;
  45. EthernetHeader hdr;
  46. if (packet->PeekHeader (hdr))
  47. {
  48. std::cout << "\t" << Now() << " Packet from " << hdr.GetSource () << " to " << hdr.GetDestination () << " is experiencing backoff" << std::endl;
  49. }
  50. }
  51.  
  52. void ClientTx (std::string context, Ptr<const Packet> packet)
  53. {
  54. total_client_tx++;
  55. }
  56. void ClientRx (std::string context, Ptr<const Packet> packet)
  57. {
  58. total_client_rx++;
  59. }
  60. void ServerTx (std::string context, Ptr<const Packet> packet)
  61. {
  62. total_server_tx++;
  63. }
  64. void ServerRx (std::string context, Ptr<const Packet> packet)
  65. {
  66. total_server_rx++;
  67. }
  68.  
  69. int
  70. main (int argc, char *argv[])
  71. {
  72. CommandLine cmd;
  73.  
  74. uint32_t n1 = 4;
  75. uint32_t n2 = 4;
  76.  
  77. cmd.AddValue ("n1", "Number of LAN 1 nodes", n1);
  78. cmd.AddValue ("n2", "Number of LAN 2 nodes", n2);
  79.  
  80. cmd.Parse (argc, argv);
  81.  
  82. //LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
  83. //LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO);
  84.  
  85. //For the first network
  86. NodeContainer lan1_nodes;
  87.  
  88. //For the second network
  89. NodeContainer lan2_nodes;
  90.  
  91. //for the nodes in the middle.
  92. NodeContainer router_nodes;
  93.  
  94. lan1_nodes.Create (n1);
  95. lan2_nodes.Create (n2);
  96. router_nodes.Create (2);
  97.  
  98. //Let's create LAN 1 by attaching a CsmaNetDevice to all the nodes on the LAN
  99. CsmaHelper csma1;
  100. csma1.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));
  101. csma1.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));
  102. //Router 1 is accessible on LAN 1, so we add it to the list nodes.
  103. lan1_nodes.Add (router_nodes.Get (0));
  104. //Actually attaching CsmaNetDevice to all LAN 1 nodes.
  105. NetDeviceContainer lan1Devices;
  106. lan1Devices = csma1.Install (lan1_nodes);
  107.  
  108. //Doing the same for LAN 2
  109. CsmaHelper csma2;
  110. csma2.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));
  111. csma2.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));
  112. //Router 2 is on LAN 2, so we add it to the node container
  113. lan2_nodes.Add (router_nodes.Get (1));
  114.  
  115. NetDeviceContainer lan2Devices;
  116. lan2Devices = csma2.Install (lan2_nodes);
  117.  
  118. /* So far our two LANs are disjoint, r1 and r2 need to be connected */
  119. //A PointToPoint connection between the two routers
  120. PointToPointHelper pointToPoint;
  121. pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("10Mbps"));
  122. pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
  123.  
  124. NetDeviceContainer routerDevices;
  125. routerDevices = pointToPoint.Install (router_nodes);
  126.  
  127. //Setting IP addresses. Notice that router 1 & 2 are in LAN 1 & 2 respectively.
  128. InternetStackHelper stack;
  129. stack.Install (lan1_nodes);
  130. stack.Install (lan2_nodes);
  131.  
  132. Ipv4AddressHelper address;
  133. //For LAN 1
  134. address.SetBase ("10.1.1.0", "255.255.255.0");
  135. Ipv4InterfaceContainer lan1interfaces;
  136. lan1interfaces = address.Assign (lan1Devices);
  137. //For LAN 2
  138. address.SetBase ("10.1.2.0", "255.255.255.0");
  139. Ipv4InterfaceContainer lan2interfaces;
  140. lan2interfaces = address.Assign (lan2Devices);
  141. //For PointToPoint
  142. address.SetBase ("10.1.100.0", "255.255.255.0");
  143. Ipv4InterfaceContainer routerInterfaces;
  144. routerInterfaces = address.Assign (routerDevices);
  145.  
  146. //Let's install a UdpEchoServer on all nodes of LAN2
  147. UdpEchoServerHelper echoServer (9);
  148. ApplicationContainer serverApps = echoServer.Install (lan2_nodes);
  149. serverApps.Start (Seconds (0));
  150. serverApps.Stop (Seconds (10));
  151.  
  152. //Let's create UdpEchoClients in all LAN1 nodes.
  153. UdpEchoClientHelper echoClient (lan2interfaces.GetAddress (0), 9);
  154. echoClient.SetAttribute ("MaxPackets", UintegerValue (100));
  155. echoClient.SetAttribute ("Interval", TimeValue (MilliSeconds (200)));
  156. echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
  157.  
  158. //We'll install UdpEchoClient on two nodes in lan1 nodes
  159. NodeContainer clientNodes (lan1_nodes.Get(0), lan1_nodes.Get(1));
  160.  
  161. ApplicationContainer clientApps = echoClient.Install (clientNodes);
  162. clientApps.Start (Seconds (1));
  163. clientApps.Stop (Seconds (10));
  164.  
  165. //For routers to be able to forward packets, they need to have routing rules.
  166. Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
  167.  
  168. csma1.EnablePcap("lan1", lan1Devices);
  169. csma2.EnablePcap("lan2", lan2Devices);
  170. pointToPoint.EnablePcapAll("routers");
  171. pointToPoint.EnableAscii("ascii-p2p", router_nodes);
  172.  
  173. //Config::Connect("/NodeList/*/DeviceList/*/$ns3::PointToPointNetDevice/TxQueue/PacketsInQueue", MakeCallback(&CheckQueueSize));
  174. //Config::Connect("/NodeList/*/DeviceList/*/$ns3::CsmaNetDevice/TxQueue/PacketsInQueue", MakeCallback(&CheckQueueSize));
  175.  
  176.  
  177. Config::Connect("/NodeList/*/DeviceList/*/$ns3::CsmaNetDevice/MacTxBackoff", MakeCallback(&BackoffTrace));
  178.  
  179. Config::Connect("/NodeList/*/ApplicationList/*/$ns3::UdpEchoClient/Tx", MakeCallback(&ClientTx));
  180. Config::Connect("/NodeList/*/ApplicationList/*/$ns3::UdpEchoClient/Rx", MakeCallback(&ClientRx));
  181. //Config::Connect("/NodeList/*/ApplicationList/*/$ns3::UdpEchoServer/Tx", MakeCallback(&ServerTx));
  182. Config::Connect("/NodeList/*/ApplicationList/*/$ns3::UdpEchoServer/Rx", MakeCallback(&ServerRx));
  183.  
  184. // lan1_nodes.Create (n1);
  185. // lan2_nodes.Create (n2);
  186. // router_nodes.Create (2);
  187.  
  188. AnimationInterface anim ("project_v1.xml");
  189.  
  190. anim.SetConstantPosition(router_nodes.Get(0), 60.0, 35.0);
  191.  
  192. anim.SetConstantPosition(lan1_nodes.Get(0), 30.0, 25.0);
  193. anim.SetConstantPosition(lan1_nodes.Get(1), 50.0, 25.0);
  194. anim.SetConstantPosition(lan1_nodes.Get(2), 70.0, 25.0);
  195. anim.SetConstantPosition(lan1_nodes.Get(3), 90.0, 25.0);
  196.  
  197. anim.SetConstantPosition(lan2_nodes.Get(0), 30.0, 75.0);
  198. anim.SetConstantPosition(lan2_nodes.Get(1), 50.0, 75.0);
  199. anim.SetConstantPosition(lan2_nodes.Get(2), 70.0, 75.0);
  200. anim.SetConstantPosition(lan2_nodes.Get(3), 90.0, 75.0);
  201.  
  202. anim.SetConstantPosition(router_nodes.Get(1), 60.0, 65.0);
  203.  
  204. NS_LOG_INFO ("Running Simulation...");
  205.  
  206. Simulator::Stop (Seconds (20));
  207. Simulator::Run ();
  208.  
  209. std::cout << "Client Tx: " << total_client_tx << "\tClient Rx: " << total_client_rx << std::endl;
  210. std::cout << "Server Rx: " << total_server_rx << std::endl;
  211.  
  212. Simulator::Destroy ();
  213. return 0;
  214.  
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement