Advertisement
MdSadmanSiraj

ns3_2_lans_2_routers

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