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 * Author: Sidharth Nabar <snabar@uw.edu>, He Wu <mdzz@u.washington.edu> |
| 19 */ |
| 20 |
| 21 /* |
| 22 * This example demonstrates how EnergyModel, Jammer, JammingMitigation, |
| 23 * WirelessModuleUtility and NslWifiPhy can be used together to simulate a |
| 24 * jammed wifi ad-hoc network. |
| 25 * |
| 26 * Network topology: 4 honest nodes + 1 jammer. One honest node will broadcast |
| 27 * UDP packets too all the other nodes. The jammer will try to jam node 3. |
| 28 */ |
| 29 |
| 30 #include "ns3/core-module.h" |
| 31 #include "ns3/network-module.h" |
| 32 #include "ns3/helper-module.h" |
| 33 #include "ns3/mobility-module.h" |
| 34 #include "ns3/contrib-module.h" |
| 35 #include "ns3/wifi-module.h" |
| 36 #include "ns3/internet-module.h" |
| 37 #include "ns3/energy-module.h" |
| 38 #include "ns3/jamming-module.h" |
| 39 |
| 40 #include <iostream> |
| 41 #include <fstream> |
| 42 #include <vector> |
| 43 #include <string> |
| 44 |
| 45 NS_LOG_COMPONENT_DEFINE ("JammingMitigationExample"); |
| 46 |
| 47 using namespace ns3; |
| 48 |
| 49 /** |
| 50 * \brief Packet receiving sink. |
| 51 * |
| 52 * \param socket Pointer to socket. |
| 53 */ |
| 54 void |
| 55 ReceivePacket (Ptr<Socket> socket) |
| 56 { |
| 57 Ptr<Packet> packet; |
| 58 Address from; |
| 59 while (packet = socket->RecvFrom (from)) |
| 60 { |
| 61 if (packet->GetSize () > 0) |
| 62 { |
| 63 InetSocketAddress iaddr = InetSocketAddress::ConvertFrom (from); |
| 64 NS_LOG_UNCOND ("--\nReceived one packet! Socket: "<< iaddr.GetIpv4 () |
| 65 << " port: " << iaddr.GetPort () << " at time = " << |
| 66 Simulator::Now ().GetSeconds () << "\n--"); |
| 67 } |
| 68 } |
| 69 } |
| 70 |
| 71 /** |
| 72 * \brief Traffic generator. |
| 73 * |
| 74 * \param socket Pointer to socket. |
| 75 * \param pktSize Packet size. |
| 76 * \param n Pointer to node. |
| 77 * \param pktCount Number of packets to generate. |
| 78 * \param pktInterval Packet sending interval. |
| 79 */ |
| 80 static void |
| 81 GenerateTraffic (Ptr<Socket> socket, uint32_t pktSize, Ptr<Node> n, |
| 82 uint32_t pktCount, Time pktInterval) |
| 83 { |
| 84 if (pktCount > 0) |
| 85 { |
| 86 socket->Send (Create<Packet> (pktSize)); |
| 87 Simulator::Schedule (pktInterval, &GenerateTraffic, socket, pktSize, n, |
| 88 pktCount - 1, pktInterval); |
| 89 } |
| 90 else |
| 91 { |
| 92 socket->Close (); |
| 93 } |
| 94 } |
| 95 |
| 96 /** |
| 97 * \brief Trace function for remaining energy at node. |
| 98 * |
| 99 * \param oldValue Old remaining energy value. |
| 100 * \param remainingEnergy New remaining energy value. |
| 101 */ |
| 102 void |
| 103 RemainingEnergy (double oldValue, double remainingEnergy) |
| 104 { |
| 105 NS_LOG_UNCOND (Simulator::Now ().GetSeconds () << |
| 106 "s Current remaining energy = " << remainingEnergy << "J"); |
| 107 } |
| 108 |
| 109 /** |
| 110 * \brief Trace function for total energy consumption at node. |
| 111 * |
| 112 * \param oldValue Old total energy consumption value. |
| 113 * \param totalEnergy New total energy consumption value. |
| 114 */ |
| 115 void |
| 116 TotalEnergy (double oldValue, double totalEnergy) |
| 117 { |
| 118 NS_LOG_UNCOND (Simulator::Now ().GetSeconds () << |
| 119 "s Total energy consumed by radio = " << totalEnergy << "J"); |
| 120 } |
| 121 |
| 122 /** |
| 123 * \brief Trace function for node RSS. |
| 124 * |
| 125 * \param oldValue Old RSS value. |
| 126 * \param rss New RSS value. |
| 127 */ |
| 128 void |
| 129 NodeRss (double oldValue, double rss) |
| 130 { |
| 131 NS_LOG_UNCOND (Simulator::Now ().GetSeconds () << "s Node RSS = " << rss << |
| 132 "W"); |
| 133 } |
| 134 |
| 135 /** |
| 136 * \brief Trace function for node PDR. |
| 137 * |
| 138 * \param oldValue Old PDR value. |
| 139 * \param pdr New PDR value. |
| 140 */ |
| 141 void |
| 142 NodePdr (double oldValue, double pdr) |
| 143 { |
| 144 NS_LOG_UNCOND (Simulator::Now ().GetSeconds () << "s Node PDR = " << pdr); |
| 145 } |
| 146 |
| 147 /** |
| 148 * \brief Trace function for node RX throughput. |
| 149 * |
| 150 * \param oldValue Old RX throughput value. |
| 151 * \param rxThroughput New RX throughput value. |
| 152 */ |
| 153 void |
| 154 NodeThroughputRx (double oldValue, double rxThroughput) |
| 155 { |
| 156 NS_LOG_UNCOND (Simulator::Now ().GetSeconds () << "s Node RX throughput = " |
| 157 << rxThroughput); |
| 158 } |
| 159 |
| 160 int |
| 161 main (int argc, char *argv[]) |
| 162 { |
| 163 /* |
| 164 LogComponentEnable ("NslWifiPhy", LOG_LEVEL_DEBUG); |
| 165 LogComponentEnable ("EnergySource", LOG_LEVEL_DEBUG); |
| 166 LogComponentEnable ("BasicEnergySource", LOG_LEVEL_DEBUG); |
| 167 LogComponentEnable ("DeviceEnergyModel", LOG_LEVEL_DEBUG); |
| 168 LogComponentEnable ("WifiRadioEnergyModel", LOG_LEVEL_DEBUG); |
| 169 LogComponentEnable ("WirelessModuleUtility", LOG_LEVEL_DEBUG); |
| 170 LogComponentEnable ("JammerHelper", LOG_LEVEL_DEBUG); |
| 171 LogComponentEnable ("Jammer", LOG_LEVEL_DEBUG); |
| 172 LogComponentEnable ("ReactiveJammer", LOG_LEVEL_DEBUG); |
| 173 LogComponentEnable ("JammingMitigationHelper", LOG_LEVEL_DEBUG); |
| 174 LogComponentEnable ("JammingMitigation", LOG_LEVEL_DEBUG); |
| 175 LogComponentEnable ("MitigateByChannelHop", LOG_LEVEL_DEBUG); |
| 176 */ |
| 177 |
| 178 LogComponentEnable ("MitigateByChannelHop", LOG_LEVEL_DEBUG); |
| 179 |
| 180 std::string phyMode ("DsssRate1Mbps"); |
| 181 double Prss = -80; // dBm |
| 182 uint32_t PpacketSize = 200; // bytes |
| 183 bool verbose = false; |
| 184 |
| 185 // simulation parameters |
| 186 uint32_t numPackets = 10000; // number of packets to send |
| 187 double interval = 1; // seconds |
| 188 double startTime = 0.0; // seconds |
| 189 double distanceToRx = 10.0; // meters |
| 190 /* |
| 191 * This is a magic number used to set the transmit power, based on other |
| 192 * configuration. |
| 193 */ |
| 194 double offset = 81; |
| 195 |
| 196 CommandLine cmd; |
| 197 cmd.AddValue ("phyMode", "Wifi Phy mode", phyMode); |
| 198 cmd.AddValue ("Prss", "Intended primary RSS (dBm)", Prss); |
| 199 cmd.AddValue ("PpacketSize", "size of application packet sent", PpacketSize); |
| 200 cmd.AddValue ("numPackets", "Total number of packets to send", numPackets); |
| 201 cmd.AddValue ("startTime", "Simulation start time", startTime); |
| 202 cmd.AddValue ("distanceToRx", "X-Axis distance between nodes", distanceToRx); |
| 203 cmd.AddValue ("verbose", "Turn on all device log components", verbose); |
| 204 cmd.Parse (argc, argv); |
| 205 |
| 206 // Convert to time object |
| 207 Time interPacketInterval = Seconds (interval); |
| 208 |
| 209 // disable fragmentation for frames below 2200 bytes |
| 210 Config::SetDefault ("ns3::WifiRemoteStationManager::FragmentationThreshold", |
| 211 StringValue ("2200")); |
| 212 // turn off RTS/CTS for frames below 2200 bytes |
| 213 Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", |
| 214 StringValue ("2200")); |
| 215 // Fix non-unicast data rate to be the same as that of unicast |
| 216 Config::SetDefault ("ns3::WifiRemoteStationManager::NonUnicastMode", |
| 217 StringValue (phyMode)); |
| 218 |
| 219 NodeContainer c; |
| 220 c.Create (5); // create 4 honest nodes + 1 jammer |
| 221 NodeContainer honestNodes; |
| 222 honestNodes.Add (c.Get (0)); |
| 223 honestNodes.Add (c.Get (1)); |
| 224 honestNodes.Add (c.Get (2)); |
| 225 honestNodes.Add (c.Get (3)); |
| 226 NodeContainer jammerNodes (c.Get (4)); |
| 227 |
| 228 // The below set of helpers will help us to put together the wifi NICs we want |
| 229 WifiHelper wifi; |
| 230 if (verbose) |
| 231 { |
| 232 wifi.EnableLogComponents (); |
| 233 } |
| 234 wifi.SetStandard (WIFI_PHY_STANDARD_80211b); |
| 235 |
| 236 /** Wifi PHY **/ |
| 237 /***************************************************************************/ |
| 238 NslWifiPhyHelper wifiPhy = NslWifiPhyHelper::Default (); |
| 239 wifiPhy.Set ("NslRxGain", DoubleValue (-10)); |
| 240 wifiPhy.Set ("NslTxGain", DoubleValue (offset + Prss)); |
| 241 wifiPhy.Set ("NslCcaMode1Threshold", DoubleValue (0.0)); |
| 242 /***************************************************************************/ |
| 243 |
| 244 /** wifi channel **/ |
| 245 NslWifiChannelHelper wifiChannel; |
| 246 wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel"); |
| 247 wifiChannel.AddPropagationLoss ("ns3::FriisPropagationLossModel"); |
| 248 // create wifi channel |
| 249 Ptr<NslWifiChannel> wifiChannelPtr = wifiChannel.Create (); |
| 250 wifiPhy.SetChannel (wifiChannelPtr); |
| 251 |
| 252 /** MAC layer **/ |
| 253 // Add a non-QoS upper MAC, and disable rate control |
| 254 NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default (); |
| 255 wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode", |
| 256 StringValue (phyMode), "ControlMode", StringValue (phyMode)); |
| 257 // Set it to ad-hoc mode |
| 258 wifiMac.SetType ("ns3::AdhocWifiMac"); |
| 259 |
| 260 /** install PHY + MAC **/ |
| 261 NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, honestNodes); |
| 262 // install MAC & PHY onto jammer |
| 263 NetDeviceContainer jammerNetdevice = wifi.Install (wifiPhy, wifiMac, jammerNod
es); |
| 264 |
| 265 /** mobility **/ |
| 266 MobilityHelper mobility; |
| 267 Ptr<ListPositionAllocator> positionAlloc = |
| 268 CreateObject<ListPositionAllocator> (); |
| 269 positionAlloc->Add (Vector (0.0, 0.0, 0.0)); |
| 270 positionAlloc->Add (Vector (distanceToRx, 0.1 * distanceToRx, 0.0)); |
| 271 positionAlloc->Add (Vector (2 * distanceToRx, 0.0, 0.0)); |
| 272 positionAlloc->Add (Vector (3 * distanceToRx, 0.1 * distanceToRx, 0.0)); |
| 273 positionAlloc->Add (Vector (2 * distanceToRx, -0.5 * distanceToRx, 0.0)); // j
ammer location |
| 274 mobility.SetPositionAllocator (positionAlloc); |
| 275 mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); |
| 276 mobility.Install (c); |
| 277 |
| 278 /** Energy Model **/ |
| 279 /***************************************************************************/ |
| 280 /* energy source */ |
| 281 BasicEnergySourceHelper basicSourceHelper; |
| 282 // configure energy source |
| 283 basicSourceHelper.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue (0.1)); |
| 284 // install on node |
| 285 EnergySourceContainer energySources = basicSourceHelper.Install (c); |
| 286 /* device energy model */ |
| 287 WifiRadioEnergyModelHelper radioEnergyHelper; |
| 288 // configure radio energy model |
| 289 radioEnergyHelper.Set ("TxCurrentA", DoubleValue (0.0174)); |
| 290 // install on devices |
| 291 DeviceEnergyModelContainer deviceModels = |
| 292 radioEnergyHelper.Install (devices, energySources); |
| 293 DeviceEnergyModelContainer jammerDeviceModels = |
| 294 radioEnergyHelper.Install (jammerNetdevice.Get (0), energySources.Get (4))
; |
| 295 /***************************************************************************/ |
| 296 |
| 297 /** WirelessModuleUtility **/ |
| 298 /***************************************************************************/ |
| 299 WirelessModuleUtilityHelper utilityHelper; |
| 300 // set inclusion/exclusion list for all nodes |
| 301 std::vector<std::string> AllInclusionList; |
| 302 AllInclusionList.push_back ("ns3::UdpHeader"); // record only UdpHead
er |
| 303 std::vector<std::string> AllExclusionList; |
| 304 AllExclusionList.push_back ("ns3::olsr::PacketHeader"); // ignore all olsr hea
ders/trailers |
| 305 // assign lists to helper |
| 306 utilityHelper.SetInclusionList (AllInclusionList); |
| 307 utilityHelper.SetExclusionList (AllExclusionList); |
| 308 // install on all nodes |
| 309 WirelessModuleUtilityContainer utilities = utilityHelper.InstallAll (); |
| 310 /***************************************************************************/ |
| 311 |
| 312 /** Jammer **/ |
| 313 /***************************************************************************/ |
| 314 JammerHelper jammerHelper; |
| 315 // configure jammer type |
| 316 jammerHelper.SetJammerType ("ns3::ReactiveJammer"); |
| 317 // set jammer parameters |
| 318 jammerHelper.Set ("ReactiveJammerRxTimeout", TimeValue (Seconds (2.0))); |
| 319 jammerHelper.Set ("ReactiveJammerReactionStrategy", |
| 320 UintegerValue(ReactiveJammer::FIXED_PROBABILITY)); |
| 321 // enable jammer reaction to jamming mitigation |
| 322 jammerHelper.Set ("ReactiveJammerReactToMitigation", UintegerValue(true)); |
| 323 // install jammer |
| 324 JammerContainer jammers = jammerHelper.Install (jammerNodes); |
| 325 // Get pointer to Jammer |
| 326 Ptr<Jammer> jammerPtr = jammers.Get (0); |
| 327 // enable all jammer debug statements |
| 328 if (verbose) |
| 329 { |
| 330 jammerHelper.EnableLogComponents (); |
| 331 } |
| 332 /***************************************************************************/ |
| 333 |
| 334 /** JammingMiigation **/ |
| 335 /***************************************************************************/ |
| 336 JammingMitigationHelper mitigationHelper; |
| 337 // configure mitigation type |
| 338 mitigationHelper.SetJammingMitigationType ("ns3::MitigateByChannelHop"); |
| 339 // configure mitigation parameters |
| 340 mitigationHelper.Set ("MitigateByChannelHopChannelHopDelay", |
| 341 TimeValue (Seconds (0.0))); |
| 342 mitigationHelper.Set ("MitigateByChannelHopDetectionMethod", |
| 343 UintegerValue (MitigateByChannelHop::PDR_AND_RSS)); |
| 344 // install mitigation on honest nodes |
| 345 JammingMitigationContainer mitigators = mitigationHelper.Install (honestNodes)
; |
| 346 // get pointer to mitigation object |
| 347 Ptr<JammingMitigation> mitigationPtr = mitigators.Get (0); |
| 348 /***************************************************************************/ |
| 349 |
| 350 /** Internet stack **/ |
| 351 InternetStackHelper internet; |
| 352 internet.Install (honestNodes); |
| 353 |
| 354 Ipv4AddressHelper ipv4; |
| 355 NS_LOG_INFO ("Assign IP Addresses."); |
| 356 ipv4.SetBase ("10.1.1.0", "255.255.255.0"); |
| 357 Ipv4InterfaceContainer i = ipv4.Assign (devices); |
| 358 |
| 359 TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory"); |
| 360 Ptr<Socket> recvSink = Socket::CreateSocket (honestNodes.Get (3), tid); // no
de 3, receiver |
| 361 InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), 80); |
| 362 recvSink->Bind (local); |
| 363 recvSink->SetRecvCallback (MakeCallback (&ReceivePacket)); |
| 364 |
| 365 Ptr<Socket> source = Socket::CreateSocket (honestNodes.Get (0), tid); // no
de 0, sender |
| 366 InetSocketAddress remote = InetSocketAddress (Ipv4Address::GetBroadcast (), 80
); |
| 367 source->SetAllowBroadcast (true); |
| 368 source->Connect (remote); |
| 369 |
| 370 /** connect trace sources **/ |
| 371 /***************************************************************************/ |
| 372 // all sources are connected to node 2 |
| 373 // energy source |
| 374 Ptr<EnergySource> basicSourcePtr = energySources.Get (2); |
| 375 basicSourcePtr->TraceConnectWithoutContext ("RemainingEnergy", |
| 376 MakeCallback (&RemainingEnergy)); |
| 377 // using honest node device energy model list |
| 378 Ptr<DeviceEnergyModel> basicRadioModelPtr = deviceModels.Get (2); |
| 379 basicRadioModelPtr->TraceConnectWithoutContext ("TotalEnergyConsumption", |
| 380 MakeCallback (&TotalEnergy)); |
| 381 // wireless module utility |
| 382 Ptr<WirelessModuleUtility> utilityPtr = utilities.Get (2); |
| 383 utilityPtr->TraceConnectWithoutContext ("Rss", MakeCallback (&NodeRss)); |
| 384 utilityPtr->TraceConnectWithoutContext ("Pdr", MakeCallback (&NodePdr)); |
| 385 /***************************************************************************/ |
| 386 |
| 387 |
| 388 /** simulation setup **/ |
| 389 // start traffic |
| 390 Simulator::Schedule (Seconds (startTime), &GenerateTraffic, source, |
| 391 PpacketSize, honestNodes.Get (0), numPackets, |
| 392 interPacketInterval); |
| 393 |
| 394 // start jammer at 7.0 seconds |
| 395 Simulator::Schedule (Seconds (startTime + 7.0), &ns3::Jammer::StartJammer, |
| 396 jammerPtr); |
| 397 |
| 398 // start jamming mitigation at 28.0 seconds |
| 399 Simulator::Schedule (Seconds (startTime + 28.0), |
| 400 &ns3::JammingMitigation::StartMitigation, |
| 401 mitigationPtr); |
| 402 |
| 403 Simulator::Stop (Seconds (60.0)); |
| 404 Simulator::Run (); |
| 405 Simulator::Destroy (); |
| 406 |
| 407 return 0; |
| 408 } |
LEFT | RIGHT |