A Discrete-Event Network Simulator
API
simple-distributed-empty-node.cc
Go to the documentation of this file.
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  * This test is equivalent to simple-distributed but tests boundary cases
18  * when one of the ranks has no Nodes on it. When run on two tasks
19  * rank 0 will have all the Nodes and rank 1 will be empty:
20  *
21  * ------- -------
22  * RANK 0 RANK 0
23  * ------- | -------
24  * |
25  * n0 ---------| | |---------- n6
26  * | | |
27  * n1 -------\ | | | /------- n7
28  * n4 ----------|---------- n5
29  * n2 -------/ | | | \------- n8
30  * | | |
31  * n3 ---------| | |---------- n9
32  *
33  *
34  * When run on three tasks rank 1 has the left half of the Nodes and rank 2
35  * will be empty.
36  *
37  * ------- -------
38  * RANK 0 RANK 1
39  * ------- | -------
40  * |
41  * n0 ---------| | |---------- n6
42  * | | |
43  * n1 -------\ | | | /------- n7
44  * n4 ----------|---------- n5
45  * n2 -------/ | | | \------- n8
46  * | | |
47  * n3 ---------| | |---------- n9
48  *
49  * OnOff clients are placed on each left leaf node. Each right leaf node
50  * is a packet sink for a left leaf node. As a packet travels from one
51  * logical processor to another (the link between n4 and n5), MPI messages
52  * are passed containing the serialized packet. The message is then
53  * deserialized into a new packet and sent on as normal.
54  *
55  * One packet is sent from each left leaf node. The packet sinks on the
56  * right leaf nodes output logging information when they receive the packet.
57  */
58 
59 #include "ns3/core-module.h"
60 #include "ns3/network-module.h"
61 #include "ns3/mpi-interface.h"
62 #include "ns3/ipv4-global-routing-helper.h"
63 #include "ns3/point-to-point-helper.h"
64 #include "ns3/internet-stack-helper.h"
65 #include "ns3/ipv4-nix-vector-helper.h"
66 #include "ns3/ipv4-address-helper.h"
67 #include "ns3/on-off-helper.h"
68 #include "ns3/packet-sink-helper.h"
69 
70 #ifdef NS3_MPI
71 #include <mpi.h>
72 #endif
73 
74 using namespace ns3;
75 
76 NS_LOG_COMPONENT_DEFINE ("SimpleDistributed");
77 
78 int
79 main (int argc, char *argv[])
80 {
81 #ifdef NS3_MPI
82 
83  bool nix = true;
84  bool nullmsg = false;
85  bool tracing = false;
86 
87  // Parse command line
89  cmd.AddValue ("nix", "Enable the use of nix-vector or global routing", nix);
90  cmd.AddValue ("nullmsg", "Enable the use of null-message synchronization", nullmsg);
91  cmd.AddValue ("tracing", "Enable pcap tracing", tracing);
92  cmd.Parse (argc, argv);
93 
94  // Distributed simulation setup; by default use granted time window algorithm.
95  if(nullmsg)
96  {
97  GlobalValue::Bind ("SimulatorImplementationType",
98  StringValue ("ns3::NullMessageSimulatorImpl"));
99  }
100  else
101  {
102  GlobalValue::Bind ("SimulatorImplementationType",
103  StringValue ("ns3::DistributedSimulatorImpl"));
104  }
105 
106  MpiInterface::Enable (&argc, &argv);
107 
108  LogComponentEnable ("PacketSink", LOG_LEVEL_INFO);
109 
110  uint32_t systemId = MpiInterface::GetSystemId ();
111  uint32_t systemCount = MpiInterface::GetSize ();
112 
113  uint32_t rightHalfSystemId = 777;
114 
115  // Check for valid distributed parameters.
116  // Must have 2 or 3 tasks.
117  if (systemCount == 2)
118  {
119  rightHalfSystemId = 0;
120  }
121  else if (systemCount == 3)
122  {
123  rightHalfSystemId = 1;
124  }
125  else
126  {
127  std::cout << "This simulation requires 2 or 3 logical processors." << std::endl;
128  return 1;
129  }
130 
131  // Some default values
132  Config::SetDefault ("ns3::OnOffApplication::PacketSize", UintegerValue (512));
133  Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue ("1Mbps"));
134  Config::SetDefault ("ns3::OnOffApplication::MaxBytes", UintegerValue (512));
135 
136  // Create leaf nodes on left with system id 0
137  NodeContainer leftLeafNodes;
138  leftLeafNodes.Create (4, 0);
139 
140  // Create router nodes. Left router with system id 0, right router
141  // with system id dependent on number of processors using
142  // rightHalfSystemId
143  NodeContainer routerNodes;
144  Ptr<Node> routerNode1 = CreateObject<Node> (0);
145  Ptr<Node> routerNode2 = CreateObject<Node> (rightHalfSystemId);
146  routerNodes.Add (routerNode1);
147  routerNodes.Add (routerNode2);
148 
149  // Create leaf nodes on left with system id rightHalfSystemId
150  NodeContainer rightLeafNodes;
151  rightLeafNodes.Create (4, rightHalfSystemId);
152 
153  PointToPointHelper routerLink;
154  routerLink.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
155  routerLink.SetChannelAttribute ("Delay", StringValue ("5ms"));
156 
157  PointToPointHelper leafLink;
158  leafLink.SetDeviceAttribute ("DataRate", StringValue ("1Mbps"));
159  leafLink.SetChannelAttribute ("Delay", StringValue ("2ms"));
160 
161  // Add link connecting routers
162  NetDeviceContainer routerDevices;
163  routerDevices = routerLink.Install (routerNodes);
164 
165  // Add links for left side leaf nodes to left router
166  NetDeviceContainer leftRouterDevices;
167  NetDeviceContainer leftLeafDevices;
168  for (uint32_t i = 0; i < 4; ++i)
169  {
170  NetDeviceContainer temp = leafLink.Install (leftLeafNodes.Get (i), routerNodes.Get (0));
171  leftLeafDevices.Add (temp.Get (0));
172  leftRouterDevices.Add (temp.Get (1));
173  }
174 
175  // Add links for right side leaf nodes to right router
176  NetDeviceContainer rightRouterDevices;
177  NetDeviceContainer rightLeafDevices;
178  for (uint32_t i = 0; i < 4; ++i)
179  {
180  NetDeviceContainer temp = leafLink.Install (rightLeafNodes.Get (i), routerNodes.Get (1));
181  rightLeafDevices.Add (temp.Get (0));
182  rightRouterDevices.Add (temp.Get (1));
183  }
184 
186  if (nix)
187  {
188  Ipv4NixVectorHelper nixRouting;
189  stack.SetRoutingHelper (nixRouting); // has effect on the next Install ()
190  }
191 
192  stack.InstallAll ();
193 
194  Ipv4InterfaceContainer routerInterfaces;
195  Ipv4InterfaceContainer leftLeafInterfaces;
196  Ipv4InterfaceContainer leftRouterInterfaces;
197  Ipv4InterfaceContainer rightLeafInterfaces;
198  Ipv4InterfaceContainer rightRouterInterfaces;
199 
200  Ipv4AddressHelper leftAddress;
201  leftAddress.SetBase ("10.1.1.0", "255.255.255.0");
202 
203  Ipv4AddressHelper routerAddress;
204  routerAddress.SetBase ("10.2.1.0", "255.255.255.0");
205 
206  Ipv4AddressHelper rightAddress;
207  rightAddress.SetBase ("10.3.1.0", "255.255.255.0");
208 
209  // Router-to-Router interfaces
210  routerInterfaces = routerAddress.Assign (routerDevices);
211 
212  // Left interfaces
213  for (uint32_t i = 0; i < 4; ++i)
214  {
215  NetDeviceContainer ndc;
216  ndc.Add (leftLeafDevices.Get (i));
217  ndc.Add (leftRouterDevices.Get (i));
218  Ipv4InterfaceContainer ifc = leftAddress.Assign (ndc);
219  leftLeafInterfaces.Add (ifc.Get (0));
220  leftRouterInterfaces.Add (ifc.Get (1));
221  leftAddress.NewNetwork ();
222  }
223 
224  // Right interfaces
225  for (uint32_t i = 0; i < 4; ++i)
226  {
227  NetDeviceContainer ndc;
228  ndc.Add (rightLeafDevices.Get (i));
229  ndc.Add (rightRouterDevices.Get (i));
230  Ipv4InterfaceContainer ifc = rightAddress.Assign (ndc);
231  rightLeafInterfaces.Add (ifc.Get (0));
232  rightRouterInterfaces.Add (ifc.Get (1));
233  rightAddress.NewNetwork ();
234  }
235 
236  if (!nix)
237  {
239  }
240 
241  if (tracing == true)
242  {
243  if (systemId == 0)
244  {
245  routerLink.EnablePcap("router-left", routerDevices, true);
246  leafLink.EnablePcap("leaf-left", leftLeafDevices, true);
247  }
248 
249  if (systemId == rightHalfSystemId)
250  {
251  routerLink.EnablePcap("router-right", routerDevices, true);
252  leafLink.EnablePcap("leaf-right", rightLeafDevices, true);
253  }
254  }
255 
256  // Create a packet sink on the right leafs to receive packets from left leafs
257  uint16_t port = 50000;
258  if (systemId == rightHalfSystemId)
259  {
260  Address sinkLocalAddress (InetSocketAddress (Ipv4Address::GetAny (), port));
261  PacketSinkHelper sinkHelper ("ns3::UdpSocketFactory", sinkLocalAddress);
262  ApplicationContainer sinkApp;
263  for (uint32_t i = 0; i < 4; ++i)
264  {
265  sinkApp.Add (sinkHelper.Install (rightLeafNodes.Get (i)));
266  }
267  sinkApp.Start (Seconds (1.0));
268  sinkApp.Stop (Seconds (5));
269  }
270 
271  // Create the OnOff applications to send
272  if (systemId == 0)
273  {
274  OnOffHelper clientHelper ("ns3::UdpSocketFactory", Address ());
275  clientHelper.SetAttribute
276  ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
277  clientHelper.SetAttribute
278  ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
279 
281  for (uint32_t i = 0; i < 4; ++i)
282  {
283  AddressValue remoteAddress
284  (InetSocketAddress (rightLeafInterfaces.GetAddress (i), port));
285  clientHelper.SetAttribute ("Remote", remoteAddress);
286  clientApps.Add (clientHelper.Install (leftLeafNodes.Get (i)));
287  }
288  clientApps.Start (Seconds (1.0));
289  clientApps.Stop (Seconds (5));
290  }
291 
292  Simulator::Stop (Seconds (5));
293  Simulator::Run ();
295  // Exit the MPI execution environment
297  return 0;
298 #else
299  NS_FATAL_ERROR ("Can't use distributed simulator without MPI compiled in");
300 #endif
301 }
holds a vector of ns3::Application pointers.
an Inet address class
static Ipv4Address GetAny(void)
std::pair< Ptr< Ipv4 >, uint32_t > Get(uint32_t i) const
Get the std::pair of an Ptr and interface stored at the location specified by the index...
holds a vector of std::pair of Ptr and interface index.
static void PopulateRoutingTables(void)
Build a routing database and initialize the routing tables of the nodes in the simulation.
Hold variables of type string.
Definition: string.h:41
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
NetDeviceContainer Install(NodeContainer c)
void Add(ApplicationContainer other)
Append the contents of another ApplicationContainer to the end of this container. ...
void Add(const Ipv4InterfaceContainer &other)
Concatenate the entries in the other container with ours.
static void Run(void)
Run the simulation.
Definition: simulator.cc:201
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
Helper class that adds Nix-vector routing to nodes.
aggregate IP/TCP/UDP functionality to existing Nodes.
LOG_INFO and above.
Definition: log.h:103
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:162
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
static void Disable()
Terminates the parallel environment.
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.
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:42
tuple cmd
Definition: second.py:35
uint16_t port
Definition: dsdv-manet.cc:44
a polymophic address class
Definition: address.h:90
tuple clientApps
Definition: first.py:54
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:351
void InstallAll(void) const
Aggregate IPv4, IPv6, UDP, and TCP stacks to all nodes in the simulation.
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
static void Enable(int *pargc, char ***pargv)
Sets up parallel communication interface.
Hold an unsigned integer type.
Definition: uinteger.h:44
holds a vector of ns3::NetDevice pointers
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...
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
Parse command-line arguments.
Definition: command-line.h:205
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:165
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
tuple stack
Definition: first.py:34
void SetChannelAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each Channel created by the helper.
AttributeValue implementation for Address.
Definition: address.h:278
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...
void Add(NodeContainer other)
Append the contents of another NodeContainer to the end of this container.
static uint32_t GetSystemId()
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:495
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:209
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:774
Ipv4Address NewNetwork(void)
Increment the network number and reset the IP address counter to the base value provided in the SetBa...
void Parse(int argc, char *argv[])
Parse the program arguments.
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 EnablePcap(std::string prefix, Ptr< NetDevice > nd, bool promiscuous=false, bool explicitFilename=false)
Enable pcap output the indicated net device.
static uint32_t GetSize()
void SetRoutingHelper(const Ipv4RoutingHelper &routing)
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const