A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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
31/**
32 * \file
33 * \ingroup mpi
34 *
35 * Distributed version of third.cc from the tutorial.
36 *
37 * Default Network Topology
38 *
39 * (same as third.cc from tutorial)
40 * Distributed simulation, split across the p2p link
41 * |
42 * Rank 0 | Rank 1
43 * -------------------------|----------------------------
44 * Wifi 10.1.3.0
45 * AP
46 * * * * *
47 * | | | | 10.1.1.0
48 * n5 n6 n7 n0 -------------- n1 n2 n3 n4
49 * point-to-point | | | |
50 * ================
51 * LAN 10.1.2.0
52 */
53
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
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
145 p2pDevices = pointToPoint.Install(p2pNodes);
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
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");
214 p2pInterfaces = address.Assign(p2pDevices);
215
216 address.SetBase("10.1.2.0", "255.255.255.0");
218 csmaInterfaces = address.Assign(csmaDevices);
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",
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 {
247 UdpEchoClientHelper echoClient(csmaInterfaces.GetAddress(nCsma), 9);
248 echoClient.SetAttribute("MaxPackets", UintegerValue(1));
249 echoClient.SetAttribute("Interval", TimeValue(Seconds(1.0)));
250 echoClient.SetAttribute("PacketSize", UintegerValue(1024));
251
252 ApplicationContainer clientApps = echoClient.Install(wifiStaNodes.Get(nWifi - 1));
253 clientApps.Start(Seconds(2.0));
254 clientApps.Stop(Seconds(10.0));
255
256 if (testing)
257 {
258 clientApps.Get(0)->TraceConnectWithoutContext("RxWithAddresses",
260 }
261 }
262
264
266
267 if (tracing)
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
290
291 if (testing)
292 {
294 }
295
296 // Exit the MPI execution environment
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
static void Bind(std::string name, const AttributeValue &value)
Iterate over the set of GlobalValues until a matching name is found and then set its value with Globa...
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
static void PopulateRoutingTables()
Build a routing database and initialize the routing tables of the nodes in the simulation.
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Helper class used to assign positions and mobility models to nodes.
static uint32_t GetSystemId()
Get the id number of this rank.
static uint32_t GetSize()
Get the number of ranks used by ns-3.
static void Disable()
Clean up the ns-3 parallel communications interface.
static void Enable(int *pargc, char ***pargv)
Setup the parallel communication interface.
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
Build a set of PointToPointNetDevice objects.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
a 2d rectangle
Definition: rectangle.h:35
AttributeValue implementation for Rectangle.
Definition: rectangle.h:125
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
static void SinkTrace(const ns3::Ptr< const ns3::Packet > packet, const ns3::Address &srcAddress, const ns3::Address &destAddress)
PacketSink receive trace callback.
static void Verify(unsigned long expectedCount)
Verify the sink trace count observed matches the expected count.
static void Init()
PacketSink Init.
The IEEE 802.11 SSID Information Element.
Definition: ssid.h:36
AttributeValue implementation for Ssid.
Definition: ssid.h:96
Hold variables of type string.
Definition: string.h:56
AttributeValue implementation for Time.
Definition: nstime.h:1406
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:324
create MAC layers for a ns3::WifiNetDevice.
manage and create wifi channel objects for the YANS model.
static YansWifiChannelHelper Default()
Create a channel helper in a default working state.
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:1355
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
Common methods for MPI examples.
ns echoServer
Definition: first.py:52
ns serverApps
Definition: first.py:54
ns echoClient
Definition: first.py:59
ns clientApps
Definition: first.py:64
ns address
Definition: first.py:47
ns stack
Definition: first.py:44
ns pointToPoint
Definition: first.py:38
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:302
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:706
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:104
ns cmd
Definition: second.py:40
ns csmaInterfaces
Definition: second.py:78
ns p2pNodes
Definition: second.py:50
ns csmaNodes
Definition: second.py:53
ns csma
Definition: second.py:63
ns p2pDevices
Definition: second.py:61
ns p2pInterfaces
Definition: second.py:75
ns csmaDevices
Definition: second.py:67
c_int nCsma
Definition: second.py:38
ns wifi
Definition: third.py:95
ns ssid
Definition: third.py:93
ns apDevices
Definition: third.py:103
ns staDevices
Definition: third.py:100
c_int nWifi
Definition: third.py:43
ns mac
Definition: third.py:92
ns wifiApNode
Definition: third.py:86
ns channel
Definition: third.py:88
ns mobility
Definition: third.py:105
ns wifiStaNodes
Definition: third.py:84
ns phy
Definition: third.py:89
bool verbose
bool tracing
Flag to enable/disable generation of tracing files.