A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
csma-star.cc
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License version 2 as
4 * published by the Free Software Foundation;
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 *
15 */
16
17#include "ns3/applications-module.h"
18#include "ns3/core-module.h"
19#include "ns3/csma-module.h"
20#include "ns3/csma-star-helper.h"
21#include "ns3/internet-module.h"
22#include "ns3/ipv6-address-generator.h"
23#include "ns3/network-module.h"
24
25// Network topology (default)
26//
27// n2 + + n3 .
28// | ... |\ /| ... | .
29// ======= \ / ======= .
30// CSMA \ / CSMA .
31// \ / .
32// n1 +--- n0 ---+ n4 .
33// | ... | / \ | ... | .
34// ======= / \ ======= .
35// CSMA / \ CSMA .
36// / \ .
37// n6 + + n5 .
38// | ... | | ... | .
39// ======= ======= .
40// CSMA CSMA .
41//
42
43using namespace ns3;
44
45NS_LOG_COMPONENT_DEFINE("CsmaStar");
46
47int
48main(int argc, char* argv[])
49{
50 //
51 // Set up some default values for the simulation.
52 //
53 Config::SetDefault("ns3::OnOffApplication::PacketSize", UintegerValue(137));
54
55 // ??? try and stick 15kb/s into the data rate
56 Config::SetDefault("ns3::OnOffApplication::DataRate", StringValue("14kb/s"));
57
58 //
59 // Default number of nodes in the star. Overridable by command line argument.
60 //
61 uint32_t nSpokes = 7;
62 uint32_t useIpv6 = 0;
63 Ipv6Address ipv6AddressBase = Ipv6Address("2001::");
64 Ipv6Prefix ipv6AddressPrefix = Ipv6Prefix(64);
65
66 CommandLine cmd(__FILE__);
67 cmd.AddValue("nSpokes", "Number of spoke nodes to place in the star", nSpokes);
68 cmd.AddValue("useIpv6", "Use Ipv6", useIpv6);
69 cmd.Parse(argc, argv);
70
71 NS_LOG_INFO("Build star topology.");
73 csma.SetChannelAttribute("DataRate", StringValue("100Mbps"));
74 csma.SetChannelAttribute("Delay", StringValue("1ms"));
75 CsmaStarHelper star(nSpokes, csma);
76
77 NodeContainer fillNodes;
78
79 //
80 // Just to be nasy, hang some more nodes off of the CSMA channel for each
81 // spoke, so that there are a total of 16 nodes on each channel. Stash
82 // all of these new devices into a container.
83 //
84 NetDeviceContainer fillDevices;
85
86 uint32_t nFill = 14;
87 for (uint32_t i = 0; i < star.GetSpokeDevices().GetN(); ++i)
88 {
89 Ptr<Channel> channel = star.GetSpokeDevices().Get(i)->GetChannel();
90 Ptr<CsmaChannel> csmaChannel = channel->GetObject<CsmaChannel>();
91 NodeContainer newNodes;
92 newNodes.Create(nFill);
93 fillNodes.Add(newNodes);
94 fillDevices.Add(csma.Install(newNodes, csmaChannel));
95 }
96
97 NS_LOG_INFO("Install internet stack on all nodes.");
99 star.InstallStack(internet);
100 internet.Install(fillNodes);
101
102 NS_LOG_INFO("Assign IP Addresses.");
103 if (useIpv6 == 0)
104 {
105 star.AssignIpv4Addresses(Ipv4AddressHelper("10.1.0.0", "255.255.255.0"));
106 }
107 else
108 {
109 star.AssignIpv6Addresses(ipv6AddressBase, ipv6AddressPrefix);
110 }
111
112 //
113 // We assigned addresses to the logical hub and the first "drop" of the
114 // CSMA network that acts as the spoke, but we also have a number of fill
115 // devices (nFill) also hanging off the CSMA network. We have got to
116 // assign addresses to them as well. We put all of the fill devices into
117 // a single device container, so the first nFill devices are associated
118 // with the channel connected to spokeDevices.Get (0), the second nFill
119 // devices are associated with the channel connected to spokeDevices.Get (1)
120 // etc.
121 //
123 Ipv6AddressHelper address6;
124 for (uint32_t i = 0; i < star.SpokeCount(); ++i)
125 {
126 if (useIpv6 == 0)
127 {
128 std::ostringstream subnet;
129 subnet << "10.1." << i << ".0";
130 NS_LOG_INFO("Assign IP Addresses for CSMA subnet " << subnet.str());
131 address.SetBase(subnet.str().c_str(), "255.255.255.0", "0.0.0.3");
132
133 for (uint32_t j = 0; j < nFill; ++j)
134 {
135 address.Assign(fillDevices.Get(i * nFill + j));
136 }
137 }
138 else
139 {
140 Ipv6AddressGenerator::Init(ipv6AddressBase, ipv6AddressPrefix);
141 Ipv6Address v6network = Ipv6AddressGenerator::GetNetwork(ipv6AddressPrefix);
142 address6.SetBase(v6network, ipv6AddressPrefix);
143
144 for (uint32_t j = 0; j < nFill; ++j)
145 {
146 address6.Assign(fillDevices.Get(i * nFill + j));
147 }
148 }
149 }
150
151 NS_LOG_INFO("Create applications.");
152 //
153 // Create a packet sink on the star "hub" to receive packets.
154 //
155 uint16_t port = 50000;
156
157 if (useIpv6 == 0)
158 {
160 PacketSinkHelper packetSinkHelper("ns3::TcpSocketFactory", hubLocalAddress);
161 ApplicationContainer hubApp = packetSinkHelper.Install(star.GetHub());
162 hubApp.Start(Seconds(1.0));
163 hubApp.Stop(Seconds(10.0));
164 }
165 else
166 {
168 PacketSinkHelper packetSinkHelper6("ns3::TcpSocketFactory", hubLocalAddress6);
169 ApplicationContainer hubApp6 = packetSinkHelper6.Install(star.GetHub());
170 hubApp6.Start(Seconds(1.0));
171 hubApp6.Stop(Seconds(10.0));
172 }
173
174 //
175 // Create OnOff applications to send TCP to the hub, one on each spoke node.
176 //
177 OnOffHelper onOffHelper("ns3::TcpSocketFactory", Address());
178 onOffHelper.SetAttribute("OnTime", StringValue("ns3::ConstantRandomVariable[Constant=1]"));
179 onOffHelper.SetAttribute("OffTime", StringValue("ns3::ConstantRandomVariable[Constant=0]"));
180
181 ApplicationContainer spokeApps;
182
183 for (uint32_t i = 0; i < star.SpokeCount(); ++i)
184 {
185 if (useIpv6 == 0)
186 {
187 AddressValue remoteAddress(InetSocketAddress(star.GetHubIpv4Address(i), port));
188 onOffHelper.SetAttribute("Remote", remoteAddress);
189 }
190 else
191 {
192 AddressValue remoteAddress(Inet6SocketAddress(star.GetHubIpv6Address(i), port));
193 onOffHelper.SetAttribute("Remote", remoteAddress);
194 }
195 spokeApps.Add(onOffHelper.Install(star.GetSpokeNode(i)));
196 }
197
198 spokeApps.Start(Seconds(1.0));
199 spokeApps.Stop(Seconds(10.0));
200
201 //
202 // Because we are evil, we also add OnOff applications to send TCP to the hub
203 // from the fill devices on each CSMA link. The first nFill nodes in the
204 // fillNodes container are on the CSMA network talking to the zeroth device
205 // on the hub node. The next nFill nodes are on the CSMA network talking to
206 // the first device on the hub node, etc. So the ith fillNode is associated
207 // with the hub address found on the (i / nFill)th device on the hub node.
208 //
209 ApplicationContainer fillApps;
210
211 for (uint32_t i = 0; i < fillNodes.GetN(); ++i)
212 {
214 if (useIpv6 == 0)
215 {
217 AddressValue(InetSocketAddress(star.GetHubIpv4Address(i / nFill), port));
218 }
219 else
220 {
222 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 {
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.");
249 NS_LOG_INFO("Done.");
250
251 return 0;
252}
a polymophic address class
Definition: address.h:101
AttributeValue implementation for Address.
Definition: address.h:286
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.
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
Csma Channel.
Definition: csma-channel.h:92
build a set of CsmaNetDevice objects
Definition: csma-helper.h:48
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.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
static Ipv4Address GetAny()
static void PopulateRoutingTables()
Build a routing database and initialize the routing tables of the nodes in the simulation.
static void Init(const Ipv6Address net, const Ipv6Prefix prefix, const Ipv6Address interfaceId="::1")
Initialise the base network and interfaceId for the generator.
static Ipv6Address GetNetwork(const Ipv6Prefix prefix)
Get the current network of the given Ipv6Prefix.
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:49
static Ipv6Address GetAny()
Get the "any" (::) Ipv6Address.
Describes an IPv6 prefix.
Definition: ipv6-address.h:455
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() 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(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.
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:37
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
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:44
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:894
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
ns address
Definition: first.py:47
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns cmd
Definition: second.py:40
ns csma
Definition: second.py:63
ns channel
Definition: third.py:88