Assignment 3 PDF
Assignment 3 PDF
Introduction
The network layer is concerned with getting packets from the source all the way to the destination.
Getting to the destination may require making many hops at intermediate routers along the way. This
function clearly contrasts with that of the data link layer, which has the more modest goal of just moving
frames from one end of a wire to the other.
To achieve its goals, the network layer must know about the topology of the communication subnet (i.e.,
the set of all routers) and choose appropriate paths through it. It must also take care to choose routes to
avoid overloading some of the communication lines and routers while leaving others idle. It is up to the
network layer to deal with them.
A router in the network needs to be able to look at a packets destination address and then determine
the output port which is the best choice to get the packet to that destination. The router makes this
decision by consulting a forwarding table. Routing algorithms are required to build the routing tables and
hence forwarding tables.
The basic problem of routing is to find the lowest-cost path between any two nodes, where the cost of a
path equals the sum of the costs of all the edges that make up the path.
Routing is achieved in most practical networks by running routing protocols among the nodes. The
protocols provide a distributed, dynamic way to solve the problem of finding the lowest-cost path in the
presence of link and node failures, and changing edge costs. In this exercise you will see some NS3
features to simulate routing.
Data Networks
Dr. M. R. Pakravan
8
F
First thing you should know that NS3 code is in C++ syntax. In fact NS3 is a library for C++ .It consists of
several modules for different parts of network simulation. When you want to do a simulation you include
the modules you need and then you write your code.
For this part add these libraries:
#include
#include
#include
#include
#include
#include
#include
#include
#include
"ns3/core-module.h"
"ns3/network-module.h"
"ns3/internet-module.h"
"ns3/point-to-point-module.h"
"ns3/netanim-module.h"
"ns3/applications-module.h"
"ns3/animation-interface.h"
"ns3/point-to-point-layout-module.h"
"ns3/ipv4-static-routing-helper.h"
Data Networks
Dr. M. R. Pakravan
"ns3/ipv4-list-routing-helper.h"
"ns3/ipv4-global-routing-helper.h"
"ns3/flow-monitor.h"
"ns3/flow-monitor-helper.h"
"ns3/flow-monitor-module.h"
<iostream>
<fstream>
<vector>
<string>
Next line will show that this project is implemented in a C++ namespace called ns3. using statement
introduces the ns-3 namespace into the current (global) declarative region. This is a fancy way of
saying that after this declaration, you will not have to type ns3:: scope resolution operator before all of
the ns-3 code in order to use it.
using namespace ns3;
The next line enables you in showing some messages while running the code:
NS_LOG_COMPONENT_DEFINE ("Lab3_part1");
Now you can start the main function of your program.
int main (int argc, char *argv[])
{
//add your codes here
}
First we declare some parameters for our simulation. You should write all other codes on your main
function.
uint32_t PacketSize = 512; // bytes
std::string DataRate ("1Mbps");
uint16_t num_Nodes = 10;
uint16_t UDPport = 9;
bool tracing = false;
CommandLine object allows the user to override any of the defaults at run-time:
Data Networks
Dr. M. R. Pakravan
Now enable Packet Meta data for animator and the name of file for animation output:
ns3::PacketMetadata::Enable();
std::string animFile = "lab3_part1.xml" ;
Now lets create the network topology. Network nodes are stored in a container class called
NodeContainer.
NodeContainer nodes;
nodes.Create (num_Nodes);
Now you should group nodes to make links between them.
NodeContainer
NodeContainer
NodeContainer
NodeContainer
NodeContainer
NodeContainer
NodeContainer
NodeContainer
NodeContainer
NodeContainer
NodeContainer
NodeContainer
NodeContainer
NodeContainer
NodeContainer
AB
BC
CD
DE
EA
FH
GI
HJ
IF
JG
AF
BG
CH
DI
EJ
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
NodeContainer
NodeContainer
NodeContainer
NodeContainer
NodeContainer
NodeContainer
NodeContainer
NodeContainer
NodeContainer
NodeContainer
NodeContainer
NodeContainer
NodeContainer
NodeContainer
NodeContainer
(nodes.Get
(nodes.Get
(nodes.Get
(nodes.Get
(nodes.Get
(nodes.Get
(nodes.Get
(nodes.Get
(nodes.Get
(nodes.Get
(nodes.Get
(nodes.Get
(nodes.Get
(nodes.Get
(nodes.Get
(0),
(1),
(2),
(3),
(4),
(5),
(6),
(7),
(8),
(9),
(0),
(1),
(2),
(3),
(4),
nodes.Get
nodes.Get
nodes.Get
nodes.Get
nodes.Get
nodes.Get
nodes.Get
nodes.Get
nodes.Get
nodes.Get
nodes.Get
nodes.Get
nodes.Get
nodes.Get
nodes.Get
(1));
(2));
(3));
(4));
(0));
(7));
(8));
(9));
(5));
(6));
(5));
(6));
(7));
(8));
(9));
Data Networks
Dr. M. R. Pakravan
Data Networks
Dr. M. R. Pakravan
Data Networks
Dr. M. R. Pakravan
Where instead of source node and sink node above (0 and 6), you should insert the two last non-equal digits
of your student ID.
To Create a UDP packet sink to receive these packets use the following codes:
App = UDPsink.Install (SinkNode);
App.Start (Seconds (0.0));
App.Stop (Seconds (10.0));
Address Sink_Address(InetSocketAddress(iHD.GetAddress (1), UDPport));
To Create a UDP packet source to send these packets type the following codes:
OnOffHelper UDPsource ("ns3::UdpSocketFactory", Sink_Address);
UDPsource.SetAttribute ("OnTime",
StringValue("ns3::ConstantRandomVariable[Constant=1]"));
UDPsource.SetAttribute ("OffTime",
StringValue("ns3::ConstantRandomVariable[Constant=0]"));
App = UDPsource.Install(SourceNode);
App.Start (Seconds (1.0));
App.Stop (Seconds (10.0));
You should determine your nodes places for NetAnim. Do it with these codes:
AnimationInterface anim (animFile);
Ptr<Node> n = nodes.Get (0);
anim.SetConstantPosition (n, 100, 10);
n = nodes.Get (1);
anim.SetConstantPosition (n, 187, 73);
n = nodes.Get (2);
anim.SetConstantPosition (n, 154, 172);
n = nodes.Get (3);
anim.SetConstantPosition (n, 47, 172);
n = nodes.Get (4);
anim.SetConstantPosition (n, 14, 73);
n = nodes.Get (5);
anim.SetConstantPosition (n, 100, 56);
n = nodes.Get (6);
Data Networks
Dr. M. R. Pakravan
NS3 has two types of text output: Pcap files which are standard files in network programs. You can read
them with wireshark or winpcap. The second type is NS3s trace files that you can parse it yourself (in
MATLAB for example).
if (tracing == true)
{
AsciiTraceHelper ascii;
p2p.EnableAsciiAll (ascii.CreateFileStream ("Lab3_part1.tr"));
p2p.EnablePcap ("Lab3_part1");
}
To print routing tables you should add the following codes:
Ptr<OutputStreamWrapper> stream1 = Create<OutputStreamWrapper> ("Table2", std::ios::out);
Ipv4GlobalRoutingHelper helper2;
helper2.PrintRoutingTableAllAt(Seconds(2.0),stream1);
And the last thing to do is to define the simulation time and run the simulation. After everything is done,
we destroy the simulator object we have created, and then end the main function.
Simulator::Stop (Seconds (10.0));
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
To run the simulation save the code with .cc extension in scratch folder in ns3 directory. Then open a
terminal and cd to ns-allinone-3.x/ns-3.x/ and enter:
./waf run scratch/myfile (without .cc)
Do the simulation and compare the result with what calculated in the class.
Now try to change the metric of an arbitrary link, such that the path created by Dijksta's shortest path
aloghrithm changes. Again do the simulation and investigate how the path for sending packets has been
Data Networks
Dr. M. R. Pakravan
Data Networks
Dr. M. R. Pakravan
10
Due to the wireless nature of the topology, we cannot use the codes mentioned in the first part. First, we
should add these header files for further use:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
"ns3/core-module.h"
"ns3/network-module.h"
"ns3/internet-module.h"
"ns3/mobility-module.h"
"ns3/config-store-module.h"
"ns3/applications-module.h"
"ns3/wifi-module.h"
"ns3/internet-module.h"
"ns3/olsr-helper.h"
"ns3/dsdv-helper.h"
"ns3/aodv-helper.h"
"ns3/point-to-point-layout-module.h"
"ns3/ipv4-static-routing-helper.h"
"ns3/ipv4-list-routing-helper.h"
"ns3/ipv4-global-routing-helper.h"
"ns3/flow-monitor.h"
"ns3/flow-monitor-helper.h"
"ns3/flow-monitor-module.h"
"ns3/animation-interface.h"
"ns3/netanim-module.h"
<iostream>
<fstream>
<vector>
<string>
Data Networks
Dr. M. R. Pakravan
10
Now lets create the network topology, network nodes are stored in a container class called
NodeContainer:
NodeContainer Nodes;
Nodes.Create (num_Nodes);
The below set of helpers will help us to put together the wifi NICs we want, also some logging switches
are set here:
WifiHelper wifi;
if (verbose)
{
wifi.EnableLogComponents (); // Turn on all Wifi logging
}
Data Networks
Dr. M. R. Pakravan
11
Data Networks
Dr. M. R. Pakravan
12
Now we enable OLSR routing for the network. The procedure for AODV and DSDV is similar to OLSR.
OlsrHelper olsr;
Ipv4StaticRoutingHelper staticRouting;
Ipv4ListRoutingHelper list;
list.Add (staticRouting, 0);
list.Add (olsr, 10);
InternetStackHelper internet;
internet.SetRoutingHelper (list); // has effect on the next Install ()
internet.Install (Nodes);
Then we assign the IP addresses to the nodes:
Ipv4AddressHelper ipv4;
NS_LOG_INFO ("Assign IP Addresses.");
ipv4.SetBase ("192.168.1.0", "255.255.255.0");
Ipv4InterfaceContainer i;
i = ipv4.Assign (Devices);
Data Networks
Dr. M. R. Pakravan
13
And the last thing to do is to define the simulation time and run the simulation and after everything is done
you should destroy the simulator object as explained in the first part.
For this part of assignment, first run the simulation for each of the routing algorithms (OLSR, AODV and
DSDV). Explain what the parameters are in the tables of each algorithm. What are the differences between
the algorithms?
Second, check the routing table of node 0 and determine when the table converges and when a path
between node 0 and node 8 is established.
At last, set down node 6 at second
+ 40. In this case, the previous path will
10
be no more valid, and a new path should be established. Check the routing table of node 0 and determine
when a new path is created? (You should check the destination with IP address: 192.168.1.9 and see
which node is the interface).
Data Networks
Dr. M. R. Pakravan
14
Data Networks
Dr. M. R. Pakravan
15