OLD | NEW |
(Empty) | |
| 1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 /* |
| 3 * Copyright (c) 2016 NITK Surathkal |
| 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: Ankit Deepak <adadeepak8@gmail.com> |
| 19 * Shravya Ks <shravya.ks0@gmail.com>, |
| 20 * Mohit P. Tahiliani <tahiliani@nitk.edu.in> |
| 21 */ |
| 22 |
| 23 /* |
| 24 * PORT NOTE: This code was ported from ns-3 IPv4 implementation (src/olsr). A
lmost all |
| 25 * comments have also been ported from the same |
| 26 */ |
| 27 |
| 28 |
| 29 // |
| 30 // This script, adapted from examples/wireless/wifi-simple-adhoc illustrates |
| 31 // the use of OLSR6 HNA. |
| 32 // |
| 33 // Network Topology: |
| 34 // |
| 35 // |-OLSR6(2001:1::/64)-| |-non-OLSR6(2001:2::/64)-| |
| 36 // A )))) (((( B ------------------- C |
| 37 // |
| 38 // Node A needs to send a UDP packet to node C. This can be done only after |
| 39 // A receives an HNA message from B, in which B announces 2001:2""/64 |
| 40 // as an associated network. |
| 41 // |
| 42 // If no HNA message is generated by B, a will not be able to form a route to C. |
| 43 // This can be verified as follows: |
| 44 // |
| 45 // ./waf --run olsr6-hna |
| 46 // |
| 47 // There are two ways to make a node to generate HNA messages. |
| 48 // |
| 49 // One way is to use olsr6::RoutingProtocol::SetRoutingTableAssociation () |
| 50 // to use which you may run: |
| 51 // |
| 52 // ./waf --run "olsr6-hna --assocMethod1=1" |
| 53 // |
| 54 // The other way is to use olsr6::RoutingProtocol::AddHostNetworkAssociation () |
| 55 // to use which you may run: |
| 56 // |
| 57 // ./waf --run "olsr6-hna --assocMethod2=1" |
| 58 // |
| 59 |
| 60 #include "ns3/core-module.h" |
| 61 #include "ns3/network-module.h" |
| 62 #include "ns3/mobility-module.h" |
| 63 #include "ns3/config-store-module.h" |
| 64 #include "ns3/wifi-module.h" |
| 65 #include "ns3/csma-module.h" |
| 66 #include "ns3/internet-module.h" |
| 67 #include "ns3/olsr6-routing-protocol.h" |
| 68 #include "ns3/olsr6-helper.h" |
| 69 |
| 70 #include <iostream> |
| 71 #include <fstream> |
| 72 #include <vector> |
| 73 #include <string> |
| 74 |
| 75 using namespace ns3; |
| 76 |
| 77 NS_LOG_COMPONENT_DEFINE ("Olsr6Hna"); |
| 78 |
| 79 void ReceivePacket (Ptr<Socket> socket) |
| 80 { |
| 81 NS_LOG_UNCOND ("Received one packet!"); |
| 82 } |
| 83 |
| 84 static void GenerateTraffic (Ptr<Socket> socket, uint32_t pktSize, |
| 85 uint32_t pktCount, Time pktInterval ) |
| 86 { |
| 87 if (pktCount > 0) |
| 88 { |
| 89 socket->Send (Create<Packet> (pktSize)); |
| 90 Simulator::Schedule (pktInterval, &GenerateTraffic, |
| 91 socket, pktSize,pktCount - 1, pktInterval); |
| 92 } |
| 93 else |
| 94 { |
| 95 socket->Close (); |
| 96 } |
| 97 } |
| 98 |
| 99 |
| 100 int main (int argc, char *argv[]) |
| 101 { |
| 102 std::string phyMode ("DsssRate1Mbps"); |
| 103 double rss = -80; // -dBm |
| 104 uint32_t packetSize = 1000; // bytes |
| 105 uint32_t numPackets = 1; |
| 106 double interval = 1.0; // seconds |
| 107 bool verbose = false; |
| 108 bool assocMethod1 = false; |
| 109 bool assocMethod2 = false; |
| 110 |
| 111 CommandLine cmd; |
| 112 |
| 113 cmd.AddValue ("phyMode", "Wifi Phy mode", phyMode); |
| 114 cmd.AddValue ("rss", "received signal strength", rss); |
| 115 cmd.AddValue ("packetSize", "size of application packet sent", packetSize); |
| 116 cmd.AddValue ("numPackets", "number of packets generated", numPackets); |
| 117 cmd.AddValue ("interval", "interval (seconds) between packets", interval); |
| 118 cmd.AddValue ("verbose", "turn on all WifiNetDevice log components", verbose); |
| 119 cmd.AddValue ("assocMethod1", "Use SetRoutingTableAssociation () method", asso
cMethod1); |
| 120 cmd.AddValue ("assocMethod2", "Use AddHostNetworkAssociation () method", assoc
Method2); |
| 121 |
| 122 cmd.Parse (argc, argv); |
| 123 // Convert to time object |
| 124 Time interPacketInterval = Seconds (interval); |
| 125 |
| 126 // disable fragmentation for frames below 2200 bytes |
| 127 Config::SetDefault ("ns3::WifiRemoteStationManager::FragmentationThreshold", S
tringValue ("2200")); |
| 128 // turn off RTS/CTS for frames below 2200 bytes |
| 129 Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringVa
lue ("2200")); |
| 130 // Fix non-unicast data rate to be the same as that of unicast |
| 131 Config::SetDefault ("ns3::WifiRemoteStationManager::NonUnicastMode", |
| 132 StringValue (phyMode)); |
| 133 |
| 134 NodeContainer olsr6Nodes; |
| 135 olsr6Nodes.Create (2); |
| 136 |
| 137 NodeContainer csmaNodes; |
| 138 csmaNodes.Create (1); |
| 139 |
| 140 // The below set of helpers will help us to put together the wifi NICs we want |
| 141 WifiHelper wifi; |
| 142 if (verbose) |
| 143 { |
| 144 wifi.EnableLogComponents (); // Turn on all Wifi logging |
| 145 } |
| 146 wifi.SetStandard (WIFI_PHY_STANDARD_80211b); |
| 147 |
| 148 YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default (); |
| 149 // This is one parameter that matters when using FixedRssLossModel |
| 150 // set it to zero; otherwise, gain will be added |
| 151 wifiPhy.Set ("RxGain", DoubleValue (0) ); |
| 152 // ns-3 supports RadioTap and Prism tracing extensions for 802.11b |
| 153 wifiPhy.SetPcapDataLinkType (YansWifiPhyHelper::DLT_IEEE802_11_RADIO); |
| 154 |
| 155 YansWifiChannelHelper wifiChannel; |
| 156 wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel"); |
| 157 // The below FixedRssLossModel will cause the rss to be fixed regardless |
| 158 // of the distance between the two stations, and the transmit power |
| 159 wifiChannel.AddPropagationLoss ("ns3::FixedRssLossModel","Rss",DoubleValue (rs
s)); |
| 160 wifiPhy.SetChannel (wifiChannel.Create ()); |
| 161 |
| 162 // Add a mac and disable rate control |
| 163 WifiMacHelper wifiMac; |
| 164 wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", |
| 165 "DataMode",StringValue (phyMode), |
| 166 "ControlMode",StringValue (phyMode)); |
| 167 // Set it to adhoc mode |
| 168 wifiMac.SetType ("ns3::AdhocWifiMac"); |
| 169 NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, olsr6Nodes); |
| 170 |
| 171 CsmaHelper csma; |
| 172 csma.SetChannelAttribute ("DataRate", DataRateValue (DataRate (5000000))); |
| 173 csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2))); |
| 174 NetDeviceContainer csmaDevices = csma.Install (NodeContainer (csmaNodes.Get (0
), olsr6Nodes.Get (1))); |
| 175 |
| 176 // Note that with FixedRssLossModel, the positions below are not |
| 177 // used for received signal strength. |
| 178 MobilityHelper mobility; |
| 179 Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator>
(); |
| 180 positionAlloc->Add (Vector (0.0, 0.0, 0.0)); |
| 181 positionAlloc->Add (Vector (5.0, 0.0, 0.0)); |
| 182 mobility.SetPositionAllocator (positionAlloc); |
| 183 mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); |
| 184 mobility.Install (olsr6Nodes); |
| 185 |
| 186 Olsr6Helper olsr6; |
| 187 |
| 188 // Specify Node B's csma device as a non-OLSR6 device. |
| 189 olsr6.ExcludeInterface (olsr6Nodes.Get (1), 2); |
| 190 |
| 191 Ipv6StaticRoutingHelper staticRouting; |
| 192 |
| 193 Ipv6ListRoutingHelper list; |
| 194 list.Add (staticRouting, 0); |
| 195 list.Add (olsr6, 10); |
| 196 |
| 197 InternetStackHelper internet_olsr6; |
| 198 internet_olsr6.SetRoutingHelper (list); // has effect on the next Install () |
| 199 internet_olsr6.Install (olsr6Nodes); |
| 200 |
| 201 InternetStackHelper internet_csma; |
| 202 internet_csma.Install (csmaNodes); |
| 203 |
| 204 Ipv6AddressHelper ipv6; |
| 205 NS_LOG_INFO ("Assign IP Addresses."); |
| 206 ipv6.SetBase ("2001:0:1::",Ipv6Prefix (64)); |
| 207 ipv6.Assign (devices); |
| 208 |
| 209 ipv6.SetBase ("2001:0:2::", Ipv6Prefix (64)); |
| 210 ipv6.Assign (csmaDevices); |
| 211 |
| 212 TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory"); |
| 213 Ptr<Socket> recvSink = Socket::CreateSocket (csmaNodes.Get (0), tid); |
| 214 Inet6SocketAddress local = Inet6SocketAddress (Ipv6Address::GetAny (), 80); |
| 215 recvSink->Bind (local); |
| 216 recvSink->SetRecvCallback (MakeCallback (&ReceivePacket)); |
| 217 |
| 218 Ptr<Socket> source = Socket::CreateSocket (olsr6Nodes.Get (0), tid); |
| 219 Inet6SocketAddress remote = Inet6SocketAddress (Ipv6Address ("2001:0:2:0:200:f
f:fe00:1"), 80); |
| 220 source->Connect (remote); |
| 221 |
| 222 // Obtain olsr::RoutingProtocol instance of gateway node |
| 223 // (namely, node B) and add the required association |
| 224 Ptr<Ipv6> stack = olsr6Nodes.Get (1)->GetObject<Ipv6> (); |
| 225 Ptr<Ipv6RoutingProtocol> rp_Gw = (stack->GetRoutingProtocol ()); |
| 226 Ptr<Ipv6ListRouting> lrp_Gw = DynamicCast<Ipv6ListRouting> (rp_Gw); |
| 227 |
| 228 Ptr<olsr6::RoutingProtocol> olsrrp_Gw; |
| 229 |
| 230 for (uint32_t i = 0; i < lrp_Gw->GetNRoutingProtocols (); i++) |
| 231 { |
| 232 int16_t priority; |
| 233 Ptr<Ipv6RoutingProtocol> temp = lrp_Gw->GetRoutingProtocol (i, priority); |
| 234 if (DynamicCast<olsr6::RoutingProtocol> (temp)) |
| 235 { |
| 236 olsrrp_Gw = DynamicCast<olsr6::RoutingProtocol> (temp); |
| 237 } |
| 238 } |
| 239 |
| 240 if (assocMethod1) |
| 241 { |
| 242 // Create a special Ipv6StaticRouting instance for RoutingTableAssociation |
| 243 // Even the Ipv6StaticRouting instance added to list may be used |
| 244 Ptr<Ipv6StaticRouting> hnaEntries = Create<Ipv6StaticRouting> (); |
| 245 |
| 246 // Add the required routes into the Ipv6StaticRouting Protocol instance |
| 247 // and have the node generate HNA messages for all these routes |
| 248 // which are associated with non-OLSR interfaces specified above. |
| 249 hnaEntries->AddNetworkRouteTo (Ipv6Address ("2001:0:2::"), Ipv6Prefix (64)
, uint32_t (2), uint32_t (1)); |
| 250 olsrrp_Gw->SetRoutingTableAssociation (hnaEntries); |
| 251 } |
| 252 |
| 253 if (assocMethod2) |
| 254 { |
| 255 // Specify the required associations directly. |
| 256 olsrrp_Gw->AddHostNetworkAssociation (Ipv6Address ("2001:0:2::"), Ipv6Pref
ix (64)); |
| 257 } |
| 258 |
| 259 // Tracing |
| 260 wifiPhy.EnablePcap ("olsr-hna", devices); |
| 261 csma.EnablePcap ("olsr-hna", csmaDevices, false); |
| 262 |
| 263 Simulator::ScheduleWithContext (source->GetNode ()->GetId (), |
| 264 Seconds (15.0), &GenerateTraffic, |
| 265 source, packetSize, numPackets, interPacketInt
erval); |
| 266 |
| 267 |
| 268 Simulator::Stop (Seconds (20.0)); |
| 269 Simulator::Run (); |
| 270 Simulator::Destroy (); |
| 271 |
| 272 return 0; |
| 273 } |
OLD | NEW |