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