A Discrete-Event Network Simulator
API
simple-distributed-empty-node.cc
Go to the documentation of this file.
1 /*
2  * Copyright 2013. Lawrence Livermore National Security, LLC.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Steven Smith <smith84@llnl.gov>
18  */
19 
66 #include "mpi-test-fixtures.h"
67 
68 #include "ns3/core-module.h"
69 #include "ns3/internet-stack-helper.h"
70 #include "ns3/ipv4-address-helper.h"
71 #include "ns3/ipv4-global-routing-helper.h"
72 #include "ns3/mpi-interface.h"
73 #include "ns3/network-module.h"
74 #include "ns3/nix-vector-helper.h"
75 #include "ns3/on-off-helper.h"
76 #include "ns3/packet-sink-helper.h"
77 #include "ns3/point-to-point-helper.h"
78 
79 #include <mpi.h>
80 
81 using namespace ns3;
82 
83 NS_LOG_COMPONENT_DEFINE("SimpleDistributedEmptyNode");
84 
85 int
86 main(int argc, char* argv[])
87 {
88  bool nix = true;
89  bool nullmsg = false;
90  bool tracing = false;
91  bool testing = false;
92  bool verbose = false;
93 
94  // Parse command line
95  CommandLine cmd(__FILE__);
96  cmd.AddValue("nix", "Enable the use of nix-vector or global routing", nix);
97  cmd.AddValue("nullmsg", "Enable the use of null-message synchronization", nullmsg);
98  cmd.AddValue("tracing", "Enable pcap tracing", tracing);
99  cmd.AddValue("verbose", "verbose output", verbose);
100  cmd.AddValue("test", "Enable regression test output", testing);
101  cmd.Parse(argc, argv);
102 
103  // Distributed simulation setup; by default use granted time window algorithm.
104  if (nullmsg)
105  {
106  GlobalValue::Bind("SimulatorImplementationType",
107  StringValue("ns3::NullMessageSimulatorImpl"));
108  }
109  else
110  {
111  GlobalValue::Bind("SimulatorImplementationType",
112  StringValue("ns3::DistributedSimulatorImpl"));
113  }
114 
115  MpiInterface::Enable(&argc, &argv);
116 
118 
119  if (verbose)
120  {
121  LogComponentEnable("PacketSink",
123  }
124 
125  uint32_t systemId = MpiInterface::GetSystemId();
126  uint32_t systemCount = MpiInterface::GetSize();
127 
128  uint32_t rightHalfSystemId = 777;
129 
130  // Check for valid distributed parameters.
131  // Must have 2 or 3 tasks.
132  if (systemCount == 2)
133  {
134  rightHalfSystemId = 0;
135  }
136  else if (systemCount == 3)
137  {
138  rightHalfSystemId = 1;
139  }
140  else
141  {
142  std::cout << "This simulation requires 2 or 3 logical processors." << std::endl;
143  return 1;
144  }
145 
146  // Some default values
147  Config::SetDefault("ns3::OnOffApplication::PacketSize", UintegerValue(512));
148  Config::SetDefault("ns3::OnOffApplication::DataRate", StringValue("1Mbps"));
149  Config::SetDefault("ns3::OnOffApplication::MaxBytes", UintegerValue(512));
150 
151  // Create leaf nodes on left with system id 0
152  NodeContainer leftLeafNodes;
153  leftLeafNodes.Create(4, 0);
154 
155  // Create router nodes. Left router with system id 0, right router
156  // with system id dependent on number of processors using
157  // rightHalfSystemId
158  NodeContainer routerNodes;
159  Ptr<Node> routerNode1 = CreateObject<Node>(0);
160  Ptr<Node> routerNode2 = CreateObject<Node>(rightHalfSystemId);
161  routerNodes.Add(routerNode1);
162  routerNodes.Add(routerNode2);
163 
164  // Create leaf nodes on left with system id rightHalfSystemId
165  NodeContainer rightLeafNodes;
166  rightLeafNodes.Create(4, rightHalfSystemId);
167 
168  PointToPointHelper routerLink;
169  routerLink.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
170  routerLink.SetChannelAttribute("Delay", StringValue("5ms"));
171 
172  PointToPointHelper leafLink;
173  leafLink.SetDeviceAttribute("DataRate", StringValue("1Mbps"));
174  leafLink.SetChannelAttribute("Delay", StringValue("2ms"));
175 
176  // Add link connecting routers
177  NetDeviceContainer routerDevices;
178  routerDevices = routerLink.Install(routerNodes);
179 
180  // Add links for left side leaf nodes to left router
181  NetDeviceContainer leftRouterDevices;
182  NetDeviceContainer leftLeafDevices;
183  for (uint32_t i = 0; i < 4; ++i)
184  {
185  NetDeviceContainer temp = leafLink.Install(leftLeafNodes.Get(i), routerNodes.Get(0));
186  leftLeafDevices.Add(temp.Get(0));
187  leftRouterDevices.Add(temp.Get(1));
188  }
189 
190  // Add links for right side leaf nodes to right router
191  NetDeviceContainer rightRouterDevices;
192  NetDeviceContainer rightLeafDevices;
193  for (uint32_t i = 0; i < 4; ++i)
194  {
195  NetDeviceContainer temp = leafLink.Install(rightLeafNodes.Get(i), routerNodes.Get(1));
196  rightLeafDevices.Add(temp.Get(0));
197  rightRouterDevices.Add(temp.Get(1));
198  }
199 
201  if (nix)
202  {
203  Ipv4NixVectorHelper nixRouting;
204  stack.SetRoutingHelper(nixRouting); // has effect on the next Install ()
205  }
206 
207  stack.InstallAll();
208 
209  Ipv4InterfaceContainer routerInterfaces;
210  Ipv4InterfaceContainer leftLeafInterfaces;
211  Ipv4InterfaceContainer leftRouterInterfaces;
212  Ipv4InterfaceContainer rightLeafInterfaces;
213  Ipv4InterfaceContainer rightRouterInterfaces;
214 
215  Ipv4AddressHelper leftAddress;
216  leftAddress.SetBase("10.1.1.0", "255.255.255.0");
217 
218  Ipv4AddressHelper routerAddress;
219  routerAddress.SetBase("10.2.1.0", "255.255.255.0");
220 
221  Ipv4AddressHelper rightAddress;
222  rightAddress.SetBase("10.3.1.0", "255.255.255.0");
223 
224  // Router-to-Router interfaces
225  routerInterfaces = routerAddress.Assign(routerDevices);
226 
227  // Left interfaces
228  for (uint32_t i = 0; i < 4; ++i)
229  {
230  NetDeviceContainer ndc;
231  ndc.Add(leftLeafDevices.Get(i));
232  ndc.Add(leftRouterDevices.Get(i));
233  Ipv4InterfaceContainer ifc = leftAddress.Assign(ndc);
234  leftLeafInterfaces.Add(ifc.Get(0));
235  leftRouterInterfaces.Add(ifc.Get(1));
236  leftAddress.NewNetwork();
237  }
238 
239  // Right interfaces
240  for (uint32_t i = 0; i < 4; ++i)
241  {
242  NetDeviceContainer ndc;
243  ndc.Add(rightLeafDevices.Get(i));
244  ndc.Add(rightRouterDevices.Get(i));
245  Ipv4InterfaceContainer ifc = rightAddress.Assign(ndc);
246  rightLeafInterfaces.Add(ifc.Get(0));
247  rightRouterInterfaces.Add(ifc.Get(1));
248  rightAddress.NewNetwork();
249  }
250 
251  if (!nix)
252  {
254  }
255 
256  if (tracing == true)
257  {
258  if (systemId == 0)
259  {
260  routerLink.EnablePcap("router-left", routerDevices, true);
261  leafLink.EnablePcap("leaf-left", leftLeafDevices, true);
262  }
263 
264  if (systemId == rightHalfSystemId)
265  {
266  routerLink.EnablePcap("router-right", routerDevices, true);
267  leafLink.EnablePcap("leaf-right", rightLeafDevices, true);
268  }
269  }
270 
271  // Create a packet sink on the right leafs to receive packets from left leafs
272  uint16_t port = 50000;
273  if (systemId == rightHalfSystemId)
274  {
275  Address sinkLocalAddress(InetSocketAddress(Ipv4Address::GetAny(), port));
276  PacketSinkHelper sinkHelper("ns3::UdpSocketFactory", sinkLocalAddress);
277  ApplicationContainer sinkApp;
278  for (uint32_t i = 0; i < 4; ++i)
279  {
280  sinkApp.Add(sinkHelper.Install(rightLeafNodes.Get(i)));
281  if (testing)
282  {
283  sinkApp.Get(i)->TraceConnectWithoutContext("RxWithAddresses",
285  }
286  }
287  sinkApp.Start(Seconds(1.0));
288  sinkApp.Stop(Seconds(5));
289  }
290 
291  // Create the OnOff applications to send
292  if (systemId == 0)
293  {
294  OnOffHelper clientHelper("ns3::UdpSocketFactory", Address());
295  clientHelper.SetAttribute("OnTime", StringValue("ns3::ConstantRandomVariable[Constant=1]"));
296  clientHelper.SetAttribute("OffTime",
297  StringValue("ns3::ConstantRandomVariable[Constant=0]"));
298 
300  for (uint32_t i = 0; i < 4; ++i)
301  {
302  AddressValue remoteAddress(InetSocketAddress(rightLeafInterfaces.GetAddress(i), port));
303  clientHelper.SetAttribute("Remote", remoteAddress);
304  clientApps.Add(clientHelper.Install(leftLeafNodes.Get(i)));
305  }
306  clientApps.Start(Seconds(1.0));
307  clientApps.Stop(Seconds(5));
308  }
309 
311  Simulator::Run();
313 
314  if (testing)
315  {
317  }
318 
319  // Exit the MPI execution environment
321  return 0;
322 }
a polymophic address class
Definition: address.h:100
AttributeValue implementation for Address.
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.
Definition: command-line.h:232
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.
bool TraceConnectWithoutContext(std::string name, const CallbackBase &cb)
Connect a TraceSource to a Callback without a context.
Definition: object-base.cc:311
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:44
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)
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:140
static void Run()
Run the simulation.
Definition: simulator.cc:176
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:184
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:56
Hold an unsigned integer type.
Definition: uinteger.h:45
uint16_t port
Definition: dsdv-manet.cc:45
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:891
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1336
Common methods for MPI examples.
clientApps
Definition: first.py:58
stack
Definition: first.py:37
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:305
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:707
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
cmd
Definition: second.py:33
bool verbose
bool tracing
Flag to enable/disable generation of tracing files.