A Discrete-Event Network Simulator
API
rip-simple-network.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2016 Universita' di Firenze, Italy
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
19  */
20 
21 // Network topology
22 //
23 // SRC
24 // |<=== source network
25 // A-----B
26 // \ / \ all networks have cost 1, except
27 // \ / | for the direct link from C to D, which
28 // C / has cost 10
29 // | /
30 // |/
31 // D
32 // |<=== target network
33 // DST
34 //
35 //
36 // A, B, C and D are RIPng routers.
37 // A and D are configured with static addresses.
38 // SRC and DST will exchange packets.
39 //
40 // After about 3 seconds, the topology is built, and Echo Reply will be received.
41 // After 40 seconds, the link between B and D will break, causing a route failure.
42 // After 44 seconds from the failure, the routers will recovery from the failure.
43 // Split Horizoning should affect the recovery time, but it is not. See the manual
44 // for an explanation of this effect.
45 //
46 // If "showPings" is enabled, the user will see:
47 // 1) if the ping has been acknowledged
48 // 2) if a Destination Unreachable has been received by the sender
49 // 3) nothing, when the Echo Request has been received by the destination but
50 // the Echo Reply is unable to reach the sender.
51 // Examining the .pcap files with Wireshark can confirm this effect.
52 
53 
54 #include <fstream>
55 #include "ns3/core-module.h"
56 #include "ns3/internet-module.h"
57 #include "ns3/csma-module.h"
58 #include "ns3/internet-apps-module.h"
59 #include "ns3/ipv4-static-routing-helper.h"
60 #include "ns3/ipv4-routing-table-entry.h"
61 
62 using namespace ns3;
63 
64 NS_LOG_COMPONENT_DEFINE ("RipSimpleRouting");
65 
66 void TearDownLink (Ptr<Node> nodeA, Ptr<Node> nodeB, uint32_t interfaceA, uint32_t interfaceB)
67 {
68  nodeA->GetObject<Ipv4> ()->SetDown (interfaceA);
69  nodeB->GetObject<Ipv4> ()->SetDown (interfaceB);
70 }
71 
72 int main (int argc, char **argv)
73 {
74  bool verbose = false;
75  bool printRoutingTables = false;
76  bool showPings = false;
77  std::string SplitHorizon ("PoisonReverse");
78 
79  CommandLine cmd (__FILE__);
80  cmd.AddValue ("verbose", "turn on log components", verbose);
81  cmd.AddValue ("printRoutingTables", "Print routing tables at 30, 60 and 90 seconds", printRoutingTables);
82  cmd.AddValue ("showPings", "Show Ping6 reception", showPings);
83  cmd.AddValue ("splitHorizonStrategy", "Split Horizon strategy to use (NoSplitHorizon, SplitHorizon, PoisonReverse)", SplitHorizon);
84  cmd.Parse (argc, argv);
85 
86  if (verbose)
87  {
89  LogComponentEnable ("RipSimpleRouting", LOG_LEVEL_INFO);
91  LogComponentEnable ("Ipv4Interface", LOG_LEVEL_ALL);
92  LogComponentEnable ("Icmpv4L4Protocol", LOG_LEVEL_ALL);
93  LogComponentEnable ("Ipv4L3Protocol", LOG_LEVEL_ALL);
94  LogComponentEnable ("ArpCache", LOG_LEVEL_ALL);
96  }
97 
98  if (SplitHorizon == "NoSplitHorizon")
99  {
100  Config::SetDefault ("ns3::Rip::SplitHorizon", EnumValue (RipNg::NO_SPLIT_HORIZON));
101  }
102  else if (SplitHorizon == "SplitHorizon")
103  {
104  Config::SetDefault ("ns3::Rip::SplitHorizon", EnumValue (RipNg::SPLIT_HORIZON));
105  }
106  else
107  {
108  Config::SetDefault ("ns3::Rip::SplitHorizon", EnumValue (RipNg::POISON_REVERSE));
109  }
110 
111  NS_LOG_INFO ("Create nodes.");
112  Ptr<Node> src = CreateObject<Node> ();
113  Names::Add ("SrcNode", src);
114  Ptr<Node> dst = CreateObject<Node> ();
115  Names::Add ("DstNode", dst);
116  Ptr<Node> a = CreateObject<Node> ();
117  Names::Add ("RouterA", a);
118  Ptr<Node> b = CreateObject<Node> ();
119  Names::Add ("RouterB", b);
120  Ptr<Node> c = CreateObject<Node> ();
121  Names::Add ("RouterC", c);
122  Ptr<Node> d = CreateObject<Node> ();
123  Names::Add ("RouterD", d);
124  NodeContainer net1 (src, a);
125  NodeContainer net2 (a, b);
126  NodeContainer net3 (a, c);
127  NodeContainer net4 (b, c);
128  NodeContainer net5 (c, d);
129  NodeContainer net6 (b, d);
130  NodeContainer net7 (d, dst);
131  NodeContainer routers (a, b, c, d);
132  NodeContainer nodes (src, dst);
133 
134 
135  NS_LOG_INFO ("Create channels.");
137  csma.SetChannelAttribute ("DataRate", DataRateValue (5000000));
138  csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
139  NetDeviceContainer ndc1 = csma.Install (net1);
140  NetDeviceContainer ndc2 = csma.Install (net2);
141  NetDeviceContainer ndc3 = csma.Install (net3);
142  NetDeviceContainer ndc4 = csma.Install (net4);
143  NetDeviceContainer ndc5 = csma.Install (net5);
144  NetDeviceContainer ndc6 = csma.Install (net6);
145  NetDeviceContainer ndc7 = csma.Install (net7);
146 
147  NS_LOG_INFO ("Create IPv4 and routing");
148  RipHelper ripRouting;
149 
150  // Rule of thumb:
151  // Interfaces are added sequentially, starting from 0
152  // However, interface 0 is always the loopback...
153  ripRouting.ExcludeInterface (a, 1);
154  ripRouting.ExcludeInterface (d, 3);
155 
156  ripRouting.SetInterfaceMetric (c, 3, 10);
157  ripRouting.SetInterfaceMetric (d, 1, 10);
158 
159  Ipv4ListRoutingHelper listRH;
160  listRH.Add (ripRouting, 0);
161 // Ipv4StaticRoutingHelper staticRh;
162 // listRH.Add (staticRh, 5);
163 
164  InternetStackHelper internet;
165  internet.SetIpv6StackInstall (false);
166  internet.SetRoutingHelper (listRH);
167  internet.Install (routers);
168 
169  InternetStackHelper internetNodes;
170  internetNodes.SetIpv6StackInstall (false);
171  internetNodes.Install (nodes);
172 
173  // Assign addresses.
174  // The source and destination networks have global addresses
175  // The "core" network just needs link-local addresses for routing.
176  // We assign global addresses to the routers as well to receive
177  // ICMPv6 errors.
178  NS_LOG_INFO ("Assign IPv4 Addresses.");
179  Ipv4AddressHelper ipv4;
180 
181  ipv4.SetBase (Ipv4Address ("10.0.0.0"), Ipv4Mask ("255.255.255.0"));
182  Ipv4InterfaceContainer iic1 = ipv4.Assign (ndc1);
183 
184  ipv4.SetBase (Ipv4Address ("10.0.1.0"), Ipv4Mask ("255.255.255.0"));
185  Ipv4InterfaceContainer iic2 = ipv4.Assign (ndc2);
186 
187  ipv4.SetBase (Ipv4Address ("10.0.2.0"), Ipv4Mask ("255.255.255.0"));
188  Ipv4InterfaceContainer iic3 = ipv4.Assign (ndc3);
189 
190  ipv4.SetBase (Ipv4Address ("10.0.3.0"), Ipv4Mask ("255.255.255.0"));
191  Ipv4InterfaceContainer iic4 = ipv4.Assign (ndc4);
192 
193  ipv4.SetBase (Ipv4Address ("10.0.4.0"), Ipv4Mask ("255.255.255.0"));
194  Ipv4InterfaceContainer iic5 = ipv4.Assign (ndc5);
195 
196  ipv4.SetBase (Ipv4Address ("10.0.5.0"), Ipv4Mask ("255.255.255.0"));
197  Ipv4InterfaceContainer iic6 = ipv4.Assign (ndc6);
198 
199  ipv4.SetBase (Ipv4Address ("10.0.6.0"), Ipv4Mask ("255.255.255.0"));
200  Ipv4InterfaceContainer iic7 = ipv4.Assign (ndc7);
201 
202  Ptr<Ipv4StaticRouting> staticRouting;
203  staticRouting = Ipv4RoutingHelper::GetRouting <Ipv4StaticRouting> (src->GetObject<Ipv4> ()->GetRoutingProtocol ());
204  staticRouting->SetDefaultRoute ("10.0.0.2", 1 );
205  staticRouting = Ipv4RoutingHelper::GetRouting <Ipv4StaticRouting> (dst->GetObject<Ipv4> ()->GetRoutingProtocol ());
206  staticRouting->SetDefaultRoute ("10.0.6.1", 1 );
207 
208  if (printRoutingTables)
209  {
210  RipHelper routingHelper;
211 
212  Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper> (&std::cout);
213 
214  routingHelper.PrintRoutingTableAt (Seconds (30.0), a, routingStream);
215  routingHelper.PrintRoutingTableAt (Seconds (30.0), b, routingStream);
216  routingHelper.PrintRoutingTableAt (Seconds (30.0), c, routingStream);
217  routingHelper.PrintRoutingTableAt (Seconds (30.0), d, routingStream);
218 
219  routingHelper.PrintRoutingTableAt (Seconds (60.0), a, routingStream);
220  routingHelper.PrintRoutingTableAt (Seconds (60.0), b, routingStream);
221  routingHelper.PrintRoutingTableAt (Seconds (60.0), c, routingStream);
222  routingHelper.PrintRoutingTableAt (Seconds (60.0), d, routingStream);
223 
224  routingHelper.PrintRoutingTableAt (Seconds (90.0), a, routingStream);
225  routingHelper.PrintRoutingTableAt (Seconds (90.0), b, routingStream);
226  routingHelper.PrintRoutingTableAt (Seconds (90.0), c, routingStream);
227  routingHelper.PrintRoutingTableAt (Seconds (90.0), d, routingStream);
228  }
229 
230  NS_LOG_INFO ("Create Applications.");
231  uint32_t packetSize = 1024;
232  Time interPacketInterval = Seconds (1.0);
233  V4PingHelper ping ("10.0.6.2");
234 
235  ping.SetAttribute ("Interval", TimeValue (interPacketInterval));
236  ping.SetAttribute ("Size", UintegerValue (packetSize));
237  if (showPings)
238  {
239  ping.SetAttribute ("Verbose", BooleanValue (true));
240  }
241  ApplicationContainer apps = ping.Install (src);
242  apps.Start (Seconds (1.0));
243  apps.Stop (Seconds (110.0));
244 
245  AsciiTraceHelper ascii;
246  csma.EnableAsciiAll (ascii.CreateFileStream ("rip-simple-routing.tr"));
247  csma.EnablePcapAll ("rip-simple-routing", true);
248 
249  Simulator::Schedule (Seconds (40), &TearDownLink, b, d, 3, 2);
250 
251  /* Now, do the actual simulation. */
252  NS_LOG_INFO ("Run Simulation.");
253  Simulator::Stop (Seconds (131.0));
254  Simulator::Run ();
256  NS_LOG_INFO ("Done.");
257 }
258 
ns3::NetDeviceContainer
holds a vector of ns3::NetDevice pointers
Definition: net-device-container.h:42
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
ns3::DataRateValue
AttributeValue implementation for DataRate.
Definition: data-rate.h:298
ns3::CommandLine
Parse command-line arguments.
Definition: command-line.h:228
ns3::Ipv4RoutingHelper::PrintRoutingTableAt
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.
Definition: ipv4-routing-helper.cc:59
ns3::BooleanValue
AttributeValue implementation for Boolean.
Definition: boolean.h:37
ns3::LOG_LEVEL_INFO
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition: log.h:107
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::ApplicationContainer::Stop
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
Definition: application-container.cc:107
ns3::Ipv4ListRoutingHelper
Helper class that adds ns3::Ipv4ListRouting objects.
Definition: ipv4-list-routing-helper.h:38
ns3::Ipv4AddressHelper
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Definition: ipv4-address-helper.h:48
ns3::LOG_PREFIX_NODE
@ LOG_PREFIX_NODE
Prefix all trace prints with simulation node.
Definition: log.h:120
ns3::RipHelper
Helper class that adds RIP routing to nodes.
Definition: rip-helper.h:41
ns3::Object::GetObject
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:470
ns3::LOG_LEVEL_ALL
@ LOG_LEVEL_ALL
Print everything.
Definition: log.h:116
ns3::LogComponentEnable
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:361
ns3::LogComponentEnableAll
void LogComponentEnableAll(enum LogLevel level)
Enable the logging output for all registered log components.
Definition: log.cc:385
ns3::Ipv4Address
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:41
ns3::RipNg::POISON_REVERSE
@ POISON_REVERSE
Poison Reverse Split Horizon.
Definition: ripng.h:210
ns3::Ipv4StaticRouting::SetDefaultRoute
void SetDefaultRoute(Ipv4Address nextHop, uint32_t interface, uint32_t metric=0)
Add a default route to the static routing table.
Definition: ipv4-static-routing.cc:122
ns3::RipNg::NO_SPLIT_HORIZON
@ NO_SPLIT_HORIZON
No Split Horizon.
Definition: ripng.h:208
ns3::RipHelper::SetInterfaceMetric
void SetInterfaceMetric(Ptr< Node > node, uint32_t interface, uint8_t metric)
Set a metric for an interface.
Definition: rip-helper.cc:176
ns3::Simulator::Schedule
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:556
ns3::EnumValue
Hold variables of type enum.
Definition: enum.h:55
ns3::CsmaHelper
build a set of CsmaNetDevice objects
Definition: csma-helper.h:47
first.nodes
nodes
Definition: first.py:32
ns3::Ipv4AddressHelper::SetBase
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Definition: ipv4-address-helper.cc:64
ns3::Ipv4
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:77
ns3::Ptr< Node >
ns3::Ipv4ListRoutingHelper::Add
void Add(const Ipv4RoutingHelper &routing, int16_t priority)
Definition: ipv4-list-routing-helper.cc:55
ns3::Simulator::Stop
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:180
ns3::V4PingHelper
Create a IPv4 ping application and associate it to a node.
Definition: v4ping-helper.h:38
ns3::InternetStackHelper::Install
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
Definition: internet-stack-helper.cc:366
NS_LOG_INFO
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
ns3::AsciiTraceHelper::CreateFileStream
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.
Definition: trace-helper.cc:191
ns3::MilliSeconds
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1297
ns3::Ipv4InterfaceContainer
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Definition: ipv4-interface-container.h:55
verbose
bool verbose
Definition: openflow-switch.cc:50
ns3::Time
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:104
ns3::InternetStackHelper::SetIpv6StackInstall
void SetIpv6StackInstall(bool enable)
Enable/disable IPv6 stack install.
Definition: internet-stack-helper.cc:194
second.cmd
cmd
Definition: second.py:35
ns3::Simulator::Run
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
ns3::AsciiTraceHelper
Manage ASCII trace files for device models.
Definition: trace-helper.h:163
ns3::Ipv4::GetRoutingProtocol
virtual Ptr< Ipv4RoutingProtocol > GetRoutingProtocol(void) const =0
Get the routing protocol to be used by this Ipv4 stack.
ns3::LOG_PREFIX_TIME
@ LOG_PREFIX_TIME
Prefix all trace prints with simulation time.
Definition: log.h:119
ns3::RipNg::SPLIT_HORIZON
@ SPLIT_HORIZON
Split Horizon.
Definition: ripng.h:209
packetSize
static const uint32_t packetSize
Definition: wifi-power-adaptation-distance.cc:113
ns3::ApplicationContainer::Start
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter.
Definition: application-container.cc:87
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1289
ns3::InternetStackHelper::SetRoutingHelper
void SetRoutingHelper(const Ipv4RoutingHelper &routing)
Definition: internet-stack-helper.cc:175
ns3::Ipv4AddressHelper::Assign
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
Definition: ipv4-address-helper.cc:135
ns3::Simulator::Destroy
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
ns3::LogLevel
LogLevel
Logging severity classes and levels.
Definition: log.h:94
ns3::ApplicationContainer
holds a vector of ns3::Application pointers.
Definition: application-container.h:43
ns3::TimeValue
AttributeValue implementation for Time.
Definition: nstime.h:1353
ns3::NodeContainer
keep track of a set of node pointers.
Definition: node-container.h:39
ns3::Ipv4Mask
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:256
ns3::UintegerValue
Hold an unsigned integer type.
Definition: uinteger.h:44
ns3::Config::SetDefault
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:849
TearDownLink
void TearDownLink(Ptr< Node > nodeA, Ptr< Node > nodeB, uint32_t interfaceA, uint32_t interfaceB)
Definition: rip-simple-network.cc:66
ns3::InternetStackHelper
aggregate IP/TCP/UDP functionality to existing Nodes.
Definition: internet-stack-helper.h:88
ns3::RipHelper::ExcludeInterface
void ExcludeInterface(Ptr< Node > node, uint32_t interface)
Exclude an interface from RIP protocol.
Definition: rip-helper.cc:159
ns3::Names::Add
static void Add(std::string name, Ptr< Object > object)
Add the association between the string "name" and the Ptr<Object> obj.
Definition: names.cc:768
second.csma
csma
Definition: second.py:63