LEFT | RIGHT |
(no file at all) | |
| 1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ |
| 2 /* |
| 3 * Copyright (c) 2010 Network Security Lab, University of Washington, Seattle. |
| 4 * |
| 5 * This program is free software; you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 2 as |
| 7 * published by the Free Software Foundation; |
| 8 * |
| 9 * This program is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with this program; if not, write to the Free Software |
| 16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 * |
| 18 * Authors: He Wu <mdzz@u.washington.edu> |
| 19 */ |
| 20 |
| 21 // ns3 |
| 22 #include "ns3/log.h" |
| 23 #include "ns3/test.h" |
| 24 #include "ns3/node.h" |
| 25 #include "ns3/simulator.h" |
| 26 #include "ns3/double.h" |
| 27 #include "ns3/config.h" |
| 28 #include "ns3/string.h" |
| 29 #include "ns3/nstime.h" |
| 30 #include "ns3/type-id.h" |
| 31 #include "ns3/address.h" |
| 32 // wifi |
| 33 #include "ns3/nsl-wifi-helper.h" |
| 34 #include "ns3/nqos-wifi-mac-helper.h" |
| 35 // mobility |
| 36 #include "ns3/mobility-helper.h" |
| 37 #include "ns3/constant-position-mobility-model.h" |
| 38 #include "ns3/position-allocator.h" |
| 39 // Internet |
| 40 #include "ns3/internet-stack-helper.h" |
| 41 #include "ns3/ipv4-address-helper.h" |
| 42 #include "ns3/inet-socket-address.h" |
| 43 #include "ns3/olsr-helper.h" |
| 44 #include "ns3/ipv4-list-routing-helper.h" |
| 45 #include "ns3/ipv4-static-routing-helper.h" |
| 46 // energy |
| 47 #include "ns3/basic-energy-source.h" |
| 48 #include "ns3/wifi-radio-energy-model.h" |
| 49 #include "ns3/basic-energy-source-helper.h" |
| 50 #include "ns3/wifi-radio-energy-model-helper.h" |
| 51 // jamming |
| 52 #include "ns3/jammer-helper.h" |
| 53 #include "ns3/jamming-mitigation-helper.h" |
| 54 #include "ns3/constant-jammer.h" |
| 55 #include "ns3/random-jammer.h" |
| 56 #include "ns3/eavesdropper-jammer.h" |
| 57 #include "ns3/reactive-jammer.h" |
| 58 #include "ns3/mitigate-by-channel-hop.h" |
| 59 // other |
| 60 #include <math.h> |
| 61 |
| 62 namespace ns3 { |
| 63 |
| 64 NS_LOG_COMPONENT_DEFINE ("JammingComponentsTestSuite"); |
| 65 |
| 66 // -------------------------------------------------------------------------- // |
| 67 |
| 68 /** |
| 69 * \brief Traffic generator. |
| 70 * \param socket Pointer to socket. |
| 71 * \param pktSize Packet size. |
| 72 * \param n Pointer to node. |
| 73 * \param pktCount Number of packets to generate. |
| 74 * \param pktInterval Packet sending interval. |
| 75 */ |
| 76 static void |
| 77 GenerateTraffic (Ptr<Socket> socket, uint32_t pktSize, Ptr<Node> n, |
| 78 uint32_t pktCount, Time pktInterval) |
| 79 { |
| 80 if (pktCount > 0) |
| 81 { |
| 82 socket->Send (Create<Packet> (pktSize)); |
| 83 Simulator::Schedule (pktInterval, &GenerateTraffic, socket, pktSize, n, |
| 84 pktCount - 1, pktInterval); |
| 85 } |
| 86 else |
| 87 { |
| 88 socket->Close (); |
| 89 } |
| 90 } |
| 91 |
| 92 /** |
| 93 * \brief Packet receiving sink. |
| 94 * \param socket Pointer to socket. |
| 95 */ |
| 96 void |
| 97 ReceivePacket (Ptr<Socket> socket) |
| 98 { |
| 99 Address addr; |
| 100 socket->GetSockName (addr); |
| 101 InetSocketAddress iaddr = InetSocketAddress::ConvertFrom (addr); |
| 102 NS_LOG_UNCOND ("--\nReceived one packet! Socket: "<< iaddr.GetIpv4 () << |
| 103 " port: " << iaddr.GetPort () << "\n--"); |
| 104 } |
| 105 |
| 106 // -------------------------------------------------------------------------- // |
| 107 |
| 108 /** |
| 109 * Test case of installing different types of jammers onto a node. Also tests |
| 110 * hidden installation of WirelessModuleUtility object. |
| 111 */ |
| 112 class JammerTypeTest : public TestCase |
| 113 { |
| 114 public: |
| 115 JammerTypeTest (); |
| 116 virtual ~JammerTypeTest (); |
| 117 |
| 118 private: |
| 119 void DoRun (void); |
| 120 |
| 121 /** |
| 122 * \brief Install and run different types of jammer. |
| 123 * |
| 124 * \param jammerType Type name of jammer class. |
| 125 * \returns False if no error occurs. |
| 126 */ |
| 127 bool InstallJammer (std::string jammerType); |
| 128 |
| 129 private: |
| 130 double m_simTimeS; |
| 131 double m_jammerStartTimeS; |
| 132 }; |
| 133 |
| 134 JammerTypeTest::JammerTypeTest () |
| 135 : TestCase ("Test of installing & running jammers.") |
| 136 { |
| 137 m_simTimeS = 10.0; // simulate for 10.0 seconds |
| 138 m_jammerStartTimeS = 5.0; // jammer starts at 5.0 seconds |
| 139 } |
| 140 |
| 141 JammerTypeTest::~JammerTypeTest () |
| 142 { |
| 143 } |
| 144 |
| 145 void |
| 146 JammerTypeTest::DoRun (void) |
| 147 { |
| 148 NS_TEST_ASSERT_MSG_EQ (InstallJammer ("ns3::ConstantJammer"), false, |
| 149 "Failed to install ConstantJammer!"); |
| 150 NS_TEST_ASSERT_MSG_EQ (InstallJammer ("ns3::RandomJammer"), false, |
| 151 "Failed to install RandomJammer!"); |
| 152 NS_TEST_ASSERT_MSG_EQ (InstallJammer ("ns3::EavesdropperJammer"), false, |
| 153 "Failed to install EavesdropperJammer!"); |
| 154 NS_TEST_ASSERT_MSG_EQ (InstallJammer ("ns3::ReactiveJammer"), false, |
| 155 "Failed to install ReactiveJammer!"); |
| 156 } |
| 157 |
| 158 bool |
| 159 JammerTypeTest::InstallJammer (std::string jammerType) |
| 160 { |
| 161 // create node |
| 162 Ptr<Node> node = CreateObject<Node> (); |
| 163 |
| 164 /* |
| 165 * Create and install wifi. |
| 166 */ |
| 167 std::string phyMode ("DsssRate1Mbps"); |
| 168 Config::SetDefault ("ns3::WifiRemoteStationManager::FragmentationThreshold", |
| 169 StringValue ("2200")); |
| 170 Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", |
| 171 StringValue ("2200")); |
| 172 Config::SetDefault ("ns3::WifiRemoteStationManager::NonUnicastMode", |
| 173 StringValue (phyMode)); |
| 174 WifiHelper wifi; |
| 175 wifi.SetStandard (WIFI_PHY_STANDARD_80211b); |
| 176 NslWifiPhyHelper wifiPhy = NslWifiPhyHelper::Default (); |
| 177 wifiPhy.SetPcapDataLinkType (NslWifiPhyHelper::DLT_IEEE802_11_RADIO); |
| 178 |
| 179 NslWifiChannelHelper wifiChannel ; |
| 180 wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel"); |
| 181 wifiPhy.SetChannel (wifiChannel.Create ()); |
| 182 NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default (); |
| 183 wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", |
| 184 "DataMode", StringValue(phyMode), |
| 185 "ControlMode", StringValue(phyMode)); |
| 186 wifiMac.SetType ("ns3::AdhocWifiMac"); |
| 187 NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, node); |
| 188 |
| 189 /* |
| 190 * Create and install mobility. |
| 191 */ |
| 192 MobilityHelper mobility; |
| 193 Ptr<ListPositionAllocator> posAlloc = CreateObject<ListPositionAllocator> (); |
| 194 posAlloc->Add (Vector (0.0, 0.0, 0.0)); |
| 195 mobility.SetPositionAllocator (posAlloc); |
| 196 mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); |
| 197 mobility.Install (node); |
| 198 |
| 199 /* |
| 200 * Create and install energy source & device energy model |
| 201 */ |
| 202 BasicEnergySourceHelper basicSourceHelper; |
| 203 EnergySourceContainer energySources = basicSourceHelper.Install (node); |
| 204 WifiRadioEnergyModelHelper radioEnergyHelper; |
| 205 DeviceEnergyModelContainer deviceModels = |
| 206 radioEnergyHelper.Install (devices, energySources); |
| 207 |
| 208 /* |
| 209 * Create and install jammer. |
| 210 */ |
| 211 JammerHelper jammerHelper; |
| 212 jammerHelper.SetJammerType (jammerType); |
| 213 JammerContainer jammers = jammerHelper.Install (node); |
| 214 //jammerHelper.EnableLogComponents (); |
| 215 |
| 216 // start jammers at 7.0 seconds |
| 217 JammerContainer::Iterator i; |
| 218 for (i = jammers.Begin (); i != jammers.End (); i++) |
| 219 { |
| 220 Simulator::Schedule (Seconds (m_jammerStartTimeS), |
| 221 &ns3::Jammer::StartJammer, |
| 222 *i); |
| 223 } |
| 224 |
| 225 /* |
| 226 * Run simulation. |
| 227 */ |
| 228 Simulator::Stop (Seconds (m_simTimeS)); |
| 229 Simulator::Run (); |
| 230 Simulator::Destroy (); |
| 231 |
| 232 return false; // all is good |
| 233 } |
| 234 |
| 235 // -------------------------------------------------------------------------- // |
| 236 |
| 237 /** |
| 238 * Test case of installing different types of jamming mitigation classes onto a |
| 239 * node. Also tests hidden installation of WirelessModuleUtility object. |
| 240 */ |
| 241 class JammingMitigationTypeTest : public TestCase |
| 242 { |
| 243 public: |
| 244 JammingMitigationTypeTest (); |
| 245 virtual ~JammingMitigationTypeTest (); |
| 246 |
| 247 private: |
| 248 void DoRun (void); |
| 249 |
| 250 /** |
| 251 * \brief Install and run different types of jamming mitigation class. |
| 252 * |
| 253 * \param mitigationType Type name of jamming mitigation class. |
| 254 * \returns False if no error occurs. |
| 255 */ |
| 256 bool InstallMitigation (std::string mitigationType); |
| 257 |
| 258 private: |
| 259 double m_simTimeS; |
| 260 double m_mitigationStartTimeS; |
| 261 }; |
| 262 |
| 263 JammingMitigationTypeTest::JammingMitigationTypeTest () |
| 264 : TestCase ("Test of installing & running of jamming mitigation.") |
| 265 { |
| 266 m_simTimeS = 10.0; // simulate for 10.0 seconds |
| 267 m_mitigationStartTimeS = 5.0; // jammer starts at 5.0 seconds |
| 268 } |
| 269 |
| 270 JammingMitigationTypeTest::~JammingMitigationTypeTest () |
| 271 { |
| 272 } |
| 273 |
| 274 void |
| 275 JammingMitigationTypeTest::DoRun (void) |
| 276 { |
| 277 NS_TEST_ASSERT_MSG_EQ (InstallMitigation ("ns3::MitigateByChannelHop"), false, |
| 278 "Failed to install MitigateByChannelHop!"); |
| 279 } |
| 280 |
| 281 bool |
| 282 JammingMitigationTypeTest::InstallMitigation (std::string mitigationType) |
| 283 { |
| 284 // create node |
| 285 Ptr<Node> node = CreateObject<Node> (); |
| 286 |
| 287 /* |
| 288 * Create and install wifi. |
| 289 */ |
| 290 std::string phyMode ("DsssRate1Mbps"); |
| 291 Config::SetDefault ("ns3::WifiRemoteStationManager::FragmentationThreshold", |
| 292 StringValue ("2200")); |
| 293 Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", |
| 294 StringValue ("2200")); |
| 295 Config::SetDefault ("ns3::WifiRemoteStationManager::NonUnicastMode", |
| 296 StringValue (phyMode)); |
| 297 WifiHelper wifi; |
| 298 wifi.SetStandard (WIFI_PHY_STANDARD_80211b); |
| 299 NslWifiPhyHelper wifiPhy = NslWifiPhyHelper::Default (); |
| 300 wifiPhy.SetPcapDataLinkType (NslWifiPhyHelper::DLT_IEEE802_11_RADIO); |
| 301 |
| 302 NslWifiChannelHelper wifiChannel ; |
| 303 wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel"); |
| 304 wifiPhy.SetChannel (wifiChannel.Create ()); |
| 305 NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default (); |
| 306 wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", |
| 307 "DataMode", StringValue(phyMode), |
| 308 "ControlMode", StringValue(phyMode)); |
| 309 wifiMac.SetType ("ns3::AdhocWifiMac"); |
| 310 NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, node); |
| 311 |
| 312 /* |
| 313 * Create and install mobility. |
| 314 */ |
| 315 MobilityHelper mobility; |
| 316 Ptr<ListPositionAllocator> posAlloc = CreateObject<ListPositionAllocator> (); |
| 317 posAlloc->Add (Vector (0.0, 0.0, 0.0)); |
| 318 mobility.SetPositionAllocator (posAlloc); |
| 319 mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); |
| 320 mobility.Install (node); |
| 321 |
| 322 /* |
| 323 * Create and install energy source & device energy model |
| 324 */ |
| 325 BasicEnergySourceHelper basicSourceHelper; |
| 326 EnergySourceContainer energySources = basicSourceHelper.Install (node); |
| 327 WifiRadioEnergyModelHelper radioEnergyHelper; |
| 328 DeviceEnergyModelContainer deviceModels = |
| 329 radioEnergyHelper.Install (devices, energySources); |
| 330 |
| 331 /* |
| 332 * Create and install jamming mitigation. |
| 333 */ |
| 334 JammingMitigationHelper mitigationHelper; |
| 335 mitigationHelper.SetJammingMitigationType (mitigationType); |
| 336 JammingMitigationContainer mitigators = mitigationHelper.Install (node); |
| 337 //mitigationHelper.EnableLogComponents (); |
| 338 |
| 339 // start jamming mitigation on all nodes |
| 340 JammingMitigationContainer::Iterator i; |
| 341 for (i = mitigators.Begin (); i != mitigators.End (); i++) |
| 342 { |
| 343 Simulator::Schedule (Seconds (m_mitigationStartTimeS), |
| 344 &ns3::JammingMitigation::StartMitigation, |
| 345 *i); |
| 346 } |
| 347 |
| 348 /* |
| 349 * Run simulation. |
| 350 */ |
| 351 Simulator::Stop (Seconds (m_simTimeS)); |
| 352 Simulator::Run (); |
| 353 Simulator::Destroy (); |
| 354 |
| 355 return false; // all is good |
| 356 } |
| 357 |
| 358 // -------------------------------------------------------------------------- // |
| 359 |
| 360 /** |
| 361 * Test case of installing different types of WirelessModuleUtility class onto |
| 362 * nodes. Values recorded by WirelessModuleUtility are checked in this test: |
| 363 * 1. RSS. Using Friis propagation loss mode. |
| 364 * 2. Throughput (TX & RX) |
| 365 * 3. Total bytes (TX & RX) |
| 366 * 4. PDR |
| 367 * |
| 368 * Note that we never move the node out of range. Therefore the receiving node |
| 369 * should always be able to receive the packet. |
| 370 */ |
| 371 class WirelessModuleUtilityTest : public TestCase |
| 372 { |
| 373 public: |
| 374 WirelessModuleUtilityTest (); |
| 375 virtual ~WirelessModuleUtilityTest (); |
| 376 |
| 377 /** |
| 378 * \brief RSS trace function for receiver node. |
| 379 * \param oldValue Old RSS value. |
| 380 * \param rss New RSS value. |
| 381 */ |
| 382 void NodeRss (double oldValue, double rss); |
| 383 |
| 384 void PacketRss (double oldValue, double newValue); |
| 385 |
| 386 private: |
| 387 void DoRun (void); |
| 388 |
| 389 /** |
| 390 * \brief Run simulation of 2 nodes with specified distance. |
| 391 * |
| 392 * \param distance Distance between 2 nodes. |
| 393 * \returns False if no error occurs. |
| 394 */ |
| 395 bool SimulateTwoNodes (double distance); |
| 396 |
| 397 /** |
| 398 * \brief Calculates RSS using Friis propagation loss model. |
| 399 * |
| 400 * \param distance Distance between sender and receiver. |
| 401 * \returns RSS, in watts. Both Tx & Rx gain are included in the result. |
| 402 * |
| 403 * Taken from FriisPropagationLossModel::DoCalcRxPower. Note that RSS is no |
| 404 * lower then the noise floor. |
| 405 */ |
| 406 double CalculateRssFriis (double distance); |
| 407 |
| 408 /** |
| 409 * \brief Converts dBm to Watts. |
| 410 * |
| 411 * \param dBm dBm. |
| 412 * \return Watts. |
| 413 */ |
| 414 double DbmToW (double dBm) const; |
| 415 |
| 416 private: |
| 417 double m_simTimeS; |
| 418 double m_maxDistance; // maximum distance |
| 419 double m_distanceStep; // step size for distance increase |
| 420 /** |
| 421 * Cutoff distance, no packet can be received beyond or at such distance. |
| 422 */ |
| 423 double m_cutoffDistance; |
| 424 uint64_t m_packetSize; // size of each packet, in bytes |
| 425 uint64_t m_numPackets; // number of packets to send |
| 426 double m_interPacketIntervalS; // interval between each packet send |
| 427 double m_tolerance; // tolerance for double values |
| 428 |
| 429 double m_txPowerDbm; // TX power, in dBm |
| 430 double m_txGainDbm; // TX gain, in dBm |
| 431 double m_rxGainDbm; // RX gain, in dBm |
| 432 /** |
| 433 * The utility class records total size of a packet, instead of only payload |
| 434 * sizes. When calculating throughput in this test case, we will have to add |
| 435 * the packet overhead manually. |
| 436 */ |
| 437 double m_udpPacketOverhead; |
| 438 |
| 439 double m_lambda; // wavelength for Friis propagation loss model |
| 440 double PI; // PI |
| 441 double m_minDistance; // minimum distance for Friis propagation loss model |
| 442 double m_systemLoss; // system loss for Friis propagation loss model |
| 443 |
| 444 /** |
| 445 * Noise floor for this test, in Watts. |
| 446 */ |
| 447 double m_noiseFloor; |
| 448 double m_maxRssW; // maximum RSS during simulation, in Watts= |
| 449 double m_avgPktRssW; // average packet RSS |
| 450 }; |
| 451 |
| 452 WirelessModuleUtilityTest::WirelessModuleUtilityTest () |
| 453 : TestCase ("Test of installing & running of WirelessModuleUtility.") |
| 454 { |
| 455 m_simTimeS = 20.0; // simulate for 20.0 seconds |
| 456 m_maxDistance = 10.0; // go up to 10 meters |
| 457 m_distanceStep = 1.0; // go up 1 meter per step |
| 458 m_cutoffDistance = 5000.0; // |
| 459 m_numPackets = 1000; |
| 460 m_packetSize = 200; |
| 461 m_interPacketIntervalS = 1.0; |
| 462 |
| 463 m_txPowerDbm = 16.0206; |
| 464 m_txGainDbm = 1.0; // magic number |
| 465 m_rxGainDbm = 0.0; |
| 466 |
| 467 /* |
| 468 * epsilon = 2.22045e-16 |
| 469 */ |
| 470 m_tolerance = std::numeric_limits<double>::epsilon (); |
| 471 |
| 472 m_udpPacketOverhead = 64; // UDP header/trailer overhead |
| 473 |
| 474 m_lambda = 300000000.0 / 5.150e9; |
| 475 PI = 3.14159265358979323846; |
| 476 m_minDistance = 0.5; |
| 477 m_systemLoss = 1.0; |
| 478 |
| 479 /* |
| 480 * Noise floor = Boltzmann Constant * 290.0 * Bandwidth * Noise Figure |
| 481 * Where in this test case: |
| 482 * Boltzmann Constant = 1.3803e-23 |
| 483 * Bandwidth of test setup = 22000000 |
| 484 * Noise Figure = 5.01187 |
| 485 * value = 4.41361e-13 |
| 486 */ |
| 487 m_noiseFloor = 1.3803e-23 * 290.0 * 22000000 * 5.01187; |
| 488 |
| 489 m_maxRssW = 0; |
| 490 m_avgPktRssW = 0; |
| 491 } |
| 492 |
| 493 WirelessModuleUtilityTest::~WirelessModuleUtilityTest () |
| 494 { |
| 495 } |
| 496 |
| 497 void |
| 498 WirelessModuleUtilityTest::DoRun (void) |
| 499 { |
| 500 // normal test |
| 501 for (double distance = m_distanceStep; distance <= m_maxDistance; |
| 502 distance += m_distanceStep) |
| 503 { |
| 504 NS_TEST_ASSERT_MSG_EQ (SimulateTwoNodes (distance), false, |
| 505 "Failed WirelessModuleUtility normal test"); |
| 506 } |
| 507 |
| 508 // 0 PDR test |
| 509 NS_TEST_ASSERT_MSG_EQ (SimulateTwoNodes (m_cutoffDistance + 1.0), false, |
| 510 "Failed WirelessModuleUtility 0 PDR test"); |
| 511 } |
| 512 |
| 513 bool |
| 514 WirelessModuleUtilityTest::SimulateTwoNodes (double distance) |
| 515 { |
| 516 // reset RSS value |
| 517 m_maxRssW = 0; |
| 518 m_avgPktRssW = 0; |
| 519 |
| 520 // create 2 nodes |
| 521 NodeContainer c; |
| 522 c.Create (2); |
| 523 |
| 524 /* |
| 525 * Create and install wifi. |
| 526 */ |
| 527 std::string phyMode ("DsssRate1Mbps"); |
| 528 Config::SetDefault ("ns3::WifiRemoteStationManager::FragmentationThreshold", |
| 529 StringValue ("2200")); |
| 530 Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", |
| 531 StringValue ("2200")); |
| 532 Config::SetDefault ("ns3::WifiRemoteStationManager::NonUnicastMode", |
| 533 StringValue (phyMode)); |
| 534 WifiHelper wifi; |
| 535 wifi.SetStandard (WIFI_PHY_STANDARD_80211b); |
| 536 NslWifiPhyHelper wifiPhy = NslWifiPhyHelper::Default (); |
| 537 wifiPhy.Set ("NslRxGain", DoubleValue (m_rxGainDbm)); |
| 538 wifiPhy.Set ("NslTxGain", DoubleValue (m_txGainDbm)); |
| 539 wifiPhy.Set ("NslTxPowerLevels", UintegerValue (1)); |
| 540 wifiPhy.Set ("NslTxPowerEnd", DoubleValue (m_txPowerDbm)); |
| 541 wifiPhy.Set ("NslTxPowerStart", DoubleValue (m_txPowerDbm)); |
| 542 wifiPhy.Set ("NslCcaMode1Threshold", DoubleValue (0.0)); |
| 543 wifiPhy.SetPcapDataLinkType (NslWifiPhyHelper::DLT_IEEE802_11_RADIO); |
| 544 |
| 545 NslWifiChannelHelper wifiChannel ; |
| 546 wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel"); |
| 547 wifiChannel.AddPropagationLoss ("ns3::FriisPropagationLossModel"); |
| 548 wifiPhy.SetChannel (wifiChannel.Create ()); |
| 549 NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default (); |
| 550 wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", |
| 551 "DataMode", StringValue(phyMode), |
| 552 "ControlMode", StringValue(phyMode)); |
| 553 wifiMac.SetType ("ns3::AdhocWifiMac"); |
| 554 NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, c); |
| 555 |
| 556 /* |
| 557 * Create and install mobility. |
| 558 */ |
| 559 MobilityHelper mobility; |
| 560 Ptr<ListPositionAllocator> posAlloc = CreateObject<ListPositionAllocator> (); |
| 561 posAlloc->Add (Vector (0.0, 0.0, 0.0)); |
| 562 posAlloc->Add (Vector (distance, 0.0, 0.0)); // second node is on x-axis |
| 563 mobility.SetPositionAllocator (posAlloc); |
| 564 mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); |
| 565 mobility.Install (c); |
| 566 |
| 567 /* |
| 568 * Create and install energy source & device energy model |
| 569 */ |
| 570 BasicEnergySourceHelper basicSourceHelper; |
| 571 EnergySourceContainer energySources = basicSourceHelper.Install (c); |
| 572 WifiRadioEnergyModelHelper radioEnergyHelper; |
| 573 DeviceEnergyModelContainer deviceModels = |
| 574 radioEnergyHelper.Install (devices, energySources); |
| 575 |
| 576 /* |
| 577 * Create and install utility. |
| 578 */ |
| 579 WirelessModuleUtilityHelper utilityHelper; |
| 580 // set inclusion list to record only UDP packets |
| 581 std::vector<std::string> InclusionList; |
| 582 InclusionList.push_back ("ns3::UdpHeader"); |
| 583 // set exclusion list to exclude OLSR packets |
| 584 std::vector<std::string> ExclusionList; |
| 585 ExclusionList.push_back ("ns3::olsr::PacketHeader"); |
| 586 utilityHelper.SetInclusionList (InclusionList); |
| 587 utilityHelper.SetExclusionList (ExclusionList); |
| 588 // set update interval |
| 589 utilityHelper.Set ("RssUpdateInterval", TimeValue (MilliSeconds (100))); |
| 590 WirelessModuleUtilityContainer utilities = utilityHelper.InstallAll (); |
| 591 Ptr<WirelessModuleUtility> utilSend = utilities.Get (0); |
| 592 Ptr<WirelessModuleUtility> utilRecv = utilities.Get (1); |
| 593 if ((utilSend == NULL) || (utilRecv == NULL)) |
| 594 { |
| 595 NS_LOG_UNCOND ("Failed to aggregate utility onto nodes!"); |
| 596 return true; |
| 597 } |
| 598 // connect trace source |
| 599 Callback<void, double, double> rssTraceCallback; |
| 600 rssTraceCallback = MakeCallback (&WirelessModuleUtilityTest::NodeRss, this); |
| 601 utilRecv->TraceConnectWithoutContext ("Rss", rssTraceCallback); |
| 602 Callback<void, double, double> packetRssTraceCallback; |
| 603 packetRssTraceCallback = MakeCallback (&WirelessModuleUtilityTest::PacketRss,
this); |
| 604 utilRecv->TraceConnectWithoutContext ("PacketRss", packetRssTraceCallback); |
| 605 |
| 606 /* |
| 607 * Create and install Internet stack (with OLSR enabled to generate some OLSR |
| 608 * traffic), UDP socket. |
| 609 */ |
| 610 // enable OLSR |
| 611 OlsrHelper olsr; |
| 612 Ipv4StaticRoutingHelper staticRouting; |
| 613 Ipv4ListRoutingHelper list; |
| 614 list.Add (staticRouting, 0); |
| 615 list.Add (olsr, 10); |
| 616 // install Internet stack |
| 617 InternetStackHelper internet; |
| 618 internet.SetRoutingHelper (list); |
| 619 internet.Install (c); |
| 620 // assign IP addresses |
| 621 Ipv4AddressHelper ipv4; |
| 622 ipv4.SetBase ("10.1.1.0", "255.255.255.0"); |
| 623 Ipv4InterfaceContainer i = ipv4.Assign (devices); |
| 624 TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory"); |
| 625 // node 1, receiver |
| 626 Ptr<Socket> recvSink = Socket::CreateSocket (c.Get (1), tid); |
| 627 InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), 80); |
| 628 recvSink->Bind (local); |
| 629 recvSink->SetRecvCallback (MakeCallback (&ReceivePacket)); |
| 630 // node 0, sender |
| 631 Ptr<Socket> source = Socket::CreateSocket (c.Get (0), tid); |
| 632 InetSocketAddress remote = InetSocketAddress (Ipv4Address::GetBroadcast (), 80
); |
| 633 source->SetAllowBroadcast (true); |
| 634 source->Connect (remote); |
| 635 |
| 636 /* |
| 637 * Generate some traffic during simulation. Start traffic at the beginning of |
| 638 * simulation. We do not have to wait for OLSR to converge since there are 2 |
| 639 * nodes only in this simulation. We needed only OLSR traffic to test ability |
| 640 * of filtering packets in WirelessModuleUtility. |
| 641 */ |
| 642 Simulator::Schedule (Seconds (0.0), &GenerateTraffic, source, m_packetSize, |
| 643 c.Get (0), m_numPackets, Seconds (m_interPacketIntervalS)
); |
| 644 |
| 645 /* |
| 646 * Run simulation. |
| 647 */ |
| 648 Simulator::Stop (Seconds (m_simTimeS)); |
| 649 Simulator::Run (); |
| 650 Simulator::Destroy (); |
| 651 |
| 652 /* |
| 653 * Simulation results. |
| 654 */ |
| 655 uint64_t actualTxTotalBytes = utilSend->GetTotalBytesTx (); // sender |
| 656 uint64_t actualRxTotalBytes = utilRecv->GetTotalBytesRx (); // receiver |
| 657 double actualTxThroughput = utilSend->GetTxThroughput (); // sender |
| 658 double actualRxThroughput = utilRecv->GetRxThroughput (); // receiver |
| 659 double actualPdr = utilRecv->GetPdr (); // receiver |
| 660 |
| 661 NS_LOG_UNCOND ("\nDistance = " << distance); |
| 662 NS_LOG_UNCOND ("Actual TX total bytes = " << actualTxTotalBytes); |
| 663 NS_LOG_UNCOND ("Actual RX total bytes = " << actualRxTotalBytes); |
| 664 NS_LOG_UNCOND ("Actual TX throughput = " << actualTxThroughput); |
| 665 NS_LOG_UNCOND ("Actual RX throughput = " << actualRxThroughput); |
| 666 NS_LOG_UNCOND ("Actual PDR = " << actualPdr); |
| 667 NS_LOG_UNCOND ("Actual RSS = " << m_maxRssW); |
| 668 NS_LOG_UNCOND ("Actual Average Packet RSS = " << m_avgPktRssW << "\n"); |
| 669 |
| 670 /* |
| 671 * Calculated results. |
| 672 */ |
| 673 uint64_t totalNumPackets = (uint64_t) floor (m_simTimeS / m_interPacketInterva
lS); |
| 674 totalNumPackets = (totalNumPackets > m_numPackets) ? m_numPackets : totalNumPa
ckets; |
| 675 |
| 676 uint64_t actualPacketSize = m_packetSize + m_udpPacketOverhead; |
| 677 uint64_t calculatedTxTotalBytes = actualPacketSize * totalNumPackets; |
| 678 uint64_t calculatedRxTotalBytes = actualPacketSize * totalNumPackets; |
| 679 // when distance > cutoff distance, RX total bytes will drop to 0 |
| 680 calculatedRxTotalBytes = (distance >= m_cutoffDistance) ? 0 : calculatedRxTota
lBytes; |
| 681 double calculatedTxThroughput = calculatedTxTotalBytes * 8 / m_simTimeS; |
| 682 double calculatedRxThroughput = calculatedRxTotalBytes * 8 / m_simTimeS; |
| 683 // when distance > cutoff distance, PDR will drop to 0 |
| 684 double calculatedPdr = (distance >= m_cutoffDistance) ? 0 : 1; |
| 685 double calculatedRss = CalculateRssFriis (distance); |
| 686 |
| 687 NS_LOG_UNCOND ("Calculated TX total bytes = " << calculatedTxTotalBytes); |
| 688 NS_LOG_UNCOND ("Calculated RX total bytes = " << calculatedRxTotalBytes); |
| 689 NS_LOG_UNCOND ("Calculated TX throughput = " << calculatedTxThroughput); |
| 690 NS_LOG_UNCOND ("Calculated RX throughput = " << calculatedRxThroughput); |
| 691 NS_LOG_UNCOND ("Calculated PDR = " << calculatedPdr); |
| 692 NS_LOG_UNCOND ("Calculated RSS = " << calculatedRss << "\n"); |
| 693 |
| 694 NS_LOG_UNCOND ("RSS difference = " << calculatedRss - m_maxRssW); |
| 695 NS_LOG_UNCOND ("Packet RSS difference = " << calculatedRss - m_avgPktRssW); |
| 696 |
| 697 /* |
| 698 * Check results. |
| 699 */ |
| 700 NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL (calculatedTxTotalBytes, actualTxTotalBytes
, |
| 701 "Incorrect Tx Total Bytes!"); |
| 702 NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL (calculatedRxTotalBytes, actualRxTotalBytes
, |
| 703 "Incorrect Rx Total Bytes!"); |
| 704 NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL (calculatedTxThroughput, actualTxThroughput
, |
| 705 "Incorrect Tx Throughput!"); |
| 706 NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL (calculatedRxThroughput, actualRxThroughput
, |
| 707 "Incorrect Rx Throughput!"); |
| 708 NS_TEST_ASSERT_MSG_EQ_TOL_RETURNS_BOOL (calculatedPdr, actualPdr, m_tolerance, |
| 709 "Incorrect PDR!"); |
| 710 NS_TEST_ASSERT_MSG_EQ_TOL_RETURNS_BOOL (calculatedRss, m_maxRssW, m_tolerance, |
| 711 "Incorrect RSS!"); |
| 712 NS_TEST_ASSERT_MSG_EQ_TOL_RETURNS_BOOL (calculatedRss, m_avgPktRssW, m_toleran
ce, |
| 713 "Incorrect Packet RSS!"); |
| 714 |
| 715 return false; // all is good |
| 716 } |
| 717 |
| 718 double |
| 719 WirelessModuleUtilityTest::CalculateRssFriis (double distance) |
| 720 { |
| 721 double txPowerDbm = m_txGainDbm + m_txPowerDbm; |
| 722 if (distance <= m_minDistance) |
| 723 { |
| 724 return m_noiseFloor + DbmToW (txPowerDbm + m_rxGainDbm); |
| 725 } |
| 726 double numerator = m_lambda * m_lambda; |
| 727 double denominator = 16 * PI * PI * distance * distance * m_systemLoss; |
| 728 double pr = 10 * log10 (numerator / denominator); |
| 729 double rssDbm = txPowerDbm + pr + m_rxGainDbm; |
| 730 return m_noiseFloor + DbmToW (rssDbm); |
| 731 } |
| 732 |
| 733 double |
| 734 WirelessModuleUtilityTest::DbmToW (double dBm) const |
| 735 { |
| 736 double mW = pow (10.0, dBm / 10.0); |
| 737 return mW / 1000.0; |
| 738 } |
| 739 |
| 740 void |
| 741 WirelessModuleUtilityTest::NodeRss (double oldValue, double rss) |
| 742 { |
| 743 if (rss > m_maxRssW) |
| 744 { |
| 745 m_maxRssW = rss; |
| 746 } |
| 747 } |
| 748 |
| 749 void |
| 750 WirelessModuleUtilityTest::PacketRss (double oldValue, double newValue) |
| 751 { |
| 752 if (newValue > m_avgPktRssW) |
| 753 { |
| 754 m_avgPktRssW = newValue; |
| 755 } |
| 756 } |
| 757 |
| 758 // -------------------------------------------------------------------------- // |
| 759 |
| 760 /** |
| 761 * Test suite for Jammer, JammingMitigation & WirelessModuleUtility components. |
| 762 */ |
| 763 class JammingComponentTestSuite : public TestSuite |
| 764 { |
| 765 public: |
| 766 JammingComponentTestSuite (); |
| 767 }; |
| 768 |
| 769 JammingComponentTestSuite::JammingComponentTestSuite () |
| 770 : TestSuite ("jamming-components-test", UNIT) |
| 771 { |
| 772 AddTestCase (new JammerTypeTest); |
| 773 AddTestCase (new JammingMitigationTypeTest); |
| 774 AddTestCase (new WirelessModuleUtilityTest); |
| 775 } |
| 776 |
| 777 // create an instance of the test suite |
| 778 JammingComponentTestSuite g_jammingComponentsTestSuite; |
| 779 |
| 780 } // namespace ns3 |
LEFT | RIGHT |