A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
olsr-hna.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Authors: Lalith Suresh <suresh.lalith@gmail.com>
18  *
19  */
20 
21 //
22 // This script, adapted from examples/wireless/wifi-simple-adhoc illustrates
23 // the use of OLSR HNA.
24 //
25 // Network Topology:
26 //
27 // |------ OLSR ------| |---- non-OLSR ----|
28 // A )))) (((( B ------------------- C
29 // 10.1.1.1 10.1.1.2 172.16.1.2 172.16.1.1
30 //
31 // Node A needs to send a UDP packet to node C. This can be done only after
32 // A receives an HNA message from B, in which B announces 172.16.1.0/24
33 // as an associated network.
34 //
35 // If no HNA message is generated by B, a will not be able to form a route to C.
36 // This can be verified as follows:
37 //
38 // ./waf --run olsr-hna
39 //
40 // There are two ways to make a node to generate HNA messages.
41 //
42 // One way is to use olsr::RoutingProtocol::SetRoutingTableAssociation ()
43 // to use which you may run:
44 //
45 // ./waf --run "olsr-hna --assocMethod1=1"
46 //
47 // The other way is to use olsr::RoutingProtocol::AddHostNetworkAssociation ()
48 // to use which you may run:
49 //
50 // ./waf --run "olsr-hna --assocMethod2=1"
51 //
52 
53 #include "ns3/core-module.h"
54 #include "ns3/network-module.h"
55 #include "ns3/mobility-module.h"
56 #include "ns3/config-store-module.h"
57 #include "ns3/wifi-module.h"
58 #include "ns3/csma-module.h"
59 #include "ns3/internet-module.h"
60 #include "ns3/olsr-routing-protocol.h"
61 #include "ns3/olsr-helper.h"
62 
63 #include <iostream>
64 #include <fstream>
65 #include <vector>
66 #include <string>
67 
68 NS_LOG_COMPONENT_DEFINE ("OlsrHna");
69 
70 using namespace ns3;
71 
73 {
74  NS_LOG_UNCOND ("Received one packet!");
75 }
76 
77 static void GenerateTraffic (Ptr<Socket> socket, uint32_t pktSize,
78  uint32_t pktCount, Time pktInterval )
79 {
80  if (pktCount > 0)
81  {
82  socket->Send (Create<Packet> (pktSize));
83  Simulator::Schedule (pktInterval, &GenerateTraffic,
84  socket, pktSize,pktCount-1, pktInterval);
85  }
86  else
87  {
88  socket->Close ();
89  }
90 }
91 
92 
93 int main (int argc, char *argv[])
94 {
95  std::string phyMode ("DsssRate1Mbps");
96  double rss = -80; // -dBm
97  uint32_t packetSize = 1000; // bytes
98  uint32_t numPackets = 1;
99  double interval = 1.0; // seconds
100  bool verbose = false;
101  bool assocMethod1 = false;
102  bool assocMethod2 = false;
103 
104  CommandLine cmd;
105 
106  cmd.AddValue ("phyMode", "Wifi Phy mode", phyMode);
107  cmd.AddValue ("rss", "received signal strength", rss);
108  cmd.AddValue ("packetSize", "size of application packet sent", packetSize);
109  cmd.AddValue ("numPackets", "number of packets generated", numPackets);
110  cmd.AddValue ("interval", "interval (seconds) between packets", interval);
111  cmd.AddValue ("verbose", "turn on all WifiNetDevice log components", verbose);
112  cmd.AddValue ("assocMethod1", "Use SetRoutingTableAssociation () method", assocMethod1);
113  cmd.AddValue ("assocMethod2", "Use AddHostNetworkAssociation () method", assocMethod2);
114 
115  cmd.Parse (argc, argv);
116  // Convert to time object
117  Time interPacketInterval = Seconds (interval);
118 
119  // disable fragmentation for frames below 2200 bytes
120  Config::SetDefault ("ns3::WifiRemoteStationManager::FragmentationThreshold", StringValue ("2200"));
121  // turn off RTS/CTS for frames below 2200 bytes
122  Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue ("2200"));
123  // Fix non-unicast data rate to be the same as that of unicast
124  Config::SetDefault ("ns3::WifiRemoteStationManager::NonUnicastMode",
125  StringValue (phyMode));
126 
127  NodeContainer olsrNodes;
128  olsrNodes.Create (2);
129 
130  NodeContainer csmaNodes;
131  csmaNodes.Create (1);
132 
133  // The below set of helpers will help us to put together the wifi NICs we want
134  WifiHelper wifi;
135  if (verbose)
136  {
137  wifi.EnableLogComponents (); // Turn on all Wifi logging
138  }
140 
142  // This is one parameter that matters when using FixedRssLossModel
143  // set it to zero; otherwise, gain will be added
144  wifiPhy.Set ("RxGain", DoubleValue (0) );
145  // ns-3 supports RadioTap and Prism tracing extensions for 802.11b
147 
148  YansWifiChannelHelper wifiChannel;
149  wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
150  // The below FixedRssLossModel will cause the rss to be fixed regardless
151  // of the distance between the two stations, and the transmit power
152  wifiChannel.AddPropagationLoss ("ns3::FixedRssLossModel","Rss",DoubleValue (rss));
153  wifiPhy.SetChannel (wifiChannel.Create ());
154 
155  // Add a non-QoS upper mac, and disable rate control
157  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
158  "DataMode",StringValue (phyMode),
159  "ControlMode",StringValue (phyMode));
160  // Set it to adhoc mode
161  wifiMac.SetType ("ns3::AdhocWifiMac");
162  NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, olsrNodes);
163 
164  CsmaHelper csma;
165  csma.SetChannelAttribute ("DataRate", DataRateValue (DataRate (5000000)));
166  csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
167  NetDeviceContainer csmaDevices = csma.Install (NodeContainer (csmaNodes.Get (0), olsrNodes.Get (1)));
168 
169  // Note that with FixedRssLossModel, the positions below are not
170  // used for received signal strength.
171  MobilityHelper mobility;
172  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
173  positionAlloc->Add (Vector (0.0, 0.0, 0.0));
174  positionAlloc->Add (Vector (5.0, 0.0, 0.0));
175  mobility.SetPositionAllocator (positionAlloc);
176  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
177  mobility.Install (olsrNodes);
178 
179  OlsrHelper olsr;
180 
181  // Specify Node B's csma device as a non-OLSR device.
182  olsr.ExcludeInterface (olsrNodes.Get (1), 2);
183 
184  Ipv4StaticRoutingHelper staticRouting;
185 
187  list.Add (staticRouting, 0);
188  list.Add (olsr, 10);
189 
190  InternetStackHelper internet_olsr;
191  internet_olsr.SetRoutingHelper (list); // has effect on the next Install ()
192  internet_olsr.Install (olsrNodes);
193 
194  InternetStackHelper internet_csma;
195  internet_csma.Install (csmaNodes);
196 
197  Ipv4AddressHelper ipv4;
198  NS_LOG_INFO ("Assign IP Addresses.");
199  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
200  ipv4.Assign (devices);
201 
202  ipv4.SetBase ("172.16.1.0", "255.255.255.0");
203  ipv4.Assign (csmaDevices);
204 
205  TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
206  Ptr<Socket> recvSink = Socket::CreateSocket (csmaNodes.Get (0), tid);
208  recvSink->Bind (local);
210 
211  Ptr<Socket> source = Socket::CreateSocket (olsrNodes.Get (0), tid);
212  InetSocketAddress remote = InetSocketAddress (Ipv4Address ("172.16.1.1"), 80);
213  source->Connect (remote);
214 
215  // Obtain olsr::RoutingProtocol instance of gateway node
216  // (namely, node B) and add the required association
217  Ptr<Ipv4> stack = olsrNodes.Get (1)->GetObject<Ipv4> ();
218  Ptr<Ipv4RoutingProtocol> rp_Gw = (stack->GetRoutingProtocol ());
219  Ptr<Ipv4ListRouting> lrp_Gw = DynamicCast<Ipv4ListRouting> (rp_Gw);
220 
221  Ptr<olsr::RoutingProtocol> olsrrp_Gw;
222 
223  for (uint32_t i = 0; i < lrp_Gw->GetNRoutingProtocols (); i++)
224  {
225  int16_t priority;
226  Ptr<Ipv4RoutingProtocol> temp = lrp_Gw->GetRoutingProtocol (i, priority);
227  if (DynamicCast<olsr::RoutingProtocol> (temp))
228  {
229  olsrrp_Gw = DynamicCast<olsr::RoutingProtocol> (temp);
230  }
231  }
232 
233  if (assocMethod1)
234  {
235  // Create a special Ipv4StaticRouting instance for RoutingTableAssociation
236  // Even the Ipv4StaticRouting instance added to list may be used
237  Ptr<Ipv4StaticRouting> hnaEntries = Create<Ipv4StaticRouting> ();
238 
239  // Add the required routes into the Ipv4StaticRouting Protocol instance
240  // and have the node generate HNA messages for all these routes
241  // which are associated with non-OLSR interfaces specified above.
242  hnaEntries->AddNetworkRouteTo (Ipv4Address ("172.16.1.0"), Ipv4Mask ("255.255.255.0"), uint32_t (2), uint32_t (1));
243  olsrrp_Gw->SetRoutingTableAssociation (hnaEntries);
244  }
245 
246  if (assocMethod2)
247  {
248  // Specify the required associations directly.
249  olsrrp_Gw->AddHostNetworkAssociation (Ipv4Address ("172.16.1.0"), Ipv4Mask ("255.255.255.0"));
250  }
251 
252  // Tracing
253  wifiPhy.EnablePcap ("olsr-hna", devices);
254  csma.EnablePcap ("olsr-hna", csmaDevices, false);
255 
257  Seconds (15.0), &GenerateTraffic,
258  source, packetSize, numPackets, interPacketInterval);
259 
260  Simulator::Stop (Seconds (20.0));
261  Simulator::Run ();
263 
264  return 0;
265 }