A Discrete-Event Network Simulator
API
third-distributed.cc
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License version 2 as
4 * published by the Free Software Foundation;
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 */
15
16#include "mpi-test-fixtures.h"
17
18#include "ns3/applications-module.h"
19#include "ns3/core-module.h"
20#include "ns3/csma-module.h"
21#include "ns3/internet-module.h"
22#include "ns3/mobility-module.h"
23#include "ns3/mpi-module.h"
24#include "ns3/network-module.h"
25#include "ns3/point-to-point-module.h"
26#include "ns3/ssid.h"
27#include "ns3/yans-wifi-helper.h"
28
29#include <iomanip>
30
54using namespace ns3;
55
56NS_LOG_COMPONENT_DEFINE("ThirdExampleDistributed");
57
58int
59main(int argc, char* argv[])
60{
61 bool verbose = false;
62 uint32_t nCsma = 3;
63 uint32_t nWifi = 3;
64 bool tracing = false;
65 bool nullmsg = false;
66 bool testing = false;
67
68 CommandLine cmd(__FILE__);
69 cmd.AddValue("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma);
70 cmd.AddValue("nWifi", "Number of wifi STA devices", nWifi);
71 cmd.AddValue("verbose", "Tell echo applications to log if true", verbose);
72 cmd.AddValue("tracing", "Enable pcap tracing", tracing);
73 cmd.AddValue("nullmsg", "Enable the use of null-message synchronization", nullmsg);
74 cmd.AddValue("test", "Enable regression test output", testing);
75
76 cmd.Parse(argc, argv);
77
78 // The underlying restriction of 18 is due to the grid position
79 // allocator's configuration; the grid layout will exceed the
80 // bounding box if more than 18 nodes are provided.
81 if (nWifi > 18)
82 {
83 std::cout << "nWifi should be 18 or less; otherwise grid layout exceeds the bounding box"
84 << std::endl;
85 return 1;
86 }
87
88 if (verbose)
89 {
90 LogComponentEnable("UdpEchoClientApplication",
92 LogComponentEnable("UdpEchoServerApplication",
94 }
95
96 // Sequential fallback values
97 uint32_t systemId = 0;
98 uint32_t systemCount = 1;
99
100 // Distributed simulation setup; by default use granted time window algorithm.
101 if (nullmsg)
102 {
103 GlobalValue::Bind("SimulatorImplementationType",
104 StringValue("ns3::NullMessageSimulatorImpl"));
105 }
106 else
107 {
108 GlobalValue::Bind("SimulatorImplementationType",
109 StringValue("ns3::DistributedSimulatorImpl"));
110 }
111
112 MpiInterface::Enable(&argc, &argv);
113
114 SinkTracer::Init();
115
116 systemId = MpiInterface::GetSystemId();
117 systemCount = MpiInterface::GetSize();
118
119 // Check for valid distributed parameters.
120 // Must have 2 and only 2 Logical Processors (LPs)
121 if (systemCount != 2)
122 {
123 std::cout << "This simulation requires 2 and only 2 logical processors." << std::endl;
124 return 1;
125 }
126
127 // System id of Wifi side
128 uint32_t systemWifi = 0;
129
130 // System id of CSMA side
131 uint32_t systemCsma = systemCount - 1;
132
134 // Create each end of the P2P link on a separate system (rank)
135 Ptr<Node> p2pNode1 = CreateObject<Node>(systemWifi);
136 Ptr<Node> p2pNode2 = CreateObject<Node>(systemCsma);
137 p2pNodes.Add(p2pNode1);
138 p2pNodes.Add(p2pNode2);
139
141 pointToPoint.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
142 pointToPoint.SetChannelAttribute("Delay", StringValue("2ms"));
143
146
148 csmaNodes.Add(p2pNodes.Get(1));
149 // Create the csma nodes on one system (rank)
150 csmaNodes.Create(nCsma, systemCsma);
151
153 csma.SetChannelAttribute("DataRate", StringValue("100Mbps"));
154 csma.SetChannelAttribute("Delay", TimeValue(NanoSeconds(6560)));
155
157 csmaDevices = csma.Install(csmaNodes);
158
160 // Create the wifi nodes on the other system (rank)
161 wifiStaNodes.Create(nWifi, systemWifi);
163
164 YansWifiChannelHelper channel = YansWifiChannelHelper::Default();
166 phy.SetChannel(channel.Create());
167
169 Ssid ssid = Ssid("ns-3-ssid");
170
172
174 mac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid), "ActiveProbing", BooleanValue(false));
175 staDevices = wifi.Install(phy, mac, wifiStaNodes);
176
178 mac.SetType("ns3::ApWifiMac", "Ssid", SsidValue(ssid));
179 apDevices = wifi.Install(phy, mac, wifiApNode);
180
182
183 mobility.SetPositionAllocator("ns3::GridPositionAllocator",
184 "MinX",
185 DoubleValue(0.0),
186 "MinY",
187 DoubleValue(0.0),
188 "DeltaX",
189 DoubleValue(5.0),
190 "DeltaY",
191 DoubleValue(10.0),
192 "GridWidth",
193 UintegerValue(3),
194 "LayoutType",
195 StringValue("RowFirst"));
196
197 mobility.SetMobilityModel("ns3::RandomWalk2dMobilityModel",
198 "Bounds",
199 RectangleValue(Rectangle(-50, 50, -50, 50)));
200 mobility.Install(wifiStaNodes);
201
202 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
203 mobility.Install(wifiApNode);
204
206 stack.Install(csmaNodes);
207 stack.Install(wifiApNode);
208 stack.Install(wifiStaNodes);
209
211
212 address.SetBase("10.1.1.0", "255.255.255.0");
215
216 address.SetBase("10.1.2.0", "255.255.255.0");
219
220 address.SetBase("10.1.3.0", "255.255.255.0");
221 address.Assign(staDevices);
222 address.Assign(apDevices);
223
224 // If this rank is systemCsma,
225 // it should contain the server application,
226 // since it is on one of the csma nodes
227 if (systemId == systemCsma)
228 {
230
232 serverApps.Start(Seconds(1.0));
233 serverApps.Stop(Seconds(10.0));
234
235 if (testing)
236 {
237 serverApps.Get(0)->TraceConnectWithoutContext("RxWithAddresses",
238 MakeCallback(&SinkTracer::SinkTrace));
239 }
240 }
241
242 // If this rank is systemWifi
243 // it should contain the client application,
244 // since it is on one of the wifi nodes
245 if (systemId == systemWifi)
246 {
248 echoClient.SetAttribute("MaxPackets", UintegerValue(1));
249 echoClient.SetAttribute("Interval", TimeValue(Seconds(1.0)));
250 echoClient.SetAttribute("PacketSize", UintegerValue(1024));
251
253 clientApps.Start(Seconds(2.0));
254 clientApps.Stop(Seconds(10.0));
255
256 if (testing)
257 {
258 clientApps.Get(0)->TraceConnectWithoutContext("RxWithAddresses",
259 MakeCallback(&SinkTracer::SinkTrace));
260 }
261 }
262
263 Ipv4GlobalRoutingHelper::PopulateRoutingTables();
264
265 Simulator::Stop(Seconds(10.0));
266
267 if (tracing == true)
268 {
269 // Depending on the system Id (rank), the pcap information
270 // traced will be different. For example, the ethernet pcap
271 // will be empty for rank0, since these nodes are placed on
272 // on rank 1. All ethernet traffic will take place on rank 1.
273 // Similar differences are seen in the p2p and wireless pcaps.
274 if (systemId == systemCsma)
275 {
276 pointToPoint.EnablePcapAll("third-distributed-csma");
277 phy.EnablePcap("third-distributed-csma", apDevices.Get(0));
278 csma.EnablePcap("third-distributed-csma", csmaDevices.Get(0), true);
279 }
280 else // systemWifi
281 {
282 pointToPoint.EnablePcapAll("third-distributed-wifi");
283 phy.EnablePcap("third-distributed-wifi", apDevices.Get(0));
284 csma.EnablePcap("third-distributed-wifi", csmaDevices.Get(0), true);
285 }
286 }
287
288 Simulator::Run();
289 Simulator::Destroy();
290
291 if (testing)
292 {
293 SinkTracer::Verify(2);
294 }
295
296 // Exit the MPI execution environment
297 MpiInterface::Disable();
298
299 return 0;
300}
holds a vector of ns3::Application pointers.
AttributeValue implementation for Boolean.
Definition: boolean.h:37
Parse command-line arguments.
Definition: command-line.h:232
build a set of CsmaNetDevice objects
Definition: csma-helper.h:48
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
holds a vector of std::pair of Ptr<Ipv4> and interface index.
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.
Build a set of PointToPointNetDevice objects.
a 2d rectangle
Definition: rectangle.h:35
AttributeValue implementation for Rectangle.
The IEEE 802.11 SSID Information Element.
Definition: ssid.h:36
AttributeValue implementation for Ssid.
Hold variables of type string.
Definition: string.h:42
AttributeValue implementation for Time.
Definition: nstime.h:1425
Create an application which sends a UDP packet and waits for an echo of this packet.
Create a server application which waits for input UDP packets and sends them back to the original sen...
Hold an unsigned integer type.
Definition: uinteger.h:45
helps to create WifiNetDevice objects
Definition: wifi-helper.h:325
create MAC layers for a ns3::WifiNetDevice.
manage and create wifi channel objects for the YANS model.
Make it easy to create and manage PHY objects for the YANS model.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1374
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1338
Common methods for MPI examples.
echoClient
Definition: first.py:53
address
Definition: first.py:40
serverApps
Definition: first.py:48
pointToPoint
Definition: first.py:31
echoServer
Definition: first.py:46
clientApps
Definition: first.py:58
stack
Definition: first.py:37
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:691
LogLevel
Logging severity classes and levels.
Definition: log.h:94
@ LOG_PREFIX_TIME
Prefix all trace prints with simulation time.
Definition: log.h:119
@ LOG_PREFIX_NODE
Prefix all trace prints with simulation node.
Definition: log.h:120
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition: log.h:107
uint32_t GetSize(Ptr< const Packet > packet, const WifiMacHeader *hdr, bool isAmpdu)
Return the total size of the packet after WifiMacHeader and FCS trailer have been added.
Definition: wifi-utils.cc:132
void LogComponentEnable(const char *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:358
p2pNodes
Definition: second.py:43
p2pInterfaces
Definition: second.py:68
csmaInterfaces
Definition: second.py:71
csmaNodes
Definition: second.py:46
csma
Definition: second.py:56
p2pDevices
Definition: second.py:54
nCsma
Definition: second.py:31
cmd
Definition: second.py:33
csmaDevices
Definition: second.py:60
staDevices
Definition: third.py:91
ssid
Definition: third.py:86
channel
Definition: third.py:81
nWifi
Definition: third.py:36
mac
Definition: third.py:85
wifi
Definition: third.py:88
apDevices
Definition: third.py:94
wifiApNode
Definition: third.py:79
mobility
Definition: third.py:96
wifiStaNodes
Definition: third.py:77
phy
Definition: third.py:82
bool verbose
bool tracing
Flag to enable/disable generation of tracing files.