A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
csma-star.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 
18 #include "ns3/core-module.h"
19 #include "ns3/network-module.h"
20 #include "ns3/csma-module.h"
21 #include "ns3/csma-star-helper.h"
22 #include "ns3/applications-module.h"
23 #include "ns3/csma-module.h"
24 #include "ns3/internet-module.h"
25 #include "ns3/ipv6-address-generator.h"
26 
27 // Network topology (default)
28 //
29 // n2 + + n3 .
30 // | ... |\ /| ... | .
31 // ======= \ / ======= .
32 // CSMA \ / CSMA .
33 // \ / .
34 // n1 +--- n0 ---+ n4 .
35 // | ... | / \ | ... | .
36 // ======= / \ ======= .
37 // CSMA / \ CSMA .
38 // / \ .
39 // n6 + + n5 .
40 // | ... | | ... | .
41 // ======= ======= .
42 // CSMA CSMA .
43 //
44 
45 using namespace ns3;
46 
47 NS_LOG_COMPONENT_DEFINE ("CsmaStar");
48 
49 int
50 main (int argc, char *argv[])
51 {
52 
53  //
54  // Set up some default values for the simulation.
55  //
56  Config::SetDefault ("ns3::OnOffApplication::PacketSize", UintegerValue (137));
57 
58  // ??? try and stick 15kb/s into the data rate
59  Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue ("14kb/s"));
60 
61  //
62  // Default number of nodes in the star. Overridable by command line argument.
63  //
64  uint32_t nSpokes = 7;
65  uint32_t useIpv6 = 0;
66  Ipv6Address ipv6AddressBase = Ipv6Address("2001::");
67  Ipv6Prefix ipv6AddressPrefix = Ipv6Prefix(64);
68 
69  CommandLine cmd;
70  cmd.AddValue ("nSpokes", "Number of spoke nodes to place in the star", nSpokes);
71  cmd.AddValue ("useIpv6", "Use Ipv6", useIpv6);
72  cmd.Parse (argc, argv);
73 
74  NS_LOG_INFO ("Build star topology.");
75  CsmaHelper csma;
76  csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));
77  csma.SetChannelAttribute ("Delay", StringValue ("1ms"));
78  CsmaStarHelper star (nSpokes, csma);
79 
80  NodeContainer fillNodes;
81 
82  //
83  // Just to be nasy, hang some more nodes off of the CSMA channel for each
84  // spoke, so that there are a total of 16 nodes on each channel. Stash
85  // all of these new devices into a container.
86  //
87  NetDeviceContainer fillDevices;
88 
89  uint32_t nFill = 14;
90  for (uint32_t i = 0; i < star.GetSpokeDevices ().GetN (); ++i)
91  {
92  Ptr<Channel> channel = star.GetSpokeDevices ().Get (i)->GetChannel ();
93  Ptr<CsmaChannel> csmaChannel = channel->GetObject<CsmaChannel> ();
94  NodeContainer newNodes;
95  newNodes.Create (nFill);
96  fillNodes.Add (newNodes);
97  fillDevices.Add (csma.Install (newNodes, csmaChannel));
98  }
99 
100  NS_LOG_INFO ("Install internet stack on all nodes.");
101  InternetStackHelper internet;
102  star.InstallStack (internet);
103  internet.Install (fillNodes);
104 
105  NS_LOG_INFO ("Assign IP Addresses.");
106  if (useIpv6 == 0)
107  {
108  star.AssignIpv4Addresses (Ipv4AddressHelper ("10.1.0.0", "255.255.255.0"));
109  }
110  else
111  {
112  star.AssignIpv6Addresses (ipv6AddressBase, ipv6AddressPrefix);
113  }
114 
115  //
116  // We assigned addresses to the logical hub and the first "drop" of the
117  // CSMA network that acts as the spoke, but we also have a number of fill
118  // devices (nFill) also hanging off the CSMA network. We have got to
119  // assign addresses to them as well. We put all of the fill devices into
120  // a single device container, so the first nFill devices are associated
121  // with the channel connected to spokeDevices.Get (0), the second nFill
122  // devices afe associated with the channel connected to spokeDevices.Get (1)
123  // etc.
124  //
126  Ipv6AddressHelper address6;
127  for(uint32_t i = 0; i < star.SpokeCount (); ++i)
128  {
129  if (useIpv6 == 0)
130  {
131  std::ostringstream subnet;
132  subnet << "10.1." << i << ".0";
133  NS_LOG_INFO ("Assign IP Addresses for CSMA subnet " << subnet.str ());
134  address.SetBase (subnet.str ().c_str (), "255.255.255.0", "0.0.0.3");
135 
136  for (uint32_t j = 0; j < nFill; ++j)
137  {
138  address.Assign (fillDevices.Get (i * nFill + j));
139  }
140  }
141  else
142  {
143  Ipv6AddressGenerator::Init (ipv6AddressBase, ipv6AddressPrefix);
144  Ipv6Address v6network = Ipv6AddressGenerator::GetNetwork (ipv6AddressPrefix);
145  address6.SetBase (v6network, ipv6AddressPrefix);
146 
147  for (uint32_t j = 0; j < nFill; ++j)
148  {
149  address6.Assign(fillDevices.Get (i * nFill + j));
150  }
151  }
152  }
153 
154  NS_LOG_INFO ("Create applications.");
155  //
156  // Create a packet sink on the star "hub" to receive packets.
157  //
158  uint16_t port = 50000;
159 
160  if (useIpv6 == 0)
161  {
162  Address hubLocalAddress (InetSocketAddress (Ipv4Address::GetAny (), port));
163  PacketSinkHelper packetSinkHelper ("ns3::TcpSocketFactory", hubLocalAddress);
164  ApplicationContainer hubApp = packetSinkHelper.Install (star.GetHub ());
165  hubApp.Start (Seconds (1.0));
166  hubApp.Stop (Seconds (10.0));
167  }
168  else
169  {
170  Address hubLocalAddress6 (Inet6SocketAddress (Ipv6Address::GetAny (), port));
171  PacketSinkHelper packetSinkHelper6 ("ns3::TcpSocketFactory", hubLocalAddress6);
172  ApplicationContainer hubApp6 = packetSinkHelper6.Install (star.GetHub ());
173  hubApp6.Start (Seconds (1.0));
174  hubApp6.Stop (Seconds (10.0));
175  }
176 
177  //
178  // Create OnOff applications to send TCP to the hub, one on each spoke node.
179  //
180  OnOffHelper onOffHelper ("ns3::TcpSocketFactory", Address ());
181  onOffHelper.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
182  onOffHelper.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
183 
184  ApplicationContainer spokeApps;
185 
186  for (uint32_t i = 0; i < star.SpokeCount (); ++i)
187  {
188  if (useIpv6 == 0)
189  {
190  AddressValue remoteAddress (InetSocketAddress (star.GetHubIpv4Address (i), port));
191  onOffHelper.SetAttribute ("Remote", remoteAddress);
192  }
193  else
194  {
195  AddressValue remoteAddress (Inet6SocketAddress (star.GetHubIpv6Address (i), port));
196  onOffHelper.SetAttribute ("Remote", remoteAddress);
197  }
198  spokeApps.Add (onOffHelper.Install (star.GetSpokeNode (i)));
199  }
200 
201  spokeApps.Start (Seconds (1.0));
202  spokeApps.Stop (Seconds (10.0));
203 
204  //
205  // Because we are evil, we also add OnOff applications to send TCP to the hub
206  // from the fill devices on each CSMA link. The first nFill nodes in the
207  // fillNodes container are on the CSMA network talking to the zeroth device
208  // on the hub node. The next nFill nodes are on the CSMA network talking to
209  // the first device on the hub node, etc. So the ith fillNode is associated
210  // with the hub address found on the (i / nFill)th device on the hub node.
211  //
212  ApplicationContainer fillApps;
213 
214  for (uint32_t i = 0; i < fillNodes.GetN (); ++i)
215  {
216  AddressValue remoteAddress;
217  if (useIpv6 == 0)
218  {
219  remoteAddress = AddressValue(InetSocketAddress (star.GetHubIpv4Address (i / nFill), port));
220  }
221  else
222  {
223  remoteAddress = AddressValue(Inet6SocketAddress (star.GetHubIpv6Address (i / nFill), port));
224  }
225  onOffHelper.SetAttribute ("Remote", remoteAddress);
226  fillApps.Add (onOffHelper.Install (fillNodes.Get (i)));
227  }
228 
229  fillApps.Start (Seconds (1.0));
230  fillApps.Stop (Seconds (10.0));
231 
232  NS_LOG_INFO ("Enable static global routing.");
233  //
234  // Turn on global static routing so we can actually be routed across the star.
235  //
236  if (useIpv6 == 0)
237  {
239  }
240 
241  NS_LOG_INFO ("Enable pcap tracing.");
242  //
243  // Do pcap tracing on all devices on all nodes.
244  //
245  csma.EnablePcapAll ("csma-star", false);
246 
247  NS_LOG_INFO ("Run Simulation.");
248  Simulator::Run ();
250  NS_LOG_INFO ("Done.");
251 
252  return 0;
253 }
void AssignIpv4Addresses(Ipv4AddressHelper address)
holds a vector of ns3::Application pointers.
void InstallStack(InternetStackHelper stack)
an Inet address class
static Ipv4Address GetAny(void)
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
void SetChannelAttribute(std::string n1, const AttributeValue &v1)
Definition: csma-helper.cc:69
int main(int argc, char *argv[])
Definition: csma-star.cc:50
NS_LOG_COMPONENT_DEFINE("GrantedTimeWindowMpiInterface")
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:19
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
void Add(ApplicationContainer other)
Append the contents of another ApplicationContainer to the end of this container. ...
static void Run(void)
Run the simulation until one of:
Definition: simulator.cc:157
static Ipv6Address GetAny()
Get the "any" (::) Ipv6Address.
Ipv4Address GetHubIpv4Address(uint32_t i) const
aggregate IP/TCP/UDP functionality to existing Nodes.
void SetBase(Ipv6Address network, Ipv6Prefix prefix, Ipv6Address base=Ipv6Address("::1"))
Set the base network number, network prefix, and base interface ID.
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::CsmaChannel with the attributes configured by CsmaHelper::SetChannelAttri...
Definition: csma-helper.cc:215
#define NS_LOG_INFO(msg)
Definition: log.h:298
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:42
uint16_t port
Definition: dsdv-manet.cc:44
a polymophic address class
Definition: address.h:86
Ipv6Address GetHubIpv6Address(uint32_t i) const
Ipv6InterfaceContainer Assign(const NetDeviceContainer &c)
Allocate an Ipv6InterfaceContainer with auto-assigned addresses.
uint32_t GetN(void) const
Get the number of Ptr stored in this container.
NetDeviceContainer GetSpokeDevices() const
uint32_t GetN(void) const
Get the number of Ptr stored in this container.
void EnablePcapAll(std::string prefix, bool promiscuous=false)
Enable pcap output on each device (which is of the appropriate type) in the set of all nodes created ...
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
static void Init(const Ipv6Address net, const Ipv6Prefix prefix, const Ipv6Address interfaceId="::1")
Initialise the base network and interfaceId for the generator.
Hold an unsigned integer type.
Definition: uinteger.h:46
holds a vector of ns3::NetDevice pointers
An Inet6 address class.
Ptr< Node > GetSpokeNode(uint32_t i) const
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:152
static void Destroy(void)
Every event scheduled by the Simulator::insertAtDestroy method is invoked.
Definition: simulator.cc:121
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:667
Csma Channel.
Definition: csma-channel.h:76
keep track of a set of node pointers.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
void AssignIpv6Addresses(Ipv6Address network, Ipv6Prefix prefix)
build a set of CsmaNetDevice objects
Definition: csma-helper.h:46
Helper class to auto-assign global IPv6 unicast addresses.
Describes an IPv6 address.
Definition: ipv6-address.h:46
hold objects of type ns3::Address
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.
uint32_t SpokeCount() const
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:408
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
Describes an IPv6 prefix.
Definition: ipv6-address.h:387
ApplicationContainer Install(NodeContainer c) const
Install an ns3::PacketSinkApplication on each node of the input container configured with all the att...
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.
A helper to make it easier to create a star topology with Csma links.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
tuple address
Definition: first.py:37
Ptr< Node > GetHub() const
Ptr< T > GetObject(void) const
Definition: object.h:361
ApplicationContainer Install(NodeContainer c) const
Install an ns3::OnOffApplication on each node of the input container configured with all the attribut...
void SetAttribute(std::string name, const AttributeValue &value)
Helper function used to set the underlying application attributes.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
static Ipv6Address GetNetwork(const Ipv6Prefix prefix)
Get the current network of the given Ipv6Prefix.