A Discrete-Event Network Simulator
API
nsclick-udp-client-server-wifi.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 as
5  * published by the Free Software Foundation;
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  */
16 
17 // Adaptation of examples/udp/udp-client-server.cc for
18 // Click based nodes running wifi.
19 //
20 // Network topology:
21 //
22 // (1.4)
23 // (( n4 ))
24 //
25 // 172.16.1.0/24
26 //
27 // (1.1) (1.2) (1.3)
28 // n0 )) (( n1 )) (( n2
29 // WLAN
30 //
31 // - UDP flows from n0 to n1 and n2 to n1.
32 // - All nodes are Click based.
33 // - The single ethernet interface that each node
34 // uses is named 'eth0' in the Click file.
35 // - Node 4 is running in promiscuous mode and can listen in on
36 // the packets being exchanged between n0-n1 and n2-n1.
37 //
38 
39 #include <fstream>
40 #include "ns3/core-module.h"
41 #include "ns3/network-module.h"
42 #include "ns3/internet-module.h"
43 #include "ns3/wifi-module.h"
44 #include "ns3/mobility-module.h"
45 #include "ns3/applications-module.h"
46 #include "ns3/ipv4-click-routing.h"
47 #include "ns3/click-internet-stack-helper.h"
48 
49 using namespace ns3;
50 
51 NS_LOG_COMPONENT_DEFINE ("NsclickUdpClientServerWifi");
52 
53 #ifdef NS3_CLICK
54 void
55 ReadArp (Ptr<Ipv4ClickRouting> clickRouter)
56 {
57  // Access the handlers
58  NS_LOG_INFO (clickRouter->ReadHandler ("wifi/arpquerier", "table"));
59  NS_LOG_INFO (clickRouter->ReadHandler ("wifi/arpquerier", "stats"));
60 }
61 
62 void
63 WriteArp (Ptr<Ipv4ClickRouting> clickRouter)
64 {
65  // Access the handler
66  NS_LOG_INFO (clickRouter->WriteHandler ("wifi/arpquerier", "insert", "172.16.1.2 00:00:00:00:00:02"));
67 }
68 #endif
69 
70 int
71 main (int argc, char *argv[])
72 {
73 #ifdef NS3_CLICK
74  std::string clickConfigFolder = "src/click/examples";
75 
76  CommandLine cmd (__FILE__);
77  cmd.AddValue ("clickConfigFolder", "Base folder for click configuration files", clickConfigFolder);
78  cmd.Parse (argc, argv);
79 
80  //
81  // Enable logging
82  //
83  LogComponentEnable ("NsclickUdpClientServerWifi", LOG_LEVEL_INFO);
84 
85  //
86  // Explicitly create the nodes required by the topology (shown above).
87  //
88  NS_LOG_INFO ("Create nodes.");
90  n.Create (4);
91 
92  NS_LOG_INFO ("Create channels.");
93  //
94  // Explicitly create the channels required by the topology (shown above).
95  //
96  std::string phyMode ("DsssRate1Mbps");
97 
98  // disable fragmentation for frames below 2200 bytes
99  Config::SetDefault ("ns3::WifiRemoteStationManager::FragmentationThreshold", StringValue ("2200"));
100  // turn off RTS/CTS for frames below 2200 bytes
101  Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue ("2200"));
102  // Fix non-unicast data rate to be the same as that of unicast
103  Config::SetDefault ("ns3::WifiRemoteStationManager::NonUnicastMode",
104  StringValue (phyMode));
105 
107  wifi.SetStandard (WIFI_STANDARD_80211b);
108 
109  YansWifiPhyHelper wifiPhy;
110  // This is one parameter that matters when using FixedRssLossModel
111  // set it to zero; otherwise, gain will be added
112  wifiPhy.Set ("RxGain", DoubleValue (0) );
113  // ns-3 supports RadioTap and Prism tracing extensions for 802.11b
115 
116  YansWifiChannelHelper wifiChannel;
117  wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
118  // The below FixedRssLossModel will cause the rss to be fixed regardless
119  // of the distance between the two stations, and the transmit power
120  wifiChannel.AddPropagationLoss ("ns3::FixedRssLossModel","Rss",DoubleValue (-80));
121  wifiPhy.SetChannel (wifiChannel.Create ());
122 
123  // Add an upper mac and disable rate control
124  WifiMacHelper wifiMac;
125  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
126  "DataMode",StringValue (phyMode),
127  "ControlMode",StringValue (phyMode));
128  // Set it to adhoc mode
129  wifiMac.SetType ("ns3::AdhocWifiMac");
130  NetDeviceContainer d = wifi.Install (wifiPhy, wifiMac, n);
131 
133  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
134  positionAlloc->Add (Vector (0.0, 0.0, 0.0));
135  positionAlloc->Add (Vector (10.0, 0.0, 0.0));
136  positionAlloc->Add (Vector (20.0, 0.0, 0.0));
137  positionAlloc->Add (Vector (0.0, 10.0, 0.0));
138  mobility.SetPositionAllocator (positionAlloc);
139  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
140  mobility.Install (n);
141 
142  //
143  // Install Click on the nodes
144  //
145  ClickInternetStackHelper clickinternet;
146  clickinternet.SetClickFile (n.Get (0), clickConfigFolder + "/nsclick-wifi-single-interface.click");
147  clickinternet.SetClickFile (n.Get (1), clickConfigFolder + "/nsclick-wifi-single-interface.click");
148  clickinternet.SetClickFile (n.Get (2), clickConfigFolder + "/nsclick-wifi-single-interface.click");
149 
150  // Node 4 is to run in promiscuous mode. This can be verified
151  // from the pcap trace Node4_in_eth0.pcap generated after running
152  // this script.
153  clickinternet.SetClickFile (n.Get (3), clickConfigFolder + "/nsclick-wifi-single-interface-promisc.click");
154  clickinternet.SetRoutingTableElement (n, "rt");
155  clickinternet.Install (n);
156  Ipv4AddressHelper ipv4;
157  //
158  // We've got the "hardware" in place. Now we need to add IP addresses.
159  //
160  NS_LOG_INFO ("Assign IP Addresses.");
161  ipv4.SetBase ("172.16.1.0", "255.255.255.0");
162  Ipv4InterfaceContainer i = ipv4.Assign (d);
163 
164  NS_LOG_INFO ("Create Applications.");
165  //
166  // Create one udpServer applications on node one.
167  //
168  uint16_t port = 4000;
169  UdpServerHelper server (port);
170  ApplicationContainer apps = server.Install (n.Get (1));
171  apps.Start (Seconds (1.0));
172  apps.Stop (Seconds (10.0));
173 
174  //
175  // Create one UdpClient application to send UDP datagrams from node zero to
176  // node one.
177  //
178  uint32_t MaxPacketSize = 1024;
179  Time interPacketInterval = Seconds (0.5);
180  uint32_t maxPacketCount = 320;
181  UdpClientHelper client (i.GetAddress (1), port);
182  client.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount));
183  client.SetAttribute ("Interval", TimeValue (interPacketInterval));
184  client.SetAttribute ("PacketSize", UintegerValue (MaxPacketSize));
185  apps = client.Install (NodeContainer (n.Get (0), n.Get (2)));
186  apps.Start (Seconds (2.0));
187  apps.Stop (Seconds (10.0));
188 
189  wifiPhy.EnablePcap ("nsclick-udp-client-server-wifi", d);
190 
191  // Force the MAC address of the second node: The current ARP
192  // implementation of Click sends only one ARP request per incoming
193  // packet for an unknown destination and does not retransmit if no
194  // response is received. With the scenario of this example, all ARP
195  // requests of node 3 are lost due to interference from node
196  // 1. Hence, we fill in the ARP table of node 2 before at the
197  // beginning of the simulation
198  Simulator::Schedule (Seconds (0.5), &ReadArp, n.Get (2)->GetObject<Ipv4ClickRouting> ());
199  Simulator::Schedule (Seconds (0.6), &WriteArp, n.Get (2)->GetObject<Ipv4ClickRouting> ());
200  Simulator::Schedule (Seconds (0.7), &ReadArp, n.Get (2)->GetObject<Ipv4ClickRouting> ());
201 
202  //
203  // Now, do the actual simulation.
204  //
205  NS_LOG_INFO ("Run Simulation.");
206  Simulator::Stop (Seconds (20.0));
207  Simulator::Run ();
209  NS_LOG_INFO ("Done.");
210 #else
211  NS_FATAL_ERROR ("Can't use ns-3-click without NSCLICK compiled in");
212 #endif
213 }
ns3::NetDeviceContainer
holds a vector of ns3::NetDevice pointers
Definition: net-device-container.h:42
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
ns3::UdpClientHelper
Create a client application which sends UDP packets carrying a 32bit sequence number and a 64 bit tim...
Definition: udp-client-server-helper.h:94
ns3::YansWifiPhyHelper
Make it easy to create and manage PHY objects for the YANS model.
Definition: yans-wifi-helper.h:161
ns3::CommandLine
Parse command-line arguments.
Definition: command-line.h:228
ns3::ListPositionAllocator::Add
void Add(Vector v)
Add a position to the list of positions.
Definition: position-allocator.cc:70
ns3::YansWifiChannelHelper::SetPropagationDelay
void SetPropagationDelay(std::string name, std::string n0="", const AttributeValue &v0=EmptyAttributeValue(), std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue())
Definition: yans-wifi-helper.cc:74
ns3::LOG_LEVEL_INFO
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition: log.h:107
ns3::PcapHelperForDevice::EnablePcap
void EnablePcap(std::string prefix, Ptr< NetDevice > nd, bool promiscuous=false, bool explicitFilename=false)
Enable pcap output the indicated net device.
Definition: trace-helper.cc:401
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::ApplicationContainer::Stop
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
Definition: application-container.cc:107
ns3::WifiHelper
helps to create WifiNetDevice objects
Definition: wifi-helper.h:327
ns3::Ipv4AddressHelper
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Definition: ipv4-address-helper.h:48
ns3::LogComponentEnable
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:361
ns3::YansWifiChannelHelper::AddPropagationLoss
void AddPropagationLoss(std::string name, std::string n0="", const AttributeValue &v0=EmptyAttributeValue(), std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue())
Definition: yans-wifi-helper.cc:50
ns3::WifiPhyHelper::DLT_IEEE802_11_RADIO
@ DLT_IEEE802_11_RADIO
Include Radiotap link layer information.
Definition: wifi-helper.h:180
ns3::Simulator::Schedule
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:557
ns3::YansWifiPhyHelper::SetChannel
void SetChannel(Ptr< YansWifiChannel > channel)
Definition: yans-wifi-helper.cc:134
ns3::DoubleValue
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:41
ns3::UdpServerHelper
Create a server application which waits for input UDP packets and uses the information carried into t...
Definition: udp-client-server-helper.h:38
ns3::Ipv4AddressHelper::SetBase
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Definition: ipv4-address-helper.cc:64
third.wifi
wifi
Definition: third.py:96
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
NS_FATAL_ERROR
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:165
ns3::YansWifiChannelHelper::Create
Ptr< YansWifiChannel > Create(void) const
Definition: yans-wifi-helper.cc:98
ns3::Simulator::Stop
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:180
NS_LOG_INFO
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
ns3::Ipv4InterfaceContainer
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Definition: ipv4-interface-container.h:55
ns3::WIFI_STANDARD_80211b
@ WIFI_STANDARD_80211b
Definition: wifi-standards.h:128
ns3::Time
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:104
second.cmd
cmd
Definition: second.py:35
ns3::Simulator::Run
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
ns3::StringValue
Hold variables of type string.
Definition: string.h:41
ns3::WifiPhyHelper::SetPcapDataLinkType
void SetPcapDataLinkType(SupportedPcapDataLinkTypes dlt)
Set the data link type of PCAP traces to be used.
Definition: wifi-helper.cc:573
ns3::ApplicationContainer::Start
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter.
Definition: application-container.cc:87
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1289
ns3::Ipv4AddressHelper::Assign
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
Definition: ipv4-address-helper.cc:135
ns3::Simulator::Destroy
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
ns3::ApplicationContainer
holds a vector of ns3::Application pointers.
Definition: application-container.h:43
ns3::TimeValue
AttributeValue implementation for Time.
Definition: nstime.h:1353
ns3::NodeContainer
keep track of a set of node pointers.
Definition: node-container.h:39
ns3::WifiMacHelper::SetType
void SetType(std::string type, Args &&... args)
Definition: wifi-mac-helper.h:130
ns3::Ipv4InterfaceContainer::GetAddress
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
Definition: ipv4-interface-container.cc:59
ns3::UintegerValue
Hold an unsigned integer type.
Definition: uinteger.h:44
ns3::YansWifiChannelHelper
manage and create wifi channel objects for the YANS model.
Definition: yans-wifi-helper.h:37
ns3::WifiMacHelper
create MAC layers for a ns3::WifiNetDevice.
Definition: wifi-mac-helper.h:48
ns3::Config::SetDefault
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:849
ns3::WifiPhyHelper::Set
void Set(std::string name, const AttributeValue &v)
Definition: wifi-helper.cc:140
ns3::MobilityHelper
Helper class used to assign positions and mobility models to nodes.
Definition: mobility-helper.h:43
sample-rng-plot.n
n
Definition: sample-rng-plot.py:37
third.mobility
mobility
Definition: third.py:108
ns3::Ipv4ClickRouting
Class to allow a node to use Click for external routing.
Definition: ipv4-click-routing.h:59
port
uint16_t port
Definition: dsdv-manet.cc:45