A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
third-distributed.cc
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 */
4
5#include "mpi-test-fixtures.h"
6
7#include "ns3/applications-module.h"
8#include "ns3/core-module.h"
9#include "ns3/csma-module.h"
10#include "ns3/internet-module.h"
11#include "ns3/mobility-module.h"
12#include "ns3/mpi-module.h"
13#include "ns3/network-module.h"
14#include "ns3/point-to-point-module.h"
15#include "ns3/ssid.h"
16#include "ns3/yans-wifi-helper.h"
17
18#include <iomanip>
19
20/**
21 * \file
22 * \ingroup mpi
23 *
24 * Distributed version of third.cc from the tutorial.
25 *
26 * Default Network Topology
27 *
28 * (same as third.cc from tutorial)
29 * Distributed simulation, split across the p2p link
30 * |
31 * Rank 0 | Rank 1
32 * -------------------------|----------------------------
33 * Wifi 10.1.3.0
34 * AP
35 * * * * *
36 * | | | | 10.1.1.0
37 * n5 n6 n7 n0 -------------- n1 n2 n3 n4
38 * point-to-point | | | |
39 * ================
40 * LAN 10.1.2.0
41 */
42
43using namespace ns3;
44
45NS_LOG_COMPONENT_DEFINE("ThirdExampleDistributed");
46
47int
48main(int argc, char* argv[])
49{
50 bool verbose = false;
51 uint32_t nCsma = 3;
52 uint32_t nWifi = 3;
53 bool tracing = false;
54 bool nullmsg = false;
55 bool testing = false;
56
57 CommandLine cmd(__FILE__);
58 cmd.AddValue("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma);
59 cmd.AddValue("nWifi", "Number of wifi STA devices", nWifi);
60 cmd.AddValue("verbose", "Tell echo applications to log if true", verbose);
61 cmd.AddValue("tracing", "Enable pcap tracing", tracing);
62 cmd.AddValue("nullmsg", "Enable the use of null-message synchronization", nullmsg);
63 cmd.AddValue("test", "Enable regression test output", testing);
64
65 cmd.Parse(argc, argv);
66
67 // The underlying restriction of 18 is due to the grid position
68 // allocator's configuration; the grid layout will exceed the
69 // bounding box if more than 18 nodes are provided.
70 if (nWifi > 18)
71 {
72 std::cout << "nWifi should be 18 or less; otherwise grid layout exceeds the bounding box"
73 << std::endl;
74 return 1;
75 }
76
77 if (verbose)
78 {
79 LogComponentEnable("UdpEchoClientApplication",
81 LogComponentEnable("UdpEchoServerApplication",
83 }
84
85 // Sequential fallback values
86 uint32_t systemId = 0;
87 uint32_t systemCount = 1;
88
89 // Distributed simulation setup; by default use granted time window algorithm.
90 if (nullmsg)
91 {
92 GlobalValue::Bind("SimulatorImplementationType",
93 StringValue("ns3::NullMessageSimulatorImpl"));
94 }
95 else
96 {
97 GlobalValue::Bind("SimulatorImplementationType",
98 StringValue("ns3::DistributedSimulatorImpl"));
99 }
100
101 MpiInterface::Enable(&argc, &argv);
102
104
105 systemId = MpiInterface::GetSystemId();
106 systemCount = MpiInterface::GetSize();
107
108 // Check for valid distributed parameters.
109 // Must have 2 and only 2 Logical Processors (LPs)
110 if (systemCount != 2)
111 {
112 std::cout << "This simulation requires 2 and only 2 logical processors." << std::endl;
113 return 1;
114 }
115
116 // System id of Wifi side
117 uint32_t systemWifi = 0;
118
119 // System id of CSMA side
120 uint32_t systemCsma = systemCount - 1;
121
123 // Create each end of the P2P link on a separate system (rank)
124 Ptr<Node> p2pNode1 = CreateObject<Node>(systemWifi);
125 Ptr<Node> p2pNode2 = CreateObject<Node>(systemCsma);
126 p2pNodes.Add(p2pNode1);
127 p2pNodes.Add(p2pNode2);
128
130 pointToPoint.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
131 pointToPoint.SetChannelAttribute("Delay", StringValue("2ms"));
132
134 p2pDevices = pointToPoint.Install(p2pNodes);
135
137 csmaNodes.Add(p2pNodes.Get(1));
138 // Create the csma nodes on one system (rank)
139 csmaNodes.Create(nCsma, systemCsma);
140
142 csma.SetChannelAttribute("DataRate", StringValue("100Mbps"));
143 csma.SetChannelAttribute("Delay", TimeValue(NanoSeconds(6560)));
144
146 csmaDevices = csma.Install(csmaNodes);
147
149 // Create the wifi nodes on the other system (rank)
150 wifiStaNodes.Create(nWifi, systemWifi);
152
155 phy.SetChannel(channel.Create());
156
158 Ssid ssid = Ssid("ns-3-ssid");
159
161
163 mac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid), "ActiveProbing", BooleanValue(false));
164 staDevices = wifi.Install(phy, mac, wifiStaNodes);
165
167 mac.SetType("ns3::ApWifiMac", "Ssid", SsidValue(ssid));
168 apDevices = wifi.Install(phy, mac, wifiApNode);
169
171
172 mobility.SetPositionAllocator("ns3::GridPositionAllocator",
173 "MinX",
174 DoubleValue(0.0),
175 "MinY",
176 DoubleValue(0.0),
177 "DeltaX",
178 DoubleValue(5.0),
179 "DeltaY",
180 DoubleValue(10.0),
181 "GridWidth",
182 UintegerValue(3),
183 "LayoutType",
184 StringValue("RowFirst"));
185
186 mobility.SetMobilityModel("ns3::RandomWalk2dMobilityModel",
187 "Bounds",
188 RectangleValue(Rectangle(-50, 50, -50, 50)));
189 mobility.Install(wifiStaNodes);
190
191 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
192 mobility.Install(wifiApNode);
193
195 stack.Install(csmaNodes);
196 stack.Install(wifiApNode);
197 stack.Install(wifiStaNodes);
198
200
201 address.SetBase("10.1.1.0", "255.255.255.0");
203 p2pInterfaces = address.Assign(p2pDevices);
204
205 address.SetBase("10.1.2.0", "255.255.255.0");
207 csmaInterfaces = address.Assign(csmaDevices);
208
209 address.SetBase("10.1.3.0", "255.255.255.0");
210 address.Assign(staDevices);
211 address.Assign(apDevices);
212
213 // If this rank is systemCsma,
214 // it should contain the server application,
215 // since it is on one of the csma nodes
216 if (systemId == systemCsma)
217 {
219
221 serverApps.Start(Seconds(1.0));
222 serverApps.Stop(Seconds(10.0));
223
224 if (testing)
225 {
226 serverApps.Get(0)->TraceConnectWithoutContext("RxWithAddresses",
228 }
229 }
230
231 // If this rank is systemWifi
232 // it should contain the client application,
233 // since it is on one of the wifi nodes
234 if (systemId == systemWifi)
235 {
236 UdpEchoClientHelper echoClient(csmaInterfaces.GetAddress(nCsma), 9);
237 echoClient.SetAttribute("MaxPackets", UintegerValue(1));
238 echoClient.SetAttribute("Interval", TimeValue(Seconds(1.0)));
239 echoClient.SetAttribute("PacketSize", UintegerValue(1024));
240
241 ApplicationContainer clientApps = echoClient.Install(wifiStaNodes.Get(nWifi - 1));
242 clientApps.Start(Seconds(2.0));
243 clientApps.Stop(Seconds(10.0));
244
245 if (testing)
246 {
247 clientApps.Get(0)->TraceConnectWithoutContext("RxWithAddresses",
249 }
250 }
251
253
255
256 if (tracing)
257 {
258 // Depending on the system Id (rank), the pcap information
259 // traced will be different. For example, the ethernet pcap
260 // will be empty for rank0, since these nodes are placed on
261 // on rank 1. All ethernet traffic will take place on rank 1.
262 // Similar differences are seen in the p2p and wireless pcaps.
263 if (systemId == systemCsma)
264 {
265 pointToPoint.EnablePcapAll("third-distributed-csma");
266 phy.EnablePcap("third-distributed-csma", apDevices.Get(0));
267 csma.EnablePcap("third-distributed-csma", csmaDevices.Get(0), true);
268 }
269 else // systemWifi
270 {
271 pointToPoint.EnablePcapAll("third-distributed-wifi");
272 phy.EnablePcap("third-distributed-wifi", apDevices.Get(0));
273 csma.EnablePcap("third-distributed-wifi", csmaDevices.Get(0), true);
274 }
275 }
276
279
280 if (testing)
281 {
283 }
284
285 // Exit the MPI execution environment
287
288 return 0;
289}
holds a vector of ns3::Application pointers.
Parse command-line arguments.
build a set of CsmaNetDevice objects
Definition csma-helper.h:37
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
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.
a 2d rectangle
Definition rectangle.h:24
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
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:25
Hold variables of type string.
Definition string.h:45
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:34
helps to create WifiNetDevice objects
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:191
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1344
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Common methods for MPI examples.
echoClient
Definition first.py:48
address
Definition first.py:36
serverApps
Definition first.py:43
pointToPoint
Definition first.py:27
echoServer
Definition first.py:41
clientApps
Definition first.py:53
stack
Definition first.py:33
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:291
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:684
LogLevel
Logging severity classes and levels.
Definition log.h:83
@ LOG_PREFIX_TIME
Prefix all trace prints with simulation time.
Definition log.h:108
@ LOG_PREFIX_NODE
Prefix all trace prints with simulation node.
Definition log.h:109
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition log.h:93
p2pNodes
Definition second.py:39
p2pInterfaces
Definition second.py:64
csmaInterfaces
Definition second.py:67
csmaNodes
Definition second.py:42
p2pDevices
Definition second.py:50
nCsma
Definition second.py:27
csmaDevices
Definition second.py:56
staDevices
Definition third.py:87
ssid
Definition third.py:82
channel
Definition third.py:77
nWifi
Definition third.py:32
mac
Definition third.py:81
wifi
Definition third.py:84
apDevices
Definition third.py:90
wifiApNode
Definition third.py:75
mobility
Definition third.py:92
wifiStaNodes
Definition third.py:73
phy
Definition third.py:78
bool verbose
bool tracing
Flag to enable/disable generation of tracing files.