A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ripng-simple-network.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2014 Universita' di Firenze, Italy
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
7 */
8
9// Network topology
10//
11// SRC
12// |<=== source network
13// A-----B
14// \ / \ all networks have cost 1, except
15// \ / | for the direct link from C to D, which
16// C / has cost 10
17// | /
18// |/
19// D
20// |<=== target network
21// DST
22//
23//
24// A, B, C and D are RIPng routers.
25// A and D are configured with static addresses.
26// SRC and DST will exchange packets.
27//
28// After about 3 seconds, the topology is built, and Echo Reply will be received.
29// After 40 seconds, the link between B and D will break, causing a route failure.
30// After 44 seconds from the failure, the routers will recovery from the failure.
31// Split Horizoning should affect the recovery time, but it is not. See the manual
32// for an explanation of this effect.
33//
34// If "showPings" is enabled, the user will see:
35// 1) if the ping has been acknowledged
36// 2) if a Destination Unreachable has been received by the sender
37// 3) nothing, when the Echo Request has been received by the destination but
38// the Echo Reply is unable to reach the sender.
39// Examining the .pcap files with Wireshark can confirm this effect.
40
41#include "ns3/core-module.h"
42#include "ns3/csma-module.h"
43#include "ns3/internet-apps-module.h"
44#include "ns3/internet-module.h"
45#include "ns3/ipv6-routing-table-entry.h"
46#include "ns3/ipv6-static-routing-helper.h"
47
48#include <fstream>
49
50using namespace ns3;
51
52NS_LOG_COMPONENT_DEFINE("RipNgSimpleRouting");
53
54void
55TearDownLink(Ptr<Node> nodeA, Ptr<Node> nodeB, uint32_t interfaceA, uint32_t interfaceB)
56{
57 nodeA->GetObject<Ipv6>()->SetDown(interfaceA);
58 nodeB->GetObject<Ipv6>()->SetDown(interfaceB);
59}
60
61int
62main(int argc, char** argv)
63{
64 bool verbose = false;
65 bool printRoutingTables = false;
66 bool showPings = false;
67 std::string SplitHorizon("PoisonReverse");
68
69 CommandLine cmd(__FILE__);
70 cmd.AddValue("verbose", "turn on log components", verbose);
71 cmd.AddValue("printRoutingTables",
72 "Print routing tables at 30, 60 and 90 seconds",
73 printRoutingTables);
74 cmd.AddValue("showPings", "Show Ping reception", showPings);
75 cmd.AddValue("splitHorizonStrategy",
76 "Split Horizon strategy to use (NoSplitHorizon, SplitHorizon, PoisonReverse)",
77 SplitHorizon);
78 cmd.Parse(argc, argv);
79
80 if (verbose)
81 {
82 LogComponentEnable("RipNgSimpleRouting", LOG_LEVEL_INFO);
84 LogComponentEnable("Icmpv6L4Protocol", LOG_LEVEL_INFO);
85 LogComponentEnable("Ipv6Interface", LOG_LEVEL_ALL);
86 LogComponentEnable("Icmpv6L4Protocol", LOG_LEVEL_ALL);
87 LogComponentEnable("NdiscCache", LOG_LEVEL_ALL);
89 }
90
91 if (SplitHorizon == "NoSplitHorizon")
92 {
93 Config::SetDefault("ns3::RipNg::SplitHorizon", EnumValue(RipNg::NO_SPLIT_HORIZON));
94 }
95 else if (SplitHorizon == "SplitHorizon")
96 {
97 Config::SetDefault("ns3::RipNg::SplitHorizon", EnumValue(RipNg::SPLIT_HORIZON));
98 }
99 else
100 {
101 Config::SetDefault("ns3::RipNg::SplitHorizon", EnumValue(RipNg::POISON_REVERSE));
102 }
103
104 NS_LOG_INFO("Create nodes.");
106 Names::Add("SrcNode", src);
108 Names::Add("DstNode", dst);
110 Names::Add("RouterA", a);
112 Names::Add("RouterB", b);
114 Names::Add("RouterC", c);
116 Names::Add("RouterD", d);
117 NodeContainer net1(src, a);
118 NodeContainer net2(a, b);
119 NodeContainer net3(a, c);
120 NodeContainer net4(b, c);
121 NodeContainer net5(c, d);
122 NodeContainer net6(b, d);
123 NodeContainer net7(d, dst);
124 NodeContainer routers(a, b, c, d);
125 NodeContainer nodes(src, dst);
126
127 NS_LOG_INFO("Create channels.");
129 csma.SetChannelAttribute("DataRate", DataRateValue(5000000));
130 csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
131 NetDeviceContainer ndc1 = csma.Install(net1);
132 NetDeviceContainer ndc2 = csma.Install(net2);
133 NetDeviceContainer ndc3 = csma.Install(net3);
134 NetDeviceContainer ndc4 = csma.Install(net4);
135 NetDeviceContainer ndc5 = csma.Install(net5);
136 NetDeviceContainer ndc6 = csma.Install(net6);
137 NetDeviceContainer ndc7 = csma.Install(net7);
138
139 NS_LOG_INFO("Create IPv6 and routing");
140 RipNgHelper ripNgRouting;
141
142 // Rule of thumb:
143 // Interfaces are added sequentially, starting from 0
144 // However, interface 0 is always the loopback...
145 ripNgRouting.ExcludeInterface(a, 1);
146 ripNgRouting.ExcludeInterface(d, 3);
147
148 ripNgRouting.SetInterfaceMetric(c, 3, 10);
149 ripNgRouting.SetInterfaceMetric(d, 1, 10);
150
152 listRH.Add(ripNgRouting, 0);
154 listRH.Add(staticRh, 5);
155
156 InternetStackHelper internetv6;
157 internetv6.SetIpv4StackInstall(false);
158 internetv6.SetRoutingHelper(listRH);
159 internetv6.Install(routers);
160
161 InternetStackHelper internetv6Nodes;
162 internetv6Nodes.SetIpv4StackInstall(false);
163 internetv6Nodes.Install(nodes);
164
165 // Assign addresses.
166 // The source and destination networks have global addresses
167 // The "core" network just needs link-local addresses for routing.
168 // We assign global addresses to the routers as well to receive
169 // ICMPv6 errors.
170 NS_LOG_INFO("Assign IPv6 Addresses.");
172
173 ipv6.SetBase(Ipv6Address("2001:1::"), Ipv6Prefix(64));
174 Ipv6InterfaceContainer iic1 = ipv6.Assign(ndc1);
175 iic1.SetForwarding(1, true);
177
178 ipv6.SetBase(Ipv6Address("2001:0:1::"), Ipv6Prefix(64));
179 Ipv6InterfaceContainer iic2 = ipv6.Assign(ndc2);
180 iic2.SetForwarding(0, true);
181 iic2.SetForwarding(1, true);
182
183 ipv6.SetBase(Ipv6Address("2001:0:2::"), Ipv6Prefix(64));
184 Ipv6InterfaceContainer iic3 = ipv6.Assign(ndc3);
185 iic3.SetForwarding(0, true);
186 iic3.SetForwarding(1, true);
187
188 ipv6.SetBase(Ipv6Address("2001:0:3::"), Ipv6Prefix(64));
189 Ipv6InterfaceContainer iic4 = ipv6.Assign(ndc4);
190 iic4.SetForwarding(0, true);
191 iic4.SetForwarding(1, true);
192
193 ipv6.SetBase(Ipv6Address("2001:0:4::"), Ipv6Prefix(64));
194 Ipv6InterfaceContainer iic5 = ipv6.Assign(ndc5);
195 iic5.SetForwarding(0, true);
196 iic5.SetForwarding(1, true);
197
198 ipv6.SetBase(Ipv6Address("2001:0:5::"), Ipv6Prefix(64));
199 Ipv6InterfaceContainer iic6 = ipv6.Assign(ndc6);
200 iic6.SetForwarding(0, true);
201 iic6.SetForwarding(1, true);
202
203 ipv6.SetBase(Ipv6Address("2001:2::"), Ipv6Prefix(64));
204 Ipv6InterfaceContainer iic7 = ipv6.Assign(ndc7);
205 iic7.SetForwarding(0, true);
207
208 if (printRoutingTables)
209 {
210 Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper>(&std::cout);
211
212 Ipv6RoutingHelper::PrintRoutingTableAt(Seconds(30.0), a, routingStream);
213 Ipv6RoutingHelper::PrintRoutingTableAt(Seconds(30.0), b, routingStream);
214 Ipv6RoutingHelper::PrintRoutingTableAt(Seconds(30.0), c, routingStream);
215 Ipv6RoutingHelper::PrintRoutingTableAt(Seconds(30.0), d, routingStream);
216
217 Ipv6RoutingHelper::PrintRoutingTableAt(Seconds(60.0), a, routingStream);
218 Ipv6RoutingHelper::PrintRoutingTableAt(Seconds(60.0), b, routingStream);
219 Ipv6RoutingHelper::PrintRoutingTableAt(Seconds(60.0), c, routingStream);
220 Ipv6RoutingHelper::PrintRoutingTableAt(Seconds(60.0), d, routingStream);
221
222 Ipv6RoutingHelper::PrintRoutingTableAt(Seconds(90.0), a, routingStream);
223 Ipv6RoutingHelper::PrintRoutingTableAt(Seconds(90.0), b, routingStream);
224 Ipv6RoutingHelper::PrintRoutingTableAt(Seconds(90.0), c, routingStream);
225 Ipv6RoutingHelper::PrintRoutingTableAt(Seconds(90.0), d, routingStream);
226 }
227
228 NS_LOG_INFO("Create Applications.");
229 uint32_t packetSize = 1024;
230 Time interPacketInterval = Seconds(1.0);
231 PingHelper ping(iic7.GetAddress(1, 1));
232
233 ping.SetAttribute("Interval", TimeValue(interPacketInterval));
234 ping.SetAttribute("Size", UintegerValue(packetSize));
235 if (showPings)
236 {
237 ping.SetAttribute("VerboseMode", EnumValue(Ping::VerboseMode::VERBOSE));
238 }
239 ApplicationContainer apps = ping.Install(src);
240 apps.Start(Seconds(1.0));
241 apps.Stop(Seconds(110.0));
242
243 AsciiTraceHelper ascii;
244 csma.EnableAsciiAll(ascii.CreateFileStream("ripng-simple-routing.tr"));
245 csma.EnablePcapAll("ripng-simple-routing", true);
246
247 Simulator::Schedule(Seconds(40), &TearDownLink, b, d, 3, 2);
248
249 /* Now, do the actual simulation. */
250 NS_LOG_INFO("Run Simulation.");
254 NS_LOG_INFO("Done.");
255
256 return 0;
257}
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.
build a set of CsmaNetDevice objects
Definition csma-helper.h:37
Hold variables of type enum.
Definition enum.h:52
aggregate IP/TCP/UDP functionality to existing Nodes.
void SetIpv4StackInstall(bool enable)
Enable/disable IPv4 stack install.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
void SetRoutingHelper(const Ipv4RoutingHelper &routing)
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.
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition ipv6.h:71
Keep track of a set of IPv6 interfaces.
void SetForwarding(uint32_t i, bool state)
Set the state of the stack (act as a router or as an host) for the specified index.
void SetDefaultRouteInAllNodes(uint32_t router)
Set the default route for all the devices (except the router itself).
Ipv6Address GetAddress(uint32_t i, uint32_t j) const
Get the address for the specified index.
Helper class that adds ns3::Ipv6ListRouting objects.
void Add(const Ipv6RoutingHelper &routing, int16_t priority)
Describes an IPv6 prefix.
static void PrintRoutingTableAt(Time printTime, Ptr< Node > node, Ptr< OutputStreamWrapper > stream, Time::Unit unit=Time::S)
prints the routing tables of a node at a particular time.
Helper class that adds ns3::Ipv6StaticRouting objects.
static void Add(std::string name, Ptr< Object > object)
Add the association between the string "name" and the Ptr<Object> obj.
Definition names.cc:764
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
Create a ping application and associate it to a node.
Definition ping-helper.h:31
Smart pointer class similar to boost::intrusive_ptr.
Helper class that adds RIPng routing to nodes.
void ExcludeInterface(Ptr< Node > node, uint32_t interface)
Exclude an interface from RIPng protocol.
void SetInterfaceMetric(Ptr< Node > node, uint32_t interface, uint8_t metric)
Set a metric for an interface.
@ SPLIT_HORIZON
Split Horizon.
Definition ripng.h:215
@ POISON_REVERSE
Poison Reverse Split Horizon.
Definition ripng.h:216
@ NO_SPLIT_HORIZON
No Split Horizon.
Definition ripng.h:214
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
Hold an unsigned integer type.
Definition uinteger.h:34
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
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1320
NodeContainer nodes
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_LEVEL_ALL
Print everything.
Definition log.h:105
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition log.h:93
bool verbose
void TearDownLink(Ptr< Node > nodeA, Ptr< Node > nodeB, uint32_t interfaceA, uint32_t interfaceB)
static const uint32_t packetSize
Packet size generated at the AP.