A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ripng-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) 2014 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/applications-module.h"
59 #include "ns3/ipv6-static-routing-helper.h"
60 #include "ns3/ipv6-routing-table-entry.h"
61 
62 using namespace ns3;
63 
64 NS_LOG_COMPONENT_DEFINE ("RipNgSimpleRouting");
65 
66 void TearDownLink (Ptr<Node> nodeA, Ptr<Node> nodeB, uint32_t interfaceA, uint32_t interfaceB)
67 {
68  nodeA->GetObject<Ipv6> ()->SetDown (interfaceA);
69  nodeB->GetObject<Ipv6> ()->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;
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  {
88  LogComponentEnable ("RipNgSimpleRouting", LOG_LEVEL_INFO);
90  LogComponentEnable ("Icmpv6L4Protocol", LOG_LEVEL_INFO);
91  LogComponentEnable ("Ipv6Interface", LOG_LEVEL_ALL);
92  LogComponentEnable ("Icmpv6L4Protocol", LOG_LEVEL_ALL);
93  LogComponentEnable ("NdiscCache", LOG_LEVEL_ALL);
94  LogComponentEnable ("Ping6Application", LOG_LEVEL_ALL);
95  }
96 
97  if (showPings)
98  {
99  LogComponentEnable ("Ping6Application", LOG_LEVEL_INFO);
100  }
101 
102  if (SplitHorizon == "NoSplitHorizon")
103  {
104  Config::SetDefault ("ns3::RipNg::SplitHorizon", EnumValue (RipNg::NO_SPLIT_HORIZON));
105  }
106  else if (SplitHorizon == "SplitHorizon")
107  {
108  Config::SetDefault ("ns3::RipNg::SplitHorizon", EnumValue (RipNg::SPLIT_HORIZON));
109  }
110  else
111  {
112  Config::SetDefault ("ns3::RipNg::SplitHorizon", EnumValue (RipNg::POISON_REVERSE));
113  }
114 
115  NS_LOG_INFO ("Create nodes.");
116  Ptr<Node> src = CreateObject<Node> ();
117  Names::Add ("SrcNode", src);
118  Ptr<Node> dst = CreateObject<Node> ();
119  Names::Add ("DstNode", dst);
120  Ptr<Node> a = CreateObject<Node> ();
121  Names::Add ("RouterA", a);
122  Ptr<Node> b = CreateObject<Node> ();
123  Names::Add ("RouterB", b);
124  Ptr<Node> c = CreateObject<Node> ();
125  Names::Add ("RouterC", c);
126  Ptr<Node> d = CreateObject<Node> ();
127  Names::Add ("RouterD", d);
128  NodeContainer net1 (src, a);
129  NodeContainer net2 (a, b);
130  NodeContainer net3 (a, c);
131  NodeContainer net4 (b, c);
132  NodeContainer net5 (c, d);
133  NodeContainer net6 (b, d);
134  NodeContainer net7 (d, dst);
135  NodeContainer routers (a, b, c, d);
136  NodeContainer nodes (src, dst);
137 
138 
139  NS_LOG_INFO ("Create channels.");
140  CsmaHelper csma;
141  csma.SetChannelAttribute ("DataRate", DataRateValue (5000000));
142  csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
143  NetDeviceContainer ndc1 = csma.Install (net1);
144  NetDeviceContainer ndc2 = csma.Install (net2);
145  NetDeviceContainer ndc3 = csma.Install (net3);
146  NetDeviceContainer ndc4 = csma.Install (net4);
147  NetDeviceContainer ndc5 = csma.Install (net5);
148  NetDeviceContainer ndc6 = csma.Install (net6);
149  NetDeviceContainer ndc7 = csma.Install (net7);
150 
151  NS_LOG_INFO ("Create IPv6 and routing");
152  RipNgHelper ripNgRouting;
153 
154  // Rule of thumb:
155  // Interfaces are added sequentially, starting from 0
156  // However, interface 0 is always the loopback...
157  ripNgRouting.ExcludeInterface (a, 1);
158  ripNgRouting.ExcludeInterface (d, 3);
159 
160  ripNgRouting.SetInterfaceMetric (c, 3, 10);
161  ripNgRouting.SetInterfaceMetric (d, 1, 10);
162 
163  Ipv6ListRoutingHelper listRH;
164  listRH.Add (ripNgRouting, 0);
165 
166  InternetStackHelper internetv6;
167  internetv6.SetIpv4StackInstall (false);
168  internetv6.SetRoutingHelper (listRH);
169  internetv6.Install (routers);
170 
171  InternetStackHelper internetv6Nodes;
172  internetv6Nodes.SetIpv4StackInstall (false);
173  internetv6Nodes.Install (nodes);
174 
175  // Assign addresses.
176  // The source and destination networks have global addresses
177  // The "core" network just needs link-local addresses for routing.
178  // We assign global addresses to the routers as well to receive
179  // ICMPv6 errors.
180  NS_LOG_INFO ("Assign IPv6 Addresses.");
181  Ipv6AddressHelper ipv6;
182 
183  ipv6.SetBase (Ipv6Address ("2001:1::"), Ipv6Prefix (64));
184  Ipv6InterfaceContainer iic1 = ipv6.Assign (ndc1);
185  iic1.SetForwarding (1, true);
186  iic1.SetDefaultRouteInAllNodes (1);
187 
188  ipv6.SetBase (Ipv6Address ("2001:0:1::"), Ipv6Prefix (64));
189  Ipv6InterfaceContainer iic2 = ipv6.Assign (ndc2);
190  iic2.SetForwarding (0, true);
191  iic2.SetForwarding (1, true);
192 
193  ipv6.SetBase (Ipv6Address ("2001:0:2::"), Ipv6Prefix (64));
194  Ipv6InterfaceContainer iic3 = ipv6.Assign (ndc3);
195  iic3.SetForwarding (0, true);
196  iic3.SetForwarding (1, true);
197 
198  ipv6.SetBase (Ipv6Address ("2001:0:3::"), Ipv6Prefix (64));
199  Ipv6InterfaceContainer iic4 = ipv6.Assign (ndc4);
200  iic4.SetForwarding (0, true);
201  iic4.SetForwarding (1, true);
202 
203  ipv6.SetBase (Ipv6Address ("2001:0:4::"), Ipv6Prefix (64));
204  Ipv6InterfaceContainer iic5 = ipv6.Assign (ndc5);
205  iic5.SetForwarding (0, true);
206  iic5.SetForwarding (1, true);
207 
208  ipv6.SetBase (Ipv6Address ("2001:0:5::"), Ipv6Prefix (64));
209  Ipv6InterfaceContainer iic6 = ipv6.Assign (ndc6);
210  iic6.SetForwarding (0, true);
211  iic6.SetForwarding (1, true);
212 
213  ipv6.SetBase (Ipv6Address ("2001:2::"), Ipv6Prefix (64));
214  Ipv6InterfaceContainer iic7 = ipv6.Assign (ndc7);
215  iic7.SetForwarding (0, true);
216  iic7.SetDefaultRouteInAllNodes (0);
217 
218  if (printRoutingTables)
219  {
220  RipNgHelper routingHelper;
221 
222  Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper> (&std::cout);
223 
224  routingHelper.PrintRoutingTableAt (Seconds (30.0), a, routingStream);
225  routingHelper.PrintRoutingTableAt (Seconds (30.0), b, routingStream);
226  routingHelper.PrintRoutingTableAt (Seconds (30.0), c, routingStream);
227  routingHelper.PrintRoutingTableAt (Seconds (30.0), d, routingStream);
228 
229  routingHelper.PrintRoutingTableAt (Seconds (60.0), a, routingStream);
230  routingHelper.PrintRoutingTableAt (Seconds (60.0), b, routingStream);
231  routingHelper.PrintRoutingTableAt (Seconds (60.0), c, routingStream);
232  routingHelper.PrintRoutingTableAt (Seconds (60.0), d, routingStream);
233 
234  routingHelper.PrintRoutingTableAt (Seconds (90.0), a, routingStream);
235  routingHelper.PrintRoutingTableAt (Seconds (90.0), b, routingStream);
236  routingHelper.PrintRoutingTableAt (Seconds (90.0), c, routingStream);
237  routingHelper.PrintRoutingTableAt (Seconds (90.0), d, routingStream);
238  }
239 
240  NS_LOG_INFO ("Create Applications.");
241  uint32_t packetSize = 1024;
242  uint32_t maxPacketCount = 100;
243  Time interPacketInterval = Seconds (1.0);
244  Ping6Helper ping6;
245 
246  ping6.SetLocal (iic1.GetAddress (0, 1));
247  ping6.SetRemote (iic7.GetAddress (1, 1));
248  ping6.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount));
249  ping6.SetAttribute ("Interval", TimeValue (interPacketInterval));
250  ping6.SetAttribute ("PacketSize", UintegerValue (packetSize));
251  ApplicationContainer apps = ping6.Install (src);
252  apps.Start (Seconds (1.0));
253  apps.Stop (Seconds (110.0));
254 
255  AsciiTraceHelper ascii;
256  csma.EnableAsciiAll (ascii.CreateFileStream ("ripng-simple-routing.tr"));
257  csma.EnablePcapAll ("ripng-simple-routing", true);
258 
259  Simulator::Schedule (Seconds (40), &TearDownLink, b, d, 3, 2);
260 
261  /* Now, do the actual simulation. */
262  NS_LOG_INFO ("Run Simulation.");
263  Simulator::Stop (Seconds (120));
264  Simulator::Run ();
266  NS_LOG_INFO ("Done.");
267 }
268 
holds a vector of ns3::Application pointers.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:79
Manage ASCII trace files for device models.
Definition: trace-helper.h:141
void SetChannelAttribute(std::string n1, const AttributeValue &v1)
Definition: csma-helper.cc:69
Keep track of a set of IPv6 interfaces.
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition: ipv6.h:80
void SetDefaultRouteInAllNodes(uint32_t router)
Set the default route for all the devices (except the router itself).
ApplicationContainer Install(NodeContainer c)
Install the application in Nodes.
Definition: ping6-helper.cc:50
void ExcludeInterface(Ptr< Node > node, uint32_t interface)
Exclude an interface from RIPng protocol.
static void Run(void)
Run the simulation until one of:
Definition: simulator.cc:157
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
aggregate IP/TCP/UDP functionality to existing Nodes.
void SetBase(Ipv6Address network, Ipv6Prefix prefix, Ipv6Address base=Ipv6Address("::1"))
Set the base network number, network prefix, and base interface ID.
Helper class that adds RIPng routing to nodes.
Definition: ripng-helper.h:38
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::CsmaChannel with the attributes configured by CsmaHelper::SetChannelAttri...
Definition: csma-helper.cc:215
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:223
void SetAttribute(std::string name, const AttributeValue &value)
Set some attributes.
Definition: ping6-helper.cc:45
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. ...
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event to expire at the relative time "time" is reached.
Definition: simulator.h:825
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.
static void Add(std::string name, Ptr< Object > object)
Add the association between the string "name" and the Ptr obj.
Definition: names.cc:615
Ipv6InterfaceContainer Assign(const NetDeviceContainer &c)
Allocate an Ipv6InterfaceContainer with auto-assigned addresses.
tuple nodes
Definition: first.py:25
void EnablePcapAll(std::string prefix, bool promiscuous=false)
Enable pcap output on each device (which is of the appropriate type) in the set of all nodes created ...
hold variables of type 'enum'
Definition: enum.h:37
void SetLocal(Ipv6Address ip)
Set the local IPv6 address.
Definition: ping6-helper.cc:35
hold objects of type ns3::Time
Definition: nstime.h:1008
Hold an unsigned integer type.
Definition: uinteger.h:46
void SetIpv4StackInstall(bool enable)
Enable/disable IPv4 stack install.
void SetInterfaceMetric(Ptr< Node > node, uint32_t interface, uint8_t metric)
Set a metric for an interface.
holds a vector of ns3::NetDevice pointers
void PrintRoutingTableAt(Time printTime, Ptr< Node > node, Ptr< OutputStreamWrapper > stream) const
prints the routing tables of a node at a particular time.
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
Parse command-line arguments.
Definition: command-line.h:177
static void Destroy(void)
Every event scheduled by the Simulator::insertAtDestroy method is invoked.
Definition: simulator.cc:121
int main(int argc, char **argv)
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:667
keep track of a set of node pointers.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
void SetRemote(Ipv6Address ip)
Set the remote IPv6 address.
Definition: ping6-helper.cc:40
build a set of CsmaNetDevice objects
Definition: csma-helper.h:46
Helper class to auto-assign global IPv6 unicast addresses.
void Add(const Ipv6RoutingHelper &routing, int16_t priority)
Describes an IPv6 address.
Definition: ipv6-address.h:46
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
hold objects of type ns3::DataRate
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:435
static void Stop(void)
If an event invokes this method, it will be the last event scheduled by the Simulator::run method bef...
Definition: simulator.cc:165
void TearDownLink(Ptr< Node > nodeA, Ptr< Node > nodeB, uint32_t interfaceA, uint32_t interfaceB)
Describes an IPv6 prefix.
Definition: ipv6-address.h:387
Helper class that adds ns3::Ipv6ListRouting objects.
void Parse(int argc, char *argv[])
Parse the program arguments.
void EnableAsciiAll(std::string prefix)
Enable ascii trace output on each device (which is of the appropriate type) in the set of all nodes c...
Ping6 application helper.
Definition: ping6-helper.h:39
Ptr< T > GetObject(void) const
Definition: object.h:362
void SetRoutingHelper(const Ipv4RoutingHelper &routing)
Ipv6Address GetAddress(uint32_t i, uint32_t j) const
Get the address for the specified index.
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:318
bool verbose