A Discrete-Event Network Simulator
QKDNetSim v2.0 (NS-3 v3.41) @ (+)
API
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
wifi-simple-adhoc-grid.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009 University of Washington
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  */
18 
19 //
20 // This program configures a grid (default 5x5) of nodes on an
21 // 802.11b physical layer, with
22 // 802.11b NICs in adhoc mode, and by default, sends one packet of 1000
23 // (application) bytes to node 1.
24 //
25 // The default layout is like this, on a 2-D grid.
26 //
27 // n20 n21 n22 n23 n24
28 // n15 n16 n17 n18 n19
29 // n10 n11 n12 n13 n14
30 // n5 n6 n7 n8 n9
31 // n0 n1 n2 n3 n4
32 //
33 // the layout is affected by the parameters given to GridPositionAllocator;
34 // by default, GridWidth is 5 (nodes per row) and numNodes is 25..
35 //
36 // There are a number of command-line options available to control
37 // the default behavior. The list of available command-line options
38 // can be listed with the following command:
39 // ./ns3 run "wifi-simple-adhoc-grid --help"
40 //
41 // Note that all ns-3 attributes (not just the ones exposed in the below
42 // script) can be changed at command line; see the ns-3 documentation.
43 //
44 // For instance, for this configuration, the physical layer will
45 // stop successfully receiving packets when distance increases beyond
46 // the default of 100m. The cutoff is around 116m; below that value, the
47 // received signal strength falls below the (default) RSSI limit of -82 dBm
48 // used by Wi-Fi's threshold preamble detection running.
49 //
50 // To see this effect, try running at a larger distance, and no packet
51 // reception will be reported:
52 //
53 // ./ns3 run "wifi-simple-adhoc-grid --distance=200"
54 //
55 // The default path through the topology will follow the following node
56 // numbers: 24->23->18->13->12->11->10->5->0
57 //
58 // To see this, the following Bash commands will list the UDP packet
59 // transmissions hop-by-hop, if tracing is enabled:
60 //
61 // ./ns3 run "wifi-simple-adhoc-grid --tracing=1"
62 // grep ^t wifi-simple-adhoc-grid.tr | grep Udp | grep -v olsr | less
63 //
64 // By changing the distance to a smaller value, more nodes can be reached
65 // by each transmission, and the number of forwarding hops will decrease.
66 //
67 // The source node and sink node can be changed like this:
68 //
69 // ./ns3 run "wifi-simple-adhoc-grid --sourceNode=20 --sinkNode=10"
70 //
71 // This script can also be helpful to put the Wifi layer into verbose
72 // logging mode; this command will turn on all wifi logging:
73 //
74 // ./ns3 run "wifi-simple-adhoc-grid --verbose=1"
75 //
76 // By default, trace file writing is off-- to enable it, try:
77 // ./ns3 run "wifi-simple-adhoc-grid --tracing=1"
78 //
79 // When you are done tracing, you will notice many pcap trace files
80 // in your directory. If you have tcpdump installed, you can try this:
81 //
82 // tcpdump -r wifi-simple-adhoc-grid-0-0.pcap -nn -tt
83 //
84 // or you can examine the text-based trace wifi-simple-adhoc-grid.tr with
85 // an editor.
86 //
87 
88 #include "ns3/command-line.h"
89 #include "ns3/config.h"
90 #include "ns3/double.h"
91 #include "ns3/internet-stack-helper.h"
92 #include "ns3/ipv4-address-helper.h"
93 #include "ns3/ipv4-list-routing-helper.h"
94 #include "ns3/ipv4-static-routing-helper.h"
95 #include "ns3/log.h"
96 #include "ns3/mobility-helper.h"
97 #include "ns3/mobility-model.h"
98 #include "ns3/olsr-helper.h"
99 #include "ns3/string.h"
100 #include "ns3/uinteger.h"
101 #include "ns3/yans-wifi-channel.h"
102 #include "ns3/yans-wifi-helper.h"
103 
104 using namespace ns3;
105 
106 NS_LOG_COMPONENT_DEFINE("WifiSimpleAdhocGrid");
107 
113 void
115 {
116  while (socket->Recv())
117  {
118  NS_LOG_UNCOND("Received one packet!");
119  }
120 }
121 
130 static void
131 GenerateTraffic(Ptr<Socket> socket, uint32_t pktSize, uint32_t pktCount, Time pktInterval)
132 {
133  if (pktCount > 0)
134  {
135  socket->Send(Create<Packet>(pktSize));
136  Simulator::Schedule(pktInterval,
138  socket,
139  pktSize,
140  pktCount - 1,
141  pktInterval);
142  }
143  else
144  {
145  socket->Close();
146  }
147 }
148 
149 int
150 main(int argc, char* argv[])
151 {
152  std::string phyMode("DsssRate1Mbps");
153  double distance = 100; // m
154  uint32_t packetSize = 1000; // bytes
155  uint32_t numPackets = 1;
156  uint32_t numNodes = 25; // by default, 5x5
157  uint32_t sinkNode = 0;
158  uint32_t sourceNode = 24;
159  double interval = 1.0; // seconds
160  bool verbose = false;
161  bool tracing = false;
162 
163  CommandLine cmd(__FILE__);
164  cmd.AddValue("phyMode", "Wifi Phy mode", phyMode);
165  cmd.AddValue("distance", "distance (m)", distance);
166  cmd.AddValue("packetSize", "size of application packet sent", packetSize);
167  cmd.AddValue("numPackets", "number of packets generated", numPackets);
168  cmd.AddValue("interval", "interval (seconds) between packets", interval);
169  cmd.AddValue("verbose", "turn on all WifiNetDevice log components", verbose);
170  cmd.AddValue("tracing", "turn on ascii and pcap tracing", tracing);
171  cmd.AddValue("numNodes", "number of nodes", numNodes);
172  cmd.AddValue("sinkNode", "Receiver node number", sinkNode);
173  cmd.AddValue("sourceNode", "Sender node number", sourceNode);
174  cmd.Parse(argc, argv);
175  // Convert to time object
176  Time interPacketInterval = Seconds(interval);
177 
178  // Fix non-unicast data rate to be the same as that of unicast
179  Config::SetDefault("ns3::WifiRemoteStationManager::NonUnicastMode", StringValue(phyMode));
180 
181  NodeContainer c;
182  c.Create(numNodes);
183 
184  // The below set of helpers will help us to put together the wifi NICs we want
186  if (verbose)
187  {
188  WifiHelper::EnableLogComponents(); // Turn on all Wifi logging
189  }
190 
191  YansWifiPhyHelper wifiPhy;
192  // set it to zero; otherwise, gain will be added
193  wifiPhy.Set("RxGain", DoubleValue(-10));
194  // ns-3 supports RadioTap and Prism tracing extensions for 802.11b
196 
197  YansWifiChannelHelper wifiChannel;
198  wifiChannel.SetPropagationDelay("ns3::ConstantSpeedPropagationDelayModel");
199  wifiChannel.AddPropagationLoss("ns3::FriisPropagationLossModel");
200  wifiPhy.SetChannel(wifiChannel.Create());
201 
202  // Add an upper mac and disable rate control
203  WifiMacHelper wifiMac;
204  wifi.SetStandard(WIFI_STANDARD_80211b);
205  wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
206  "DataMode",
207  StringValue(phyMode),
208  "ControlMode",
209  StringValue(phyMode));
210  // Set it to adhoc mode
211  wifiMac.SetType("ns3::AdhocWifiMac");
212  NetDeviceContainer devices = wifi.Install(wifiPhy, wifiMac, c);
213 
215  mobility.SetPositionAllocator("ns3::GridPositionAllocator",
216  "MinX",
217  DoubleValue(0.0),
218  "MinY",
219  DoubleValue(0.0),
220  "DeltaX",
221  DoubleValue(distance),
222  "DeltaY",
223  DoubleValue(distance),
224  "GridWidth",
225  UintegerValue(5),
226  "LayoutType",
227  StringValue("RowFirst"));
228  mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
229  mobility.Install(c);
230 
231  // Enable OLSR
233  Ipv4StaticRoutingHelper staticRouting;
234 
236  list.Add(staticRouting, 0);
237  list.Add(olsr, 10);
238 
240  internet.SetRoutingHelper(list); // has effect on the next Install ()
241  internet.Install(c);
242 
244  NS_LOG_INFO("Assign IP Addresses.");
245  ipv4.SetBase("10.1.1.0", "255.255.255.0");
246  Ipv4InterfaceContainer i = ipv4.Assign(devices);
247 
248  TypeId tid = TypeId::LookupByName("ns3::UdpSocketFactory");
249  Ptr<Socket> recvSink = Socket::CreateSocket(c.Get(sinkNode), tid);
251  recvSink->Bind(local);
253 
254  Ptr<Socket> source = Socket::CreateSocket(c.Get(sourceNode), tid);
255  InetSocketAddress remote = InetSocketAddress(i.GetAddress(sinkNode, 0), 80);
256  source->Connect(remote);
257 
258  if (tracing)
259  {
260  AsciiTraceHelper ascii;
261  wifiPhy.EnableAsciiAll(ascii.CreateFileStream("wifi-simple-adhoc-grid.tr"));
262  wifiPhy.EnablePcap("wifi-simple-adhoc-grid", devices);
263  // Trace routing tables
264  Ptr<OutputStreamWrapper> routingStream =
265  Create<OutputStreamWrapper>("wifi-simple-adhoc-grid.routes", std::ios::out);
267  Ptr<OutputStreamWrapper> neighborStream =
268  Create<OutputStreamWrapper>("wifi-simple-adhoc-grid.neighbors", std::ios::out);
270 
271  // To do-- enable an IP-level trace that shows forwarding events only
272  }
273 
274  // Give OLSR time to converge-- 30 seconds perhaps
277  source,
278  packetSize,
279  numPackets,
280  interPacketInterval);
281 
282  // Output what we are doing
283  NS_LOG_UNCOND("Testing from node " << sourceNode << " to " << sinkNode << " with grid distance "
284  << distance);
285 
286  Simulator::Stop(Seconds(33.0));
287  Simulator::Run();
289 
290  return 0;
291 }
void EnableAsciiAll(std::string prefix)
Enable ascii trace output on each device (which is of the appropriate type) in the set of all nodes c...
Manage ASCII trace files for device models.
Definition: trace-helper.h:174
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we'll use to write the traced bits.
Parse command-line arguments.
Definition: command-line.h:232
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
static Ipv4Address GetAny()
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
Helper class that adds ns3::Ipv4ListRouting objects.
static void PrintNeighborCacheAllEvery(Time printInterval, Ptr< OutputStreamWrapper > stream, Time::Unit unit=Time::S)
prints the neighbor cache of all nodes at regular intervals specified by user.
static void PrintRoutingTableAllEvery(Time printInterval, Ptr< OutputStreamWrapper > stream, Time::Unit unit=Time::S)
prints the routing tables of all nodes at regular intervals specified by user.
Helper class that adds ns3::Ipv4StaticRouting objects.
Helper class used to assign positions and mobility models to nodes.
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
Helper class that adds OLSR routing to nodes.
Definition: olsr-helper.h:42
void EnablePcap(std::string prefix, Ptr< NetDevice > nd, bool promiscuous=false, bool explicitFilename=false)
Enable pcap output the indicated net device.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
virtual int Send(Ptr< Packet > p, uint32_t flags)=0
Send data (or dummy data) to the remote host.
void SetRecvCallback(Callback< void, Ptr< Socket >> receivedData)
Notify application when new data is available to be read.
Definition: socket.cc:128
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
This method wraps the creation of sockets that is performed on a given node by a SocketFactory specif...
Definition: socket.cc:72
virtual int Close()=0
Close a socket.
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
Hold variables of type string.
Definition: string.h:56
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
a unique identifier for an interface.
Definition: type-id.h:59
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:835
Hold an unsigned integer type.
Definition: uinteger.h:45
helps to create WifiNetDevice objects
Definition: wifi-helper.h:324
static void EnableLogComponents()
Helper to enable all WifiNetDevice log components with one statement.
Definition: wifi-helper.cc:880
create MAC layers for a ns3::WifiNetDevice.
void SetType(std::string type, Args &&... args)
void SetPcapDataLinkType(SupportedPcapDataLinkTypes dlt)
Set the data link type of PCAP traces to be used.
Definition: wifi-helper.cc:543
void Set(std::string name, const AttributeValue &v)
Definition: wifi-helper.cc:163
@ DLT_IEEE802_11_RADIO
Include Radiotap link layer information.
Definition: wifi-helper.h:178
manage and create wifi channel objects for the YANS model.
void SetPropagationDelay(std::string name, Ts &&... args)
void AddPropagationLoss(std::string name, Ts &&... args)
Ptr< YansWifiChannel > Create() const
Make it easy to create and manage PHY objects for the YANS model.
void SetChannel(Ptr< YansWifiChannel > channel)
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:890
#define NS_LOG_UNCOND(msg)
Output the requested message unconditionally.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
@ WIFI_STANDARD_80211b
devices
Definition: first.py:42
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:704
Definition: olsr.py:1
cmd
Definition: second.py:40
wifi
Definition: third.py:95
mobility
Definition: third.py:105
#define list
bool verbose
bool tracing
Flag to enable/disable generation of tracing files.
uint32_t pktSize
packet size used for the simulation (in bytes)
static const uint32_t packetSize
Packet size generated at the AP.
void ReceivePacket(Ptr< Socket > socket)
Function called when a packet is received.
static void GenerateTraffic(Ptr< Socket > socket, uint32_t pktSize, uint32_t pktCount, Time pktInterval)
Generate traffic.