A Discrete-Event Network Simulator
API
simple-distributed.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 
48 #include "mpi-test-fixtures.h"
49 
50 #include "ns3/core-module.h"
51 #include "ns3/network-module.h"
52 #include "ns3/mpi-interface.h"
53 #include "ns3/ipv4-global-routing-helper.h"
54 #include "ns3/point-to-point-helper.h"
55 #include "ns3/internet-stack-helper.h"
56 #include "ns3/nix-vector-helper.h"
57 #include "ns3/ipv4-address-helper.h"
58 #include "ns3/on-off-helper.h"
59 #include "ns3/packet-sink-helper.h"
60 #include <mpi.h>
61 
62 #include <iomanip>
63 
64 using namespace ns3;
65 
66 NS_LOG_COMPONENT_DEFINE ("SimpleDistributed");
67 
68 int
69 main (int argc, char *argv[])
70 {
71  bool nix = true;
72  bool nullmsg = false;
73  bool tracing = false;
74  bool testing = false;
75  bool verbose = false;
76 
77  // Parse command line
78  CommandLine cmd (__FILE__);
79  cmd.AddValue ("nix", "Enable the use of nix-vector or global routing", nix);
80  cmd.AddValue ("nullmsg", "Enable the use of null-message synchronization", nullmsg);
81  cmd.AddValue ("tracing", "Enable pcap tracing", tracing);
82  cmd.AddValue ("verbose", "verbose output", verbose);
83  cmd.AddValue ("test", "Enable regression test output", testing);
84  cmd.Parse (argc, argv);
85 
86  // Distributed simulation setup; by default use granted time window algorithm.
87  if(nullmsg)
88  {
89  GlobalValue::Bind ("SimulatorImplementationType",
90  StringValue ("ns3::NullMessageSimulatorImpl"));
91  }
92  else
93  {
94  GlobalValue::Bind ("SimulatorImplementationType",
95  StringValue ("ns3::DistributedSimulatorImpl"));
96  }
97 
98  // Enable parallel simulator with the command line arguments
99  MpiInterface::Enable (&argc, &argv);
100 
101  SinkTracer::Init ();
102 
103  if (verbose)
104  {
106  }
107 
108  uint32_t systemId = MpiInterface::GetSystemId ();
109  uint32_t systemCount = MpiInterface::GetSize ();
110 
111  // Check for valid distributed parameters.
112  // Must have 2 and only 2 Logical Processors (LPs)
113  if (systemCount != 2)
114  {
115  std::cout << "This simulation requires 2 and only 2 logical processors." << std::endl;
116  return 1;
117  }
118 
119  // Some default values
120  Config::SetDefault ("ns3::OnOffApplication::PacketSize", UintegerValue (512));
121  Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue ("1Mbps"));
122  Config::SetDefault ("ns3::OnOffApplication::MaxBytes", UintegerValue (512));
123 
124  // Create leaf nodes on left with system id 0
125  NodeContainer leftLeafNodes;
126  leftLeafNodes.Create (4, 0);
127 
128  // Create router nodes. Left router
129  // with system id 0, right router with
130  // system id 1
131  NodeContainer routerNodes;
132  Ptr<Node> routerNode1 = CreateObject<Node> (0);
133  Ptr<Node> routerNode2 = CreateObject<Node> (1);
134  routerNodes.Add (routerNode1);
135  routerNodes.Add (routerNode2);
136 
137  // Create leaf nodes on right with system id 1
138  NodeContainer rightLeafNodes;
139  rightLeafNodes.Create (4, 1);
140 
141  PointToPointHelper routerLink;
142  routerLink.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
143  routerLink.SetChannelAttribute ("Delay", StringValue ("5ms"));
144 
145  PointToPointHelper leafLink;
146  leafLink.SetDeviceAttribute ("DataRate", StringValue ("1Mbps"));
147  leafLink.SetChannelAttribute ("Delay", StringValue ("2ms"));
148 
149  // Add link connecting routers
150  NetDeviceContainer routerDevices;
151  routerDevices = routerLink.Install (routerNodes);
152 
153  // Add links for left side leaf nodes to left router
154  NetDeviceContainer leftRouterDevices;
155  NetDeviceContainer leftLeafDevices;
156  for (uint32_t i = 0; i < 4; ++i)
157  {
158  NetDeviceContainer temp = leafLink.Install (leftLeafNodes.Get (i), routerNodes.Get (0));
159  leftLeafDevices.Add (temp.Get (0));
160  leftRouterDevices.Add (temp.Get (1));
161  }
162 
163  // Add links for right side leaf nodes to right router
164  NetDeviceContainer rightRouterDevices;
165  NetDeviceContainer rightLeafDevices;
166  for (uint32_t i = 0; i < 4; ++i)
167  {
168  NetDeviceContainer temp = leafLink.Install (rightLeafNodes.Get (i), routerNodes.Get (1));
169  rightLeafDevices.Add (temp.Get (0));
170  rightRouterDevices.Add (temp.Get (1));
171  }
172 
174  if (nix)
175  {
176  Ipv4NixVectorHelper nixRouting;
177  stack.SetRoutingHelper (nixRouting); // has effect on the next Install ()
178  }
179 
180  stack.InstallAll ();
181 
182  Ipv4InterfaceContainer routerInterfaces;
183  Ipv4InterfaceContainer leftLeafInterfaces;
184  Ipv4InterfaceContainer leftRouterInterfaces;
185  Ipv4InterfaceContainer rightLeafInterfaces;
186  Ipv4InterfaceContainer rightRouterInterfaces;
187 
188  Ipv4AddressHelper leftAddress;
189  leftAddress.SetBase ("10.1.1.0", "255.255.255.0");
190 
191  Ipv4AddressHelper routerAddress;
192  routerAddress.SetBase ("10.2.1.0", "255.255.255.0");
193 
194  Ipv4AddressHelper rightAddress;
195  rightAddress.SetBase ("10.3.1.0", "255.255.255.0");
196 
197  // Router-to-Router interfaces
198  routerInterfaces = routerAddress.Assign (routerDevices);
199 
200  // Left interfaces
201  for (uint32_t i = 0; i < 4; ++i)
202  {
203  NetDeviceContainer ndc;
204  ndc.Add (leftLeafDevices.Get (i));
205  ndc.Add (leftRouterDevices.Get (i));
206  Ipv4InterfaceContainer ifc = leftAddress.Assign (ndc);
207  leftLeafInterfaces.Add (ifc.Get (0));
208  leftRouterInterfaces.Add (ifc.Get (1));
209  leftAddress.NewNetwork ();
210  }
211 
212  // Right interfaces
213  for (uint32_t i = 0; i < 4; ++i)
214  {
215  NetDeviceContainer ndc;
216  ndc.Add (rightLeafDevices.Get (i));
217  ndc.Add (rightRouterDevices.Get (i));
218  Ipv4InterfaceContainer ifc = rightAddress.Assign (ndc);
219  rightLeafInterfaces.Add (ifc.Get (0));
220  rightRouterInterfaces.Add (ifc.Get (1));
221  rightAddress.NewNetwork ();
222  }
223 
224  if (!nix)
225  {
227  }
228 
229  if (tracing == true)
230  {
231  if (systemId == 0)
232  {
233  routerLink.EnablePcap("router-left", routerDevices, true);
234  leafLink.EnablePcap("leaf-left", leftLeafDevices, true);
235  }
236 
237  if (systemId == 1)
238  {
239  routerLink.EnablePcap("router-right", routerDevices, true);
240  leafLink.EnablePcap("leaf-right", rightLeafDevices, true);
241  }
242  }
243 
244  // Create a packet sink on the right leafs to receive packets from left leafs
245 
246  uint16_t port = 50000;
247  if (systemId == 1)
248  {
249  Address sinkLocalAddress (InetSocketAddress (Ipv4Address::GetAny (), port));
250  PacketSinkHelper sinkHelper ("ns3::UdpSocketFactory", sinkLocalAddress);
251  ApplicationContainer sinkApp;
252  for (uint32_t i = 0; i < 4; ++i)
253  {
254  sinkApp.Add (sinkHelper.Install (rightLeafNodes.Get (i)));
255  if (testing)
256  {
257  sinkApp.Get (i)->TraceConnectWithoutContext ("RxWithAddresses", MakeCallback (&SinkTracer::SinkTrace));
258  }
259  }
260  sinkApp.Start (Seconds (1.0));
261  sinkApp.Stop (Seconds (5));
262  }
263 
264  // Create the OnOff applications to send
265  if (systemId == 0)
266  {
267  OnOffHelper clientHelper ("ns3::UdpSocketFactory", Address ());
268  clientHelper.SetAttribute
269  ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
270  clientHelper.SetAttribute
271  ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
272 
274  for (uint32_t i = 0; i < 4; ++i)
275  {
276  AddressValue remoteAddress
277  (InetSocketAddress (rightLeafInterfaces.GetAddress (i), port));
278  clientHelper.SetAttribute ("Remote", remoteAddress);
279  clientApps.Add (clientHelper.Install (leftLeafNodes.Get (i)));
280  }
281  clientApps.Start (Seconds (1.0));
282  clientApps.Stop (Seconds (5));
283  }
284 
285  Simulator::Stop (Seconds (5));
286  Simulator::Run ();
288 
289  if (testing)
290  {
291  SinkTracer::Verify (4);
292  }
293 
294  // Exit the MPI execution environment
296  return 0;
297 }
ns3::NetDeviceContainer
holds a vector of ns3::NetDevice pointers
Definition: net-device-container.h:42
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
ns3::InetSocketAddress
an Inet address class
Definition: inet-socket-address.h:41
ns3::ApplicationContainer::Get
Ptr< Application > Get(uint32_t i) const
Get the Ptr<Application> stored in this container at a given index.
Definition: application-container.cc:62
ns3::CommandLine
Parse command-line arguments.
Definition: command-line.h:228
ns3::LOG_LEVEL_INFO
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition: log.h:107
ns3::PcapHelperForDevice::EnablePcap
void EnablePcap(std::string prefix, Ptr< NetDevice > nd, bool promiscuous=false, bool explicitFilename=false)
Enable pcap output the indicated net device.
Definition: trace-helper.cc:401
ns3::Ipv4AddressHelper::NewNetwork
Ipv4Address NewNetwork(void)
Increment the network number and reset the IP address counter to the base value provided in the SetBa...
Definition: ipv4-address-helper.cc:126
tracing
bool tracing
Flag to enable/disable generation of tracing files.
Definition: wifi-bianchi.cc:85
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::ApplicationContainer::Stop
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
Definition: application-container.cc:107
ns3::Ipv4AddressHelper
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Definition: ipv4-address-helper.h:48
ns3::PointToPointHelper::SetDeviceAttribute
void SetDeviceAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each NetDevice created by the helper.
Definition: point-to-point-helper.cc:69
ns3::PointToPointHelper::SetChannelAttribute
void SetChannelAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each Channel created by the helper.
Definition: point-to-point-helper.cc:75
ns3::LOG_PREFIX_NODE
@ LOG_PREFIX_NODE
Prefix all trace prints with simulation node.
Definition: log.h:120
ns3::AddressValue
AttributeValue implementation for Address.
Definition: address.h:278
ns3::MpiInterface::Disable
static void Disable()
Clean up the ns-3 parallel communications interface.
Definition: mpi-interface.cc:146
ns3::LogComponentEnable
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:361
ns3::PointToPointHelper::Install
NetDeviceContainer Install(NodeContainer c)
Definition: point-to-point-helper.cc:222
ns3::NodeContainer::Create
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Definition: node-container.cc:98
ns3::SinkTracer::Verify
static void Verify(unsigned long expectedCount)
Verify the sink trace count observed matches the expected count.
Definition: mpi-test-fixtures.cc:58
ns3::ApplicationContainer::Add
void Add(ApplicationContainer other)
Append the contents of another ApplicationContainer to the end of this container.
Definition: application-container.cc:67
ns3::NodeContainer::Add
void Add(NodeContainer other)
Append the contents of another NodeContainer to the end of this container.
Definition: node-container.cc:114
ns3::Ipv4AddressHelper::SetBase
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Definition: ipv4-address-helper.cc:64
ns3::Ptr< Node >
ns3::NixVectorHelper
Helper class that adds Nix-vector routing to nodes.
Definition: nix-vector-helper.h:51
ns3::Ipv4GlobalRoutingHelper::PopulateRoutingTables
static void PopulateRoutingTables(void)
Build a routing database and initialize the routing tables of the nodes in the simulation.
Definition: ipv4-global-routing-helper.cc:61
first.stack
stack
Definition: first.py:41
ns3::Simulator::Stop
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:180
mpi-test-fixtures.h
Common methods for MPI examples.
ns3::Address
a polymophic address class
Definition: address.h:91
ns3::OnOffHelper
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:43
ns3::MpiInterface::Enable
static void Enable(int *pargc, char ***pargv)
Setup the parallel communication interface.
Definition: mpi-interface.cc:115
ns3::Ipv4InterfaceContainer
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Definition: ipv4-interface-container.h:55
ns3::NodeContainer::Get
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
Definition: node-container.cc:93
verbose
bool verbose
Definition: openflow-switch.cc:50
second.cmd
cmd
Definition: second.py:35
ns3::Ipv4InterfaceContainer::Get
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.
Definition: ipv4-interface-container.cc:90
ns3::MakeCallback
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1642
ns3::Simulator::Run
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
ns3::StringValue
Hold variables of type string.
Definition: string.h:41
ns3::GlobalValue::Bind
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...
Definition: global-value.cc:155
ns3::PacketSinkHelper
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
Definition: packet-sink-helper.h:36
ns3::SinkTracer::SinkTrace
static void SinkTrace(const ns3::Ptr< const ns3::Packet > packet, const ns3::Address &srcAddress, const ns3::Address &destAddress)
PacketSink receive trace callback.
Definition: mpi-test-fixtures.cc:50
ns3::SinkTracer::Init
static void Init(void)
PacketSink receive trace callback.
Definition: mpi-test-fixtures.cc:40
ns3::LOG_PREFIX_TIME
@ LOG_PREFIX_TIME
Prefix all trace prints with simulation time.
Definition: log.h:119
first.clientApps
clientApps
Definition: first.py:61
ns3::Ipv4Address::GetAny
static Ipv4Address GetAny(void)
Definition: ipv4-address.cc:395
ns3::ApplicationContainer::Start
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter.
Definition: application-container.cc:87
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1289
ns3::Ipv4InterfaceContainer::Add
void Add(const Ipv4InterfaceContainer &other)
Concatenate the entries in the other container with ours.
Definition: ipv4-interface-container.cc:32
ns3::Ipv4AddressHelper::Assign
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
Definition: ipv4-address-helper.cc:135
ns3::Simulator::Destroy
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
ns3::LogLevel
LogLevel
Logging severity classes and levels.
Definition: log.h:94
ns3::ApplicationContainer
holds a vector of ns3::Application pointers.
Definition: application-container.h:43
ns3::PointToPointHelper
Build a set of PointToPointNetDevice objects.
Definition: point-to-point-helper.h:45
ns3::NodeContainer
keep track of a set of node pointers.
Definition: node-container.h:39
ns3::Ipv4InterfaceContainer::GetAddress
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
Definition: ipv4-interface-container.cc:59
ns3::UintegerValue
Hold an unsigned integer type.
Definition: uinteger.h:44
ns3::Config::SetDefault
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:849
ns3::ObjectBase::TraceConnectWithoutContext
bool TraceConnectWithoutContext(std::string name, const CallbackBase &cb)
Connect a TraceSource to a Callback without a context.
Definition: object-base.cc:293
ns3::InternetStackHelper
aggregate IP/TCP/UDP functionality to existing Nodes.
Definition: internet-stack-helper.h:88
ns3::NetDeviceContainer::Get
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
Definition: net-device-container.cc:62
ns3::MpiInterface::GetSystemId
static uint32_t GetSystemId()
Get the id number of this rank.
Definition: mpi-interface.cc:50
ns3::NetDeviceContainer::Add
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
Definition: net-device-container.cc:67
ns3::MpiInterface::GetSize
static uint32_t GetSize()
Get the number of ranks used by ns-3.
Definition: mpi-interface.cc:59
port
uint16_t port
Definition: dsdv-manet.cc:45