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