Guest User

Untitled

a guest
Jun 19th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.79 KB | None | 0 0
  1. #include "ns3/command-line.h"
  2. #include "ns3/string.h"
  3. #include "ns3/pointer.h"
  4. #include "ns3/log.h"
  5. #include "ns3/yans-wifi-helper.h"
  6. #include "ns3/ssid.h"
  7. #include "ns3/mobility-helper.h"
  8. #include "ns3/internet-stack-helper.h"
  9. #include "ns3/ipv4-address-helper.h"
  10. #include "ns3/udp-client-server-helper.h"
  11. #include "ns3/on-off-helper.h"
  12. #include "ns3/yans-wifi-channel.h"
  13. #include "ns3/wifi-net-device.h"
  14. #include "ns3/qos-txop.h"
  15. #include "ns3/wifi-mac.h"
  16. #include "ns3/flow-monitor-helper.h"
  17. #include "ns3/ipv4-flow-classifier.h"
  18.  
  19. // BSS A (36) BSS B (36)
  20. // * * * *
  21. // | | | |
  22. // AP A STA A AP B STA B
  23. //
  24. // The configuration is the following on the 2 networks:
  25. // - STA A sends AC_BE traffic to AP A with default AC_BE TXOP value of 0 (1 MSDU);
  26. // - STA B sends AC_BE traffic to AP B with non-default AC_BE TXOP of 3.008 ms;
  27. //
  28. // The user can select the distance between the stations and the APs, can enable/disable the RTS/CTS mechanism
  29. // and can choose the payload size and the simulation duration.
  30. // Example: ./waf --run "scratch/80211e-txop --cwMin=10 --cwMax=20 --TXOP=1280"
  31.  
  32.  
  33.  
  34. using namespace ns3;
  35.  
  36. NS_LOG_COMPONENT_DEFINE ("80211eTxop");
  37.  
  38. int main (int argc, char *argv[])
  39. {
  40. uint32_t payloadSize = 1472; //bytes
  41. double simulationTime = 10; //seconds
  42. // double distance = 5; //meters
  43. bool enablePcap = 0;
  44. bool secondCase = 0;
  45. bool verifyResults = 0; //used for regression
  46. uint32_t cwMin = 0;
  47. uint32_t cwMax = 0;
  48. uint32_t TXOP = 0;
  49.  
  50. CommandLine cmd;
  51. // cmd.AddValue ("payloadSize", "Payload size in bytes", payloadSize);
  52. // cmd.AddValue ("simulationTime", "Simulation time in seconds", simulationTime);
  53. // cmd.AddValue ("distance", "Distance in meters between the station and the access point", distance);
  54. // cmd.AddValue ("enablePcap", "Enable/disable pcap file generation", enablePcap);
  55. // cmd.AddValue ("verifyResults", "Enable/disable results verification at the end of the simulation", verifyResults);
  56. // cmd.AddValue ("enablePcap", "Enable/disable pcap file generation", enablePcap);
  57. cmd.AddValue ("cwMin", "Set cwMin for the simulation network B", cwMin);
  58. cmd.AddValue ("cwMax", "Set cwMax for the simulation network B", cwMax);
  59. cmd.AddValue ("TXOP", "Set TXOP for the simulation network B", TXOP);
  60. cmd.Parse (argc, argv);
  61.  
  62. NodeContainer wifiStaNodes;
  63. wifiStaNodes.Create (2);
  64. NodeContainer wifiApNodes;
  65. wifiApNodes.Create (2);
  66.  
  67. YansWifiChannelHelper channel = YansWifiChannelHelper::Default ();
  68. YansWifiPhyHelper phy = YansWifiPhyHelper::Default ();
  69. phy.SetPcapDataLinkType (WifiPhyHelper::DLT_IEEE802_11_RADIO);
  70. phy.SetChannel (channel.Create ());
  71.  
  72. WifiHelper wifi; //the default standard of 802.11a will be selected by this helper since the program doesn't specify another one
  73. wifi.SetRemoteStationManager ("ns3::IdealWifiManager");
  74. WifiMacHelper mac;
  75.  
  76. NetDeviceContainer staDeviceA, staDeviceB, apDeviceA, apDeviceB;
  77. Ssid ssid;
  78.  
  79. //Network A
  80. ssid = Ssid ("network-A");
  81. phy.Set ("ChannelNumber", UintegerValue (36));
  82. mac.SetType ("ns3::StaWifiMac",
  83. "QosSupported", BooleanValue (true),
  84. "Ssid", SsidValue (ssid));
  85. staDeviceA = wifi.Install (phy, mac, wifiStaNodes.Get (0));
  86.  
  87. mac.SetType ("ns3::ApWifiMac",
  88. "QosSupported", BooleanValue (true),
  89. "Ssid", SsidValue (ssid),
  90. "EnableBeaconJitter", BooleanValue (false));
  91. apDeviceA = wifi.Install (phy, mac, wifiApNodes.Get (0));
  92.  
  93. //Network B
  94. ssid = Ssid ("network-B");
  95. phy.Set ("ChannelNumber", UintegerValue (36));
  96. mac.SetType ("ns3::StaWifiMac",
  97. "QosSupported", BooleanValue (true),
  98. "Ssid", SsidValue (ssid));
  99.  
  100. staDeviceB = wifi.Install (phy, mac, wifiStaNodes.Get (1));
  101.  
  102. mac.SetType ("ns3::ApWifiMac",
  103. "QosSupported", BooleanValue (true),
  104. "Ssid", SsidValue (ssid),
  105. "EnableBeaconJitter", BooleanValue (false));
  106. apDeviceB = wifi.Install (phy, mac, wifiApNodes.Get (1));
  107.  
  108. //Modify EDCA configuration (TXOP limit) for AC_BE
  109. Ptr<NetDevice> dev = wifiApNodes.Get (1)->GetDevice (0);
  110. Ptr<WifiNetDevice> wifi_dev = DynamicCast<WifiNetDevice> (dev);
  111. Ptr<WifiMac> wifi_mac = wifi_dev->GetMac ();
  112. PointerValue ptr;
  113. Ptr<QosTxop> edca;
  114. wifi_mac->GetAttribute ("BE_Txop", ptr);
  115. edca = ptr.Get<QosTxop> ();
  116. edca->SetTxopLimit (MicroSeconds (TXOP));
  117. edca->SetMinCw (cwMin);
  118. edca->SetMaxCw (cwMax);
  119.  
  120. /* Setting mobility model */
  121. MobilityHelper mobility;
  122. Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
  123. mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
  124.  
  125. //Set position for APs
  126. positionAlloc->Add (Vector (0.0, 0.0, 0.0));
  127. positionAlloc->Add (Vector (10.0, 0.0, 0.0));
  128. //Set position for STAs
  129. positionAlloc->Add (Vector (3, 0.0, 0.0));
  130. positionAlloc->Add (Vector (7, 0.0, 0.0));
  131. //Remark: while we set these positions 10 meters apart, the networks do not interact
  132. //and the only variable that affects transmission performance is the distance.
  133.  
  134. mobility.SetPositionAllocator (positionAlloc);
  135. mobility.Install (wifiApNodes);
  136. mobility.Install (wifiStaNodes);
  137.  
  138. /* Internet stack */
  139. InternetStackHelper stack;
  140. stack.Install (wifiApNodes);
  141. stack.Install (wifiStaNodes);
  142.  
  143. Ipv4AddressHelper address;
  144. address.SetBase ("192.168.1.0", "255.255.255.0");
  145. Ipv4InterfaceContainer StaInterfaceA;
  146. StaInterfaceA = address.Assign (staDeviceA);
  147. Ipv4InterfaceContainer ApInterfaceA;
  148. ApInterfaceA = address.Assign (apDeviceA);
  149.  
  150. address.SetBase ("192.168.2.0", "255.255.255.0");
  151. Ipv4InterfaceContainer StaInterfaceB;
  152. StaInterfaceB = address.Assign (staDeviceB);
  153. Ipv4InterfaceContainer ApInterfaceB;
  154. ApInterfaceB = address.Assign (apDeviceB);
  155.  
  156. /* Setting applications */
  157. uint16_t port = 5001;
  158. UdpServerHelper serverA (port);
  159. ApplicationContainer serverAppA = serverA.Install (wifiApNodes.Get (0));
  160. serverAppA.Start (Seconds (0.0));
  161. serverAppA.Stop (Seconds (simulationTime + 1));
  162.  
  163. InetSocketAddress destA (ApInterfaceA.GetAddress (0), port);
  164. // Zmiana BE na VI dla network A
  165. // destA.SetTos (0x70); //AC_BE
  166. destA.SetTos (0xb8); //AC_VI
  167.  
  168.  
  169. OnOffHelper clientA ("ns3::UdpSocketFactory", destA);
  170. clientA.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
  171. clientA.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
  172. clientA.SetAttribute ("DataRate", StringValue ("37000kb/s"));
  173. clientA.SetAttribute ("PacketSize", UintegerValue (payloadSize));
  174.  
  175.  
  176. uint16_t port2 = 5002;
  177. UdpServerHelper serverAB (port2);
  178. ApplicationContainer serverAppAB = serverAB.Install (wifiApNodes.Get (0));
  179. if (secondCase) {
  180. serverAppAB.Start (Seconds (0.0));
  181. serverAppAB.Stop (Seconds (simulationTime + 1));
  182. InetSocketAddress destAB (ApInterfaceA.GetAddress (0), port2);
  183. // Zmiana BE na VI dla network A
  184. destAB.SetTos (0x70); //AC_BE
  185. // destAB.SetTos (0xb8); //AC_VI
  186.  
  187. OnOffHelper clientAB ("ns3::UdpSocketFactory", destAB);
  188. clientAB.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
  189. clientAB.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
  190. clientAB.SetAttribute ("DataRate", StringValue ("1kb/s"));
  191. clientAB.SetAttribute ("PacketSize", UintegerValue (payloadSize));
  192. ApplicationContainer clientAppAB = clientAB.Install (wifiStaNodes.Get (0));
  193. clientAppAB.Start (Seconds (1.0));
  194. clientAppAB.Stop (Seconds (simulationTime + 1));
  195. }
  196.  
  197.  
  198. ApplicationContainer clientAppA = clientA.Install (wifiStaNodes.Get (0));
  199. clientAppA.Start (Seconds (1.0));
  200. clientAppA.Stop (Seconds (simulationTime + 1));
  201.  
  202. UdpServerHelper serverB (port);
  203. ApplicationContainer serverAppB = serverB.Install (wifiApNodes.Get (1));
  204. serverAppB.Start (Seconds (0.0));
  205. serverAppB.Stop (Seconds (simulationTime + 1));
  206.  
  207. InetSocketAddress destB (ApInterfaceB.GetAddress (0), port);
  208. // Zmiana BE na VI dla network B
  209. destB.SetTos (0x70); //AC_BE
  210. // destB.SetTos (0xb8); //AC_VI
  211.  
  212. OnOffHelper clientB ("ns3::UdpSocketFactory", destB);
  213. clientB.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
  214. clientB.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
  215. // clientB.SetAttribute ("DataRate", StringValue ("100000kb/s"));
  216. clientB.SetAttribute ("DataRate", StringValue ("37000kb/s"));
  217. clientB.SetAttribute ("PacketSize", UintegerValue (payloadSize));
  218.  
  219. ApplicationContainer clientAppB = clientB.Install (wifiStaNodes.Get (1));
  220. clientAppB.Start (Seconds (1.0));
  221. clientAppB.Stop (Seconds (simulationTime + 1));
  222.  
  223.  
  224. AsciiTraceHelper ascii;
  225. phy.EnableAsciiAll (ascii.CreateFileStream ("out.tr"));
  226. phy.EnableAscii (ascii.CreateFileStream ("staA.tr"), staDeviceA.Get (0));
  227. phy.EnableAscii (ascii.CreateFileStream ("staB.tr"), staDeviceB.Get (0));
  228.  
  229. FlowMonitorHelper flowmon_helper;
  230. Ptr<FlowMonitor> monitor = flowmon_helper.InstallAll ();
  231. monitor->SetAttribute ("StartTime", TimeValue (Seconds (0.0)) ); //Time
  232. // from which flowmonitor statistics are gathered.
  233. monitor->SetAttribute ("DelayBinWidth", DoubleValue (0.001));
  234. monitor->SetAttribute ("JitterBinWidth", DoubleValue (0.001));
  235. monitor->SetAttribute ("PacketSizeBinWidth", DoubleValue (20));
  236.  
  237.  
  238.  
  239.  
  240.  
  241. if (enablePcap)
  242. {
  243. phy.EnablePcap ("AP_A", apDeviceA.Get (0));
  244. phy.EnablePcap ("STA_A", staDeviceA.Get (0));
  245. phy.EnablePcap ("AP_B", apDeviceB.Get (0));
  246. phy.EnablePcap ("STA_B", staDeviceB.Get (0));
  247.  
  248.  
  249.  
  250. }
  251.  
  252. Simulator::Stop (Seconds (simulationTime + 1));
  253. Simulator::Run ();
  254. Simulator::Destroy ();
  255.  
  256. monitor->SerializeToXmlFile ("out.xml", true, true);
  257.  
  258. /* Show results */
  259. uint64_t totalPacketsThrough = DynamicCast<UdpServer> (serverAppA.Get (0))->GetReceived ();
  260. double throughput = totalPacketsThrough * payloadSize * 8 / (simulationTime * 1000000.0);
  261. std::cout << "Network A - Victim AC_VI: Throughput with defaults: " << throughput << " Mbit/s" << '\n';
  262. if (verifyResults && (throughput < 28 || throughput > 29))
  263. {
  264. NS_LOG_ERROR ("Obtained throughput " << throughput << " is not in the expected boundaries!");
  265. exit (1);
  266. }
  267.  
  268. if (secondCase) {
  269. totalPacketsThrough = DynamicCast<UdpServer> (serverAppAB.Get (0))->GetReceived ();
  270. throughput = totalPacketsThrough * payloadSize * 8 / (simulationTime * 1000000.0);
  271. std::cout << "Network A - Victim AC_BE: Throughput with defaults: " << throughput << " Mbit/s" << '\n';
  272. }
  273.  
  274.  
  275. totalPacketsThrough = DynamicCast<UdpServer> (serverAppB.Get (0))->GetReceived ();
  276. throughput = totalPacketsThrough * payloadSize * 8 / (simulationTime * 1000000.0);
  277. std::cout << "Network B - Attacker AC_BE: Throughput with cwMin " << cwMin << ", cwMax "<< cwMax << " and TXOP limit (" << TXOP << "ms): " << throughput << " Mbit/s" << '\n';
  278. if (verifyResults && (throughput < 35.5 || throughput > 36.5))
  279. {
  280. NS_LOG_ERROR ("Obtained throughput " << throughput << " is not in the expected boundaries!");
  281. exit (1);
  282. }
  283.  
  284. Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier>
  285. (flowmon_helper.GetClassifier ());
  286. //iterate over traffic flows
  287. std::map<FlowId, FlowMonitor::FlowStats> stats = monitor->GetFlowStats ();
  288. for (std::map<FlowId, FlowMonitor::FlowStats>::iterator flow = stats.begin ();
  289. flow != stats.end (); flow++)
  290. {
  291. Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (flow->first);
  292. std::cout << t.destinationAddress << ":" << t.destinationPort << "\n";
  293. std::cout<<"meanDelayInMS = " << flow->second.delaySum / (flow->second.rxPackets * 1000000.0) <<std::endl;
  294. std::cout<<"meanJitter1000 = " << (flow->second.jitterSum / (flow->second.rxPackets)) / 1000 <<std::endl;
  295. std::cout<<"meanJitter1000000 = " << (flow->second.jitterSum / (flow->second.rxPackets)) / 1000000 <<std::endl;
  296. std::cout<<"JitterSum = " << flow->second.jitterSum <<std::endl;
  297. std::cout<<"JitterSumInMS = " << flow->second.jitterSum / 1000000.0 <<std::endl;
  298. }
  299.  
  300.  
  301.  
  302. return 0;
  303. }
Add Comment
Please, Sign In to add comment