A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
simple-distributed.cc
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 */
4
5/**
6 * \file
7 * \ingroup mpi
8 *
9 * TestDistributed creates a dumbbell topology and logically splits it in
10 * half. The left half is placed on logical processor 0 and the right half
11 * is placed on logical processor 1.
12 *
13 * ------- -------
14 * RANK 0 RANK 1
15 * ------- | -------
16 * |
17 * n0 ---------| | |---------- n6
18 * | | |
19 * n1 -------\ | | | /------- n7
20 * n4 ----------|---------- n5
21 * n2 -------/ | | | \------- n8
22 * | | |
23 * n3 ---------| | |---------- n9
24 *
25 *
26 * OnOff clients are placed on each left leaf node. Each right leaf node
27 * is a packet sink for a left leaf node. As a packet travels from one
28 * logical processor to another (the link between n4 and n5), MPI messages
29 * are passed containing the serialized packet. The message is then
30 * deserialized into a new packet and sent on as normal.
31 *
32 * One packet is sent from each left leaf node. The packet sinks on the
33 * right leaf nodes output logging information when they receive the packet.
34 */
35
36#include "mpi-test-fixtures.h"
37
38#include "ns3/core-module.h"
39#include "ns3/internet-stack-helper.h"
40#include "ns3/ipv4-address-helper.h"
41#include "ns3/ipv4-global-routing-helper.h"
42#include "ns3/mpi-interface.h"
43#include "ns3/network-module.h"
44#include "ns3/nix-vector-helper.h"
45#include "ns3/on-off-helper.h"
46#include "ns3/packet-sink-helper.h"
47#include "ns3/point-to-point-helper.h"
48
49#include <iomanip>
50#include <mpi.h>
51
52using namespace ns3;
53
54NS_LOG_COMPONENT_DEFINE("SimpleDistributed");
55
56int
57main(int argc, char* argv[])
58{
59 bool nix = true;
60 bool nullmsg = false;
61 bool tracing = false;
62 bool testing = false;
63 bool verbose = false;
64
65 // Parse command line
66 CommandLine cmd(__FILE__);
67 cmd.AddValue("nix", "Enable the use of nix-vector or global routing", nix);
68 cmd.AddValue("nullmsg", "Enable the use of null-message synchronization", nullmsg);
69 cmd.AddValue("tracing", "Enable pcap tracing", tracing);
70 cmd.AddValue("verbose", "verbose output", verbose);
71 cmd.AddValue("test", "Enable regression test output", testing);
72 cmd.Parse(argc, argv);
73
74 // Distributed simulation setup; by default use granted time window algorithm.
75 if (nullmsg)
76 {
77 GlobalValue::Bind("SimulatorImplementationType",
78 StringValue("ns3::NullMessageSimulatorImpl"));
79 }
80 else
81 {
82 GlobalValue::Bind("SimulatorImplementationType",
83 StringValue("ns3::DistributedSimulatorImpl"));
84 }
85
86 // Enable parallel simulator with the command line arguments
87 MpiInterface::Enable(&argc, &argv);
88
90
91 if (verbose)
92 {
93 LogComponentEnable("PacketSink",
95 }
96
98 uint32_t systemCount = MpiInterface::GetSize();
99
100 // Check for valid distributed parameters.
101 // Must have 2 and only 2 Logical Processors (LPs)
102 if (systemCount != 2)
103 {
104 std::cout << "This simulation requires 2 and only 2 logical processors." << std::endl;
105 return 1;
106 }
107
108 // Some default values
109 Config::SetDefault("ns3::OnOffApplication::PacketSize", UintegerValue(512));
110 Config::SetDefault("ns3::OnOffApplication::DataRate", StringValue("1Mbps"));
111 Config::SetDefault("ns3::OnOffApplication::MaxBytes", UintegerValue(512));
112
113 // Create leaf nodes on left with system id 0
114 NodeContainer leftLeafNodes;
115 leftLeafNodes.Create(4, 0);
116
117 // Create router nodes. Left router
118 // with system id 0, right router with
119 // system id 1
120 NodeContainer routerNodes;
121 Ptr<Node> routerNode1 = CreateObject<Node>(0);
122 Ptr<Node> routerNode2 = CreateObject<Node>(1);
123 routerNodes.Add(routerNode1);
124 routerNodes.Add(routerNode2);
125
126 // Create leaf nodes on right with system id 1
127 NodeContainer rightLeafNodes;
128 rightLeafNodes.Create(4, 1);
129
130 PointToPointHelper routerLink;
131 routerLink.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
132 routerLink.SetChannelAttribute("Delay", StringValue("5ms"));
133
134 PointToPointHelper leafLink;
135 leafLink.SetDeviceAttribute("DataRate", StringValue("1Mbps"));
136 leafLink.SetChannelAttribute("Delay", StringValue("2ms"));
137
138 // Add link connecting routers
139 NetDeviceContainer routerDevices;
140 routerDevices = routerLink.Install(routerNodes);
141
142 // Add links for left side leaf nodes to left router
143 NetDeviceContainer leftRouterDevices;
144 NetDeviceContainer leftLeafDevices;
145 for (uint32_t i = 0; i < 4; ++i)
146 {
147 NetDeviceContainer temp = leafLink.Install(leftLeafNodes.Get(i), routerNodes.Get(0));
148 leftLeafDevices.Add(temp.Get(0));
149 leftRouterDevices.Add(temp.Get(1));
150 }
151
152 // Add links for right side leaf nodes to right router
153 NetDeviceContainer rightRouterDevices;
154 NetDeviceContainer rightLeafDevices;
155 for (uint32_t i = 0; i < 4; ++i)
156 {
157 NetDeviceContainer temp = leafLink.Install(rightLeafNodes.Get(i), routerNodes.Get(1));
158 rightLeafDevices.Add(temp.Get(0));
159 rightRouterDevices.Add(temp.Get(1));
160 }
161
163 if (nix)
164 {
166 stack.SetRoutingHelper(nixRouting); // has effect on the next Install ()
167 }
168
169 stack.InstallAll();
170
171 Ipv4InterfaceContainer routerInterfaces;
172 Ipv4InterfaceContainer leftLeafInterfaces;
173 Ipv4InterfaceContainer leftRouterInterfaces;
174 Ipv4InterfaceContainer rightLeafInterfaces;
175 Ipv4InterfaceContainer rightRouterInterfaces;
176
177 Ipv4AddressHelper leftAddress;
178 leftAddress.SetBase("10.1.1.0", "255.255.255.0");
179
180 Ipv4AddressHelper routerAddress;
181 routerAddress.SetBase("10.2.1.0", "255.255.255.0");
182
183 Ipv4AddressHelper rightAddress;
184 rightAddress.SetBase("10.3.1.0", "255.255.255.0");
185
186 // Router-to-Router interfaces
187 routerInterfaces = routerAddress.Assign(routerDevices);
188
189 // Left interfaces
190 for (uint32_t i = 0; i < 4; ++i)
191 {
193 ndc.Add(leftLeafDevices.Get(i));
194 ndc.Add(leftRouterDevices.Get(i));
195 Ipv4InterfaceContainer ifc = leftAddress.Assign(ndc);
196 leftLeafInterfaces.Add(ifc.Get(0));
197 leftRouterInterfaces.Add(ifc.Get(1));
198 leftAddress.NewNetwork();
199 }
200
201 // Right interfaces
202 for (uint32_t i = 0; i < 4; ++i)
203 {
205 ndc.Add(rightLeafDevices.Get(i));
206 ndc.Add(rightRouterDevices.Get(i));
207 Ipv4InterfaceContainer ifc = rightAddress.Assign(ndc);
208 rightLeafInterfaces.Add(ifc.Get(0));
209 rightRouterInterfaces.Add(ifc.Get(1));
210 rightAddress.NewNetwork();
211 }
212
213 if (!nix)
214 {
216 }
217
218 if (tracing)
219 {
220 if (systemId == 0)
221 {
222 routerLink.EnablePcap("router-left", routerDevices, true);
223 leafLink.EnablePcap("leaf-left", leftLeafDevices, true);
224 }
225
226 if (systemId == 1)
227 {
228 routerLink.EnablePcap("router-right", routerDevices, true);
229 leafLink.EnablePcap("leaf-right", rightLeafDevices, true);
230 }
231 }
232
233 // Create a packet sink on the right leafs to receive packets from left leafs
234
235 uint16_t port = 50000;
236 if (systemId == 1)
237 {
239 PacketSinkHelper sinkHelper("ns3::UdpSocketFactory", sinkLocalAddress);
240 ApplicationContainer sinkApp;
241 for (uint32_t i = 0; i < 4; ++i)
242 {
243 sinkApp.Add(sinkHelper.Install(rightLeafNodes.Get(i)));
244 if (testing)
245 {
246 sinkApp.Get(i)->TraceConnectWithoutContext("RxWithAddresses",
248 }
249 }
250 sinkApp.Start(Seconds(1.0));
251 sinkApp.Stop(Seconds(5));
252 }
253
254 // Create the OnOff applications to send
255 if (systemId == 0)
256 {
257 OnOffHelper clientHelper("ns3::UdpSocketFactory", Address());
258 clientHelper.SetAttribute("OnTime", StringValue("ns3::ConstantRandomVariable[Constant=1]"));
259 clientHelper.SetAttribute("OffTime",
260 StringValue("ns3::ConstantRandomVariable[Constant=0]"));
261
263 for (uint32_t i = 0; i < 4; ++i)
264 {
266 clientHelper.SetAttribute("Remote", remoteAddress);
267 clientApps.Add(clientHelper.Install(leftLeafNodes.Get(i)));
268 }
269 clientApps.Start(Seconds(1.0));
270 clientApps.Stop(Seconds(5));
271 }
272
276
277 if (testing)
278 {
280 }
281
282 // Exit the MPI execution environment
284 return 0;
285}
a polymophic address class
Definition address.h:90
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
Ptr< Application > Get(uint32_t i) const
Get the Ptr<Application> stored in this container at a given index.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
void Add(ApplicationContainer other)
Append the contents of another ApplicationContainer to the end of this container.
Parse command-line arguments.
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...
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4Address NewNetwork()
Increment the network number and reset the IP address counter to the base value provided in the SetBa...
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
static Ipv4Address GetAny()
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.
std::pair< Ptr< Ipv4 >, uint32_t > Get(uint32_t i) const
Get the std::pair of an Ptr<Ipv4> and interface stored at the location specified by the index.
void Add(const Ipv4InterfaceContainer &other)
Concatenate the entries in the other container with ours.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
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
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
Helper class that adds Nix-vector routing to nodes.
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
void Add(const NodeContainer &nc)
Append the contents of another NodeContainer to the end of this container.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
void EnablePcap(std::string prefix, Ptr< NetDevice > nd, bool promiscuous=false, bool explicitFilename=false)
Enable pcap output the indicated net device.
Build a set of PointToPointNetDevice objects.
void SetDeviceAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each NetDevice created by the helper.
void SetChannelAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each Channel created by the helper.
NetDeviceContainer Install(NodeContainer c)
Smart pointer class similar to boost::intrusive_ptr.
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.
Hold variables of type string.
Definition string.h:45
Hold an unsigned integer type.
Definition uinteger.h:34
uint16_t port
Definition dsdv-manet.cc:33
void SetDefault(std::string name, const AttributeValue &value)
Definition config.cc:883
#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 Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Common methods for MPI examples.
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
bool verbose
bool tracing
Flag to enable/disable generation of tracing files.