A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
nsclick-udp-client-server-wifi.cc
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 
75  //
76  // Enable logging
77  //
78  LogComponentEnable ("NsclickUdpClientServerWifi", LOG_LEVEL_INFO);
79 
80  //
81  // Explicitly create the nodes required by the topology (shown above).
82  //
83  NS_LOG_INFO ("Create nodes.");
84  NodeContainer n;
85  n.Create (4);
86 
87  NS_LOG_INFO ("Create channels.");
88  //
89  // Explicitly create the channels required by the topology (shown above).
90  //
91  std::string phyMode ("DsssRate1Mbps");
92 
93  // disable fragmentation for frames below 2200 bytes
94  Config::SetDefault ("ns3::WifiRemoteStationManager::FragmentationThreshold", StringValue ("2200"));
95  // turn off RTS/CTS for frames below 2200 bytes
96  Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue ("2200"));
97  // Fix non-unicast data rate to be the same as that of unicast
98  Config::SetDefault ("ns3::WifiRemoteStationManager::NonUnicastMode",
99  StringValue (phyMode));
100 
101  WifiHelper wifi;
103 
105  // This is one parameter that matters when using FixedRssLossModel
106  // set it to zero; otherwise, gain will be added
107  wifiPhy.Set ("RxGain", DoubleValue (0) );
108  // ns-3 supports RadioTap and Prism tracing extensions for 802.11b
110 
111  YansWifiChannelHelper wifiChannel;
112  wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
113  // The below FixedRssLossModel will cause the rss to be fixed regardless
114  // of the distance between the two stations, and the transmit power
115  wifiChannel.AddPropagationLoss ("ns3::FixedRssLossModel","Rss",DoubleValue (-80));
116  wifiPhy.SetChannel (wifiChannel.Create ());
117 
118  // Add a non-QoS upper mac, and disable rate control
120  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
121  "DataMode",StringValue (phyMode),
122  "ControlMode",StringValue (phyMode));
123  // Set it to adhoc mode
124  wifiMac.SetType ("ns3::AdhocWifiMac");
125  NetDeviceContainer d = wifi.Install (wifiPhy, wifiMac, n);
126 
127  MobilityHelper mobility;
128  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
129  positionAlloc->Add (Vector (0.0, 0.0, 0.0));
130  positionAlloc->Add (Vector (10.0, 0.0, 0.0));
131  positionAlloc->Add (Vector (20.0, 0.0, 0.0));
132  positionAlloc->Add (Vector (0.0, 10.0, 0.0));
133  mobility.SetPositionAllocator (positionAlloc);
134  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
135  mobility.Install (n);
136 
137  //
138  // Install Click on the nodes
139  //
140  ClickInternetStackHelper clickinternet;
141  clickinternet.SetClickFile (n.Get (0), "src/click/examples/nsclick-wifi-single-interface.click");
142  clickinternet.SetClickFile (n.Get (1), "src/click/examples/nsclick-wifi-single-interface.click");
143  clickinternet.SetClickFile (n.Get (2), "src/click/examples/nsclick-wifi-single-interface.click");
144 
145  // Node 4 is to run in promiscuous mode. This can be verified
146  // from the pcap trace Node4_in_eth0.pcap generated after running
147  // this script.
148  clickinternet.SetClickFile (n.Get (3), "src/click/examples/nsclick-wifi-single-interface-promisc.click");
149  clickinternet.SetRoutingTableElement (n, "rt");
150  clickinternet.Install (n);
151  Ipv4AddressHelper ipv4;
152  //
153  // We've got the "hardware" in place. Now we need to add IP addresses.
154  //
155  NS_LOG_INFO ("Assign IP Addresses.");
156  ipv4.SetBase ("172.16.1.0", "255.255.255.0");
157  Ipv4InterfaceContainer i = ipv4.Assign (d);
158 
159  NS_LOG_INFO ("Create Applications.");
160  //
161  // Create one udpServer applications on node one.
162  //
163  uint16_t port = 4000;
164  UdpServerHelper server (port);
165  ApplicationContainer apps = server.Install (n.Get (1));
166  apps.Start (Seconds (1.0));
167  apps.Stop (Seconds (10.0));
168 
169  //
170  // Create one UdpClient application to send UDP datagrams from node zero to
171  // node one.
172  //
173  uint32_t MaxPacketSize = 1024;
174  Time interPacketInterval = Seconds (0.5);
175  uint32_t maxPacketCount = 320;
176  UdpClientHelper client (i.GetAddress (1), port);
177  client.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount));
178  client.SetAttribute ("Interval", TimeValue (interPacketInterval));
179  client.SetAttribute ("PacketSize", UintegerValue (MaxPacketSize));
180  apps = client.Install (NodeContainer (n.Get (0), n.Get (2)));
181  apps.Start (Seconds (2.0));
182  apps.Stop (Seconds (10.0));
183 
184  wifiPhy.EnablePcap ("nsclick-udp-client-server-wifi", d);
185 
186  // Force the MAC address of the second node: The current ARP
187  // implementation of Click sends only one ARP request per incoming
188  // packet for an unknown destination and does not retransmit if no
189  // response is received. With the scenario of this example, all ARP
190  // requests of node 3 are lost due to interference from node
191  // 1. Hence, we fill in the ARP table of node 2 before at the
192  // beginning of the simulation
193  Simulator::Schedule (Seconds (0.5), &ReadArp, n.Get (2)->GetObject<Ipv4ClickRouting> ());
194  Simulator::Schedule (Seconds (0.6), &WriteArp, n.Get (2)->GetObject<Ipv4ClickRouting> ());
195  Simulator::Schedule (Seconds (0.7), &ReadArp, n.Get (2)->GetObject<Ipv4ClickRouting> ());
196 
197  //
198  // Now, do the actual simulation.
199  //
200  NS_LOG_INFO ("Run Simulation.");
201  Simulator::Stop (Seconds (20.0));
202  Simulator::Run ();
204  NS_LOG_INFO ("Done.");
205 #else
206  NS_FATAL_ERROR ("Can't use ns-3-click without NSCLICK compiled in");
207 #endif
208 }
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())
holds a vector of ns3::Application pointers.
keep track of time unit.
Definition: nstime.h:149
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Ptr< YansWifiChannel > Create(void) const
void SetRemoteStationManager(std::string type, 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: wifi-helper.cc:68
hold variables of type string
Definition: string.h:19
Make it easy to create and manage PHY objects for the yans model.
void SetType(std::string type, 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())
static void Run(void)
Definition: simulator.cc:157
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
void SetPcapDataLinkType(enum SupportedPcapDataLinkTypes dlt)
#define NS_LOG_INFO(msg)
Definition: log.h:264
void Set(std::string name, const AttributeValue &v)
static YansWifiPhyHelper Default(void)
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Definition: simulator.h:820
helps to create WifiNetDevice objects
Definition: wifi-helper.h:92
a 3d vector
Definition: vector.h:31
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:72
NetDeviceContainer Install(const WifiPhyHelper &phy, const WifiMacHelper &mac, NodeContainer c) const
Definition: wifi-helper.cc:97
void SetChannel(Ptr< YansWifiChannel > channel)
void Install(Ptr< Node > node) const
"Layout" a single node according to the current position allocator type.
Create a client application which sends udp packets carrying a 32bit sequence number and a 64 bit tim...
hold objects of type ns3::Time
Definition: nstime.h:700
Hold an unsigned integer type.
Definition: uinteger.h:46
Class to allow a node to use Click for external routing.
holds a vector of ns3::NetDevice pointers
void SetStandard(enum WifiPhyStandard standard)
Definition: wifi-helper.cc:91
Create a server application which waits for input udp packets and uses the information carried into t...
static NqosWifiMacHelper Default(void)
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
create non QoS-enabled MAC layers for a ns3::WifiNetDevice.
static void Destroy(void)
Definition: simulator.cc:121
void SetAttribute(std::string name, const AttributeValue &value)
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:667
keep track of a set of node pointers.
void SetMobilityModel(std::string type, 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(), std::string n8="", const AttributeValue &v8=EmptyAttributeValue(), std::string n9="", const AttributeValue &v9=EmptyAttributeValue())
manage and create wifi channel objects for the yans model.
Helper class used to assign positions and mobility models to nodes.
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
Definition: nstime.h:586
static void Stop(void)
Definition: simulator.cc:164
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
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())
void EnablePcap(std::string prefix, Ptr< NetDevice > nd, bool promiscuous=false, bool explicitFilename=false)
Enable pcap output the indicated net device.
void SetPositionAllocator(Ptr< PositionAllocator > allocator)
Hold an floating point type.
Definition: double.h:41
Ptr< T > GetObject(void) const
Definition: object.h:332
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
void LogComponentEnable(char const *name, enum LogLevel level)
Definition: log.cc:311
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const