CNNNNN

Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

Mahaveer Swami Institute of Technology

Computer Networks
Batch
(2019-2023)

Submitted To: Ms. Savita


Submitted By: Kunal Garg
Roll No: 00155102719
B.Tech. (CSE)
6th Semester (3rd Year)
INDEX
S. No. Topic Date Sign.

1. Introduction about discrete events 04-03-2022


simulation & its tools.

2. Installation of NS3 on Linux OS. 11-03-2022

3. Run Hello Simulator NS3. 25-03-2022

4. Wap in NS3 to connect two nodes. 01-04-2022

5. Wap in NS3 to implement a bus 08-04-2022


topology.

6. Installation & configuration of NetAnim. 22-04-2022

7. Conduct a simulation in Network 29-04-2022


Animation (NetAnim).

8. Wap to show the connection of 2 nodes 06-05-2022


& 4 routers such that the extreme nodes
act as client & server.
Experiment 1: Introduction about discrete events simulation & its
tools.
System:
A collection of entities that act and interact together toward the accomplishment of
some logical end.

Discrete system:
State variables change instantaneously at separated point in time, e.g., a bank, since state
variables - number of customers, change only when a customer arrives or when a
customer finishes being served and departs.

Continuous system:
State variables change continuously with respect to time, e.g., airplane moving through
the air, since state variables - position and velocity change continuously with respect to
time.

System

Experiment with Experiment with a


actual system model of actual system

Physical Model Mathematical


Model

Analytical
Solution Simulation

Fig 1: Ways to study the system.


System Model

Deterministic Stochastic

Static Dynamic Static Dynamic


Monte Carlo Simulation

Continuous Discrete Continuous Discrete

Fig 2: Model Taxonomy


Why Simulation?

1. Many systems are overly complex, precluding the possibility of analytical solution.

2. The analytical solutions are extraordinarily complex, requiring vast computing


resources.

3. Thus, such systems should be studied by means of simulation numerically exercising


the model for inputs in question to see how they affect the output measures of
performance.

Simulation is the process of designing a model of a real system and conducting


experiments with this model for the purpose either of understanding the behaviour of
the system or of evaluating various strategies (within the limits imposed by a criterion or
set of criteria) for the operation of a system.

What is Discrete-Event Simulation (DES)

A discrete-event simulation
Models a system whose state may change only at discrete point models a system whose
state may change only at discrete point in time

System:
It is composed of objects called entities that have certain properties called attributes.

State:
A collection of attributes or state variables that represent the entities of the system.
Event:
An instantaneous occurrence in time that may alter the state of the system.

• Discrete event simulation is stochastic, dynamic, & discrete.


• Stochastic = Probabilistic
- Inter arrival times & service times are random variables.
- Have cumulative distribution functions.
• Discrete = Instantaneous events are separated by intervals
- The state variables change instantaneously at separate points in time.
o The system can change at only a countable number of points in time.
- These points in time are the ones at which an event occurs.
• Dynamic = Changes over time
- Simulation clock
o Keep track of the current value of simulated time as the simulation
proceeds.
- A mechanism to advance simulated time from one value to another.
o Next event time advance
Experiment 2: Installation of NS3 on Linus OS.
Introduction
In this lab, we will be using the Network Simulator, NS3, available from www.nsnam.org.
NS3 is a powerful program, however we will only be looking at some basic features. NS3
simulations are built in C++.

Following are the basic steps which must be followed for installing NS3:

1. Install prerequisite packages.


2. Download ns3 codes.
3. Build ns3.
4. Validate ns3!
Prerequisite packages for Linux are as follows:

1. Minimal requirements for Python: gcc g++ python


2. Debugging and GNU Scientific Library (GSL) support: gdbpython-dev
3. valgrind gsl-bin libgsl0-dev libgsl0ldbl Network Simulation Cradle (nsc): flex bison
Reading pcap packet traces: tcpdump

4. Database support for statistics framework: sqlite sqlite3


5. Xml-based version of the config store: libxml2
6. A GTK-based configuration system: libgtk2.0-0
7. Experimental with virtual machines and ns-3: vtun lxc

Detail steps are as follows:


1. sudo apt-get update / dnf update.
2. sudo apt-get upgrade / dnf upgrade.
3. Once ubuntu/fedora is installed run following command opening the
terminal(ctrl+alt+T) window.
4. To install prerequisites dependency packages- Type the following command in terminal
window.
sudo apt-get install gcc g++ python python-dev mercurial bzr gdb valgrind gsl-bin
libgsl0-dev libgsl23 flex bison tcpdump sqlite sqlite3 libsqlite3-dev libxml2 libxml2-dev
libgtk2.0-0 libgtk2.0-dev uncrustify doxygen graphviz imagemagick texlive texlive-latex-
extra texlive-generic-extra texlive-generic-recommended texinfo dia texlive texlive-latex-
extra texlive-extra-utils texlive-generic-recommended texi2html python-pygraphviz
python-kiwi python-pygccxml.
5. After downloading NS3 on the drive, extract all the files in the NS3 folder, which you
have created.
6. Then you can find build.py along with other files in NS3 folder.
Then to build the examples in ns-3 run:
. /build.py --enable-examples – enable-tests.

If the build is successful, then it will give


output "Build finished successfully".

7. Now run the following command on the terminal window, to configure with waf (build
tool) ./waf -d debug --enable-examples --enable-tests configure To build with waf

(optional)
. /waf

8. To test everything all right run the following command on the terminal window, ./test.py
If the tests are ok the installation is done.
9. Now after installing ns3 and testing it run some programs first to be ns3 user: make
sure you are in directory where waf script is available then run.
OUTPUT:
Experiment 3: Run Hello- Simulator in NS3

Here we will run the Sample Code to ensure the Working of the NS3 Development
Environment. There are some bunch of example files one of which is hello-simulator.cc

Code:

#include "ns3/core-module.h"
using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("HelloSimulator");
int
main (int argc, char *argv[])
{
NS_LOG_UNCOND ("Hello Simulator");
}
OUTPUT:
Experiment 4: WAP in NS3 to connect two nodes.

#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
#include "ns3/netanim-module.h"

using namespace ns3;


int
main (int argc, char *argv[])
{
CommandLine cmd;
cmd.Parse (argc, argv);

//Take Logs
Time::SetResolution (Time::NS);
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);

//Take n number of Nodes


NodeContainer nodes;
nodes.Create (2);
//Choose your Technology to Communicate PointToPointHelper
pointToPoint; //P2P Channel pointToPoint.SetDeviceAttribute
("DataRate", StringValue ("5Mbps"));
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));

//Install Technology on Computers


NetDeviceContainer devices;
devices = pointToPoint.Install (nodes);
//Asking to Follow Rules
InternetStackHelper stack;
stack.Install (nodes);

//Assign IP Address to Communicate


Ipv4AddressHelper address;
address.SetBase ("10.1.1.0", "255.255.255.0"); //("IP Address", "Subnet Mask")
Ipv4InterfaceContainer interfaces = address.Assign (devices);

//Create a X type of server on port X


UdpEchoServerHelper echoServer (19);

//Install server on a node then start and stop the server


ApplicationContainer serverApps = echoServer.Install (nodes.Get (1));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));

//Create a X type of Client and set its attributes


UdpEchoClientHelper echoClient (interfaces.GetAddress (1),
19); echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
echoClient.SetAttribute ("PacketSize", UintegerValue (1024));

//Install the server then Start and Stop it


ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
OUTPUT:
Experiment 5: WAP in NS3 to implement a bus topology

#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/csma-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
#include "ns3/ipv4-global-routing-helper.h"

// Default Network Topology

//
// 10.1.1.0
// n0 -------------- n1 n2 n3 n4
// point-to-point | | | |
// ================
// LAN 10.1.2.0

using namespace ns3;


int main (int argc, char *argv[])
{
bool verbose = true;
uint32_t nCsma = 3;
CommandLine cmd;

cmd.AddValue ("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma);


cmd.AddValue ("verbose", "Tell echo applications to log if true", verbose);
cmd.Parse (argc,argv);
if (verbose)
{
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
}
nCsma = nCsma == 0 ? 1 : nCsma;
NodeContainer p2pNodes;
p2pNodes.Create (2);

NodeContainer csmaNodes;
csmaNodes.Add (p2pNodes.Get (1));
csmaNodes.Create (nCsma);
PointToPointHelper pointToPoint;

pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));


pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
NetDeviceContainer p2pDevices;
p2pDevices = pointToPoint.Install (p2pNodes);
CsmaHelper csma;

csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));


csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));
NetDeviceContainer csmaDevices;

csmaDevices = csma.Install (csmaNodes);


InternetStackHelper stack;
stack.Install (p2pNodes.Get (0));
stack.Install (csmaNodes);
Ipv4AddressHelper address;

address.SetBase ("10.1.1.0", "255.255.255.0");


Ipv4InterfaceContainer p2pInterfaces;
p2pInterfaces = address.Assign (p2pDevices);

address.SetBase ("10.1.2.0", "255.255.255.0");


Ipv4InterfaceContainer csmaInterfaces;
csmaInterfaces = address.Assign (csmaDevices);

UdpEchoServerHelper echoServer (9);


ApplicationContainer serverApps = echoServer.Install (csmaNodes.Get (nCsma));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
UdpEchoClientHelper echoClient (csmaInterfaces.GetAddress (nCsma),
9); echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
echoClient.SetAttribute ("PacketSize", UintegerValue (1024));

ApplicationContainer clientApps = echoClient.Install (p2pNodes.Get (0));


clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
pointToPoint.EnablePcapAll ("second"); csma.EnablePcap
("second", csmaDevices.Get (1), true);
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
OUTPUT:
Experiment 6: Installation and configuration of NetAnim

Installing NetAnim

The website: http://www.nsnam.org/wiki/index.php/NetAnim


1. Install Mercurial: apt−get/dnf install mercurial.

2. Install QT4 development package:


apt−get/dnf install qt4−dev−tools
3. You can use Synaptic too, to install both the above packages.
4. Download NetAnim: hg clone http :// code .nsnam. org/netanim

5. Build NetAnim:
cd netanim
make clean qmake NetAnim. pro make

Compiling code with NetAnim


So you will have to make the following changes to the code, in order to view the animation
on NetAnim.

#include " ... "

#include "ns3/netanim−module .h"


int main ( int argc , char ∗argv [ ] )
//1 Include. . .

std : : string animFile = "somename. xml"; //2 Name of f i l e for animation

...

...

...

AnimationInterface anim ( animFile ); //3 Animation interface


Simulator : : Run ();

Simulator : : Destroy ();

return (0);

}
To run the code:

1. Move the waf , waf.bat , wscript and wutils.py les in to the scratch folder (~/ns-
allinone-3.24/ns-3.24/scratch/).

2. Move the example code to the scratch folder and make the changes required for
NetAnim, as shown above.
3. Now cd to the scratch folder (cd ~/ns-allinone-3.24/ns-3.24/scratch/).
4. Run the code using the command:
./ waf −−run <filename>

Note: < filename> should not contain the extension .cc

To visualize on NetAnim:
1. cd to the netanim folder (cd ~/netanim/).
2. Run Netanim:
./NetAnim

Include the .xml le generated in the ns-3.24 folder (~/ns-allinone-3.17/ns3.24/).


OUTPUT:
Experiment 7: Conduct a Simulation in Network Animation
(NetAnim)

#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
#include "ns3/netanim-module.h"

using namespace ns3;


int main (int argc, char *argv[])
{
CommandLine cmd;
cmd.Parse (argc, argv);

//Take Logs
Time::SetResolution (Time::NS);

LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);


LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);

//Take n number of Nodes


NodeContainer nodes;
nodes.Create (2);
//Choose your Technology to Communicate
PointToPointHelper pointToPoint; //P2P Channel

pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));


pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
//Install Technology on Computers
NetDeviceContainer devices;
devices = pointToPoint.Install (nodes);

//Asking to Follow Rules


InternetStackHelper stack;
stack.Install (nodes);

//Assign IP Address to Communicate


Ipv4AddressHelper address;

address.SetBase ("10.1.1.0", "255.255.255.0"); //("IP Address", "Subnet Mask")


Ipv4InterfaceContainer interfaces = address.Assign (devices);

//Create a X type of server on port X


UdpEchoServerHelper echoServer (19);

//Install server on a node then start and stop the server


ApplicationContainer serverApps = echoServer.Install (nodes.Get (1));

serverApps.Start (Seconds (1.0));


serverApps.Stop (Seconds (10.0));

//Create a X type of Client and set its attributes


UdpEchoClientHelper echoClient (interfaces.GetAddress (1),
19); echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
echoClient.SetAttribute ("PacketSize", UintegerValue (1024));

//Install the server then Start and Stop it


ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
//Run the Simulation
AnimationInterface anim ("anim1.xml");
anim.SetConstantPosition(nodes.Get(0),1.0,2.0);
anim.SetConstantPosition(nodes.Get(1),2.0,3.0);
Simulator::Run ();
Simulator::Destroy ();
return (0);
}
OUTPUT:
Experiment 8: WAP to show the connection of 2 nodes and 4
router such that the extremes nodes act as client and server.

Code:

#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/csma-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
#include "ns3/ipv4-global-routing-helper.h"
using namespace ns3;

int main(int argc, char * argv[])

bool verbose = true;


if(verbose)
{
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);

NodeContainer host, router, host1;


host.Create(2);
router.Create(4);
NodeContainer subnet1;

subnet1.Add(host.Get(0));
subnet1.Add(router.Get(0));

PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute("DataRate", StringValue ("5Mbps"));
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
NetDeviceContainer subnet1Devices;
subnet1Devices = pointToPoint.Install(subnet1);

InternetStackHelper stack;
stack.Install(router);
stack.Install(host);

Ipv4AddressHelper address1, address2, address3, address4, address5, address6;


address1.SetBase ("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer subnet1Interfaces;
subnet1Interfaces = address1.Assign(subnet1Devices);

NodeContainer subnet2;
subnet2.Add(router.Get(0));
subnet2.Add(router.Get(1));
NetDeviceContainer subnet2Devices;
subnet2Devices = pointToPoint.Install(subnet2);
address2.SetBase ("10.1.2.0", "255.255.255.0");
Ipv4InterfaceContainer subnet2Interfaces;
subnet2Interfaces = address2.Assign(subnet2Devices);

NodeContainer subnet3;
subnet3.Add(router.Get(1));
subnet3.Add(router.Get(2));
NetDeviceContainer subnet3Devices;
subnet3Devices = pointToPoint.Install(subnet3);

address3.SetBase ("10.1.3.0", "255.255.255.0");


Ipv4InterfaceContainer subnet3Interfaces;
subnet3Interfaces = address3.Assign(subnet3Devices);

NodeContainer subnet4;
subnet4.Add(router.Get(1));
subnet4.Add(router.Get(3));
NetDeviceContainer subnet4Devices;
subnet4Devices = pointToPoint.Install(subnet4);

address4.SetBase ("10.1.4.0", "255.255.255.0");


Ipv4InterfaceContainer subnet4Interfaces;
subnet4Interfaces = address4.Assign(subnet4Devices);

NodeContainer subnet5;
subnet5.Add(router.Get(2));
subnet5.Add(router.Get(1));
NetDeviceContainer subnet5Devices;
subnet5Devices = pointToPoint.Install(subnet5);

address5.SetBase ("10.1.5.0", "255.255.255.0");


Ipv4InterfaceContainer subnet5Interfaces;
subnet5Interfaces = address5.Assign(subnet5Devices);
UdpEchoServerHelper echoServer(9);
ApplicationContainer serverApps = echoServer.Install(subnet5.Get(1));
serverApps.Start(Seconds(1.0));
serverApps.Stop(Seconds(10.0));
UdpEchoClientHelper echoClient (subnet5Interfaces.GetAddress (1),
9); echoClient.SetAttribute ("MaxPackets", UintegerValue(3));
echoClient.SetAttribute("Interval", TimeValue (Seconds (1.0)));
echoClient.SetAttribute ("PacketSize", UintegerValue (1024));

ApplicationContainer clientApps = echoClient.Install (subnet1.Get (0));


clientApps.Start (Seconds (1.0));
clientApps.Stop (Seconds (10.0));

Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
Simulator::Run();
Simulator::Destroy();
return(0);
}
OUTPUT:

29

You might also like