A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
simple-alternate-routing.cc
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 *
4 */
5
6//
7// Network topology
8//
9// n0
10// \ 5 Mb/s, 2ms
11// \ 1.5Mb/s, 10ms
12// n2 ------------------------n3
13// / /
14// / 5 Mb/s, 2ms /
15// n1--------------------------
16// 1.5 Mb/s, 100ms
17//
18// this is a modification of simple-global-routing to allow for
19// a single hop but higher-cost path between n1 and n3
20//
21// - Tracing of queues and packet receptions to file "simple-rerouting.tr"
22
23#include "ns3/applications-module.h"
24#include "ns3/core-module.h"
25#include "ns3/internet-module.h"
26#include "ns3/ipv4-global-routing-helper.h"
27#include "ns3/network-module.h"
28#include "ns3/point-to-point-module.h"
29
30#include <cassert>
31#include <fstream>
32#include <iostream>
33#include <string>
34
35using namespace ns3;
36
37NS_LOG_COMPONENT_DEFINE("SimpleAlternateRoutingExample");
38
39int
40main(int argc, char* argv[])
41{
42 // Users may find it convenient to turn on explicit debugging
43 // for selected modules; the below lines suggest how to do this
44#if 0
45 LogComponentEnable ("GlobalRoutingHelper", LOG_LOGIC);
46 LogComponentEnable ("GlobalRouter", LOG_LOGIC);
47#endif
48
49 Config::SetDefault("ns3::OnOffApplication::PacketSize", UintegerValue(210));
50 Config::SetDefault("ns3::OnOffApplication::DataRate", StringValue("300b/s"));
51
52 // The below metric, if set to 3 or higher, will cause packets between
53 // n1 and n3 to take the 2-hop route through n2
54
55 CommandLine cmd(__FILE__);
56 //
57 // Additionally, we plumb this metric into the default value / command
58 // line argument system as well, for exemplary purposes. This means
59 // that it can be resettable at the command-line to the program,
60 // rather than recompiling
61 // e.g. ns3 --run "simple-alternate-routing --AlternateCost=5"
62 uint16_t sampleMetric = 1;
63 cmd.AddValue("AlternateCost",
64 "This metric is used in the example script between n3 and n1 ",
65 sampleMetric);
66
67 // Allow the user to override any of the defaults and the above
68 // DefaultValue::Bind ()s at run-time, via command-line arguments
69 cmd.Parse(argc, argv);
70
71 // Here, we will explicitly create four nodes. In more sophisticated
72 // topologies, we could configure a node factory.
73 NS_LOG_INFO("Create nodes.");
75 c.Create(4);
78 NodeContainer n3n2 = NodeContainer(c.Get(3), c.Get(2));
79 NodeContainer n1n3 = NodeContainer(c.Get(1), c.Get(3));
80
81 // We create the channels first without any IP addressing information
82 NS_LOG_INFO("Create channels.");
84 p2p.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
85 p2p.SetChannelAttribute("Delay", StringValue("2ms"));
86 NetDeviceContainer d0d2 = p2p.Install(n0n2);
87
88 NetDeviceContainer d1d2 = p2p.Install(n1n2);
89
90 p2p.SetDeviceAttribute("DataRate", StringValue("1500kbps"));
91 p2p.SetChannelAttribute("Delay", StringValue("10ms"));
92 NetDeviceContainer d3d2 = p2p.Install(n3n2);
93
94 p2p.SetChannelAttribute("Delay", StringValue("100ms"));
95 NetDeviceContainer d1d3 = p2p.Install(n1n3);
96
98 internet.Install(c);
99
100 // Later, we add IP addresses. The middle two octets correspond to
101 // the channel number.
102 NS_LOG_INFO("Assign IP Addresses.");
104 ipv4.SetBase("10.0.0.0", "255.255.255.0");
105 ipv4.Assign(d0d2);
106
107 ipv4.SetBase("10.1.1.0", "255.255.255.0");
108 Ipv4InterfaceContainer i1i2 = ipv4.Assign(d1d2);
109
110 ipv4.SetBase("10.2.2.0", "255.255.255.0");
111 ipv4.Assign(d3d2);
112
113 ipv4.SetBase("10.3.3.0", "255.255.255.0");
114 Ipv4InterfaceContainer i1i3 = ipv4.Assign(d1d3);
115
116 i1i3.SetMetric(0, sampleMetric);
117 i1i3.SetMetric(1, sampleMetric);
118
119 // Create router nodes, initialize routing database and set up the routing
120 // tables in the nodes.
122
123 // Create the OnOff application to send UDP datagrams
124 NS_LOG_INFO("Create Application.");
125 uint16_t port = 9; // Discard port (RFC 863)
126
127 // Create a flow from n3 to n1, starting at time 1.1 seconds
128 OnOffHelper onoff("ns3::UdpSocketFactory",
130 onoff.SetConstantRate(DataRate("300b/s"));
131
132 ApplicationContainer apps = onoff.Install(c.Get(3));
133 apps.Start(Seconds(1.1));
134 apps.Stop(Seconds(10.0));
135
136 // Create a packet sink to receive these packets
137 PacketSinkHelper sink("ns3::UdpSocketFactory",
139 apps = sink.Install(c.Get(1));
140 apps.Start(Seconds(1.1));
141 apps.Stop(Seconds(10.0));
142
143 AsciiTraceHelper ascii;
144 p2p.EnableAsciiAll(ascii.CreateFileStream("simple-alternate-routing.tr"));
145 p2p.EnablePcapAll("simple-alternate-routing");
146
147 NS_LOG_INFO("Run Simulation.");
150 NS_LOG_INFO("Done.");
151
152 return 0;
153}
NodeContainer n1n2
Nodecontainer n1 + n2.
Ipv4InterfaceContainer i1i2
IPv4 interface container i1 + i2.
NodeContainer n0n2
Nodecontainer n0 + n2.
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.
Manage ASCII trace files for device models.
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we'll use to write the traced bits.
Parse command-line arguments.
Class for representing data rates.
Definition data-rate.h:78
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.
holds a vector of std::pair of Ptr<Ipv4> and interface index.
void SetMetric(uint32_t i, uint16_t metric)
Set a metric for the given interface.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
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.
Build a set of PointToPointNetDevice objects.
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
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition log.cc:291
@ LOG_LOGIC
Debugging logs for key branches and decisions in a function.
Definition log.h:98
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition wifi-tcp.cc:44