Nitte Meenakshi Institute of Technology: Bachelor of Engineering in Computer Science and Engineering

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

NITTE MEENAKSHI INSTITUTE OF TECHNOLOGY

(AN AUTONOMOUS INSTITUTION, AFFILIATED TO VISVESVARAYA TECHNOLOGICAL UNIVERSITY, BELGAUM, APPROVED BY AICTE
& GOVT.OF KARNATAKA)

COURSE-IMPLEMENTATION REPORT
on
“NS-3 SIMULATION”
Submitted in partial fulfilment of the requirement for the award of Degree of
Bachelor of Engineering in
Computer Science and Engineering
Submitted by:
MEGHANA M 1NT15CS093
NEHARIKA K 1NT15CS100

Under the Guidance of


Mrs. Sharmila Sequeira
Assistant Professor,

Department of Computer Science and Engineering


(Accredited by NBA Tier-1)
2017-18

NITTE MEENAKSHI INSTITUTE OF TECHNOLOGY


(AN AUTONOMOUS INSTITUTION, AFFILIATED TO VISVESVARAYA TECHNOLOGICAL UNIVERSITY, BELGAUM
, APPROVED BY AICTE & GOVT.OF KARNATAKA)

Department of Computer Science and Engineering


(Accredited by NBA Tier-1)
ns-3 SIMULATION
ns-3 SIMULATION

TABLE OF CONTENTS

1. Introduction to ns-3…………………………… 2-3


2. ns-2 versus ns-3……………………………….. 4-5
3. Explanation of some Terms…………………...6- 8
4. Source Code…………………………………… 9-10
5. Result…………………………………………...11

Page 1
ns-3 SIMULATION

INTRODUCTION TO ns-3
ns-3 has been developed to provide an open, extensible network simulation platform,
for networking research and education. In brief, ns-3 provides models of how packet data
networks work and perform, and provides a simulation engine for users to conduct simulation
experiments. Some of the reasons to use ns-3 include to perform studies that are more difficult or
not possible to perform with real systems, to study system behaviour in a highly controlled,
reproducible environment, and to learn about how networks work. Users will note that the
available model set in ns-3 focuses on modelling how Internet protocols and networks work, but
ns-3 is not limited to Internet systems; several users are using ns-3 to model non-Internet-based
systems.

Many simulation tools exist for network simulation studies. Below are a few distinguishing
features of ns-3 in contrast to other tools.

• ns-3 is designed as a set of libraries that can be combined together and also with other
external software libraries. While some simulation platforms provide users with a single,
integrated graphical user interface environment in which all tasks are carried out, ns-3 is
more modular in this regard. Several external animators and data analysis and
visualization tools can be used with ns-3. However, users should expect to work at the
command line and with C++ and/or Python software development tools.
• ns-3 is primarily used on Linux systems, although support exists for FreeBSD, Cygwin
(for Windows), and native Windows Visual Studio support is in the process of being
developed.

Page 2
ns-3 SIMULATION

Page 3
ns-3 SIMULATION

ns-2 versus ns-3

ns-2 ns-3

1. ns-2 is implemented using a 1. ns-3 is implemented using C++ .


combination of oTCL(for scripts 2. With modern hardware capabilities,
describing the network topology)and compilation time was not an issue
C++(The core of the simulator). like for ns-2,ns-3can be developed
with C++entirely.
2. This system was chosen in the early
3. A simulation script can be written as
Programming languages 1990’s to avoid the recompilation of
a C++ program, which is not possible
C++as it was very time consuming using
in ns-2.
the hardware available at that time, oTCL
4. There is a limited support for Python
recompilation takes less time than C++.
in scripting and visualization.
3. oTCL disadvantage: there is overhead
introduced with large simulations.

4. oTCL is the only available scripting


language.

1. Because ns-3 is implemented in C++,


1. ns-2 requires basic manual C++
all normal C++ memory management
memory management functions.
functions such as new, delete,
malloc, and free are still available.
Memory Management
2. Automatic de-allocation of objects is
supported using reference counting
(track number of pointers to an
object);this is useful when dealing
with Packet objects.

Page 4
ns-3 SIMULATION

1. A packet consists of 2 distinct 1. A packet consists of a single


regions; one for headers, and the buffer of bytes, and optionally a
second stores payload data. collection of small tags
2. ns-2 never frees memory used to containing meta-data.
store packets until the simulation
2. The buffer corresponds exactly to the
Packets terminates, it just reuses the
stream of bits that would be sent over
allocated packets repeatedly, as a
areal network.
result, the header region of any
packet includes all headers defined as 3. Information is added to the packet by
part of the used protocol even if that using subclasses; Header, which adds
particular packet won't use that information to the beginning of the
particular header ,but just to be buffer, Trailer, which adds to the end.
available when this packet allocation 4. Unlike ns-2, there is generally easy
is reused. way to determine if a specific header
is attached.

1. The total computation time required 1. NS-3 performs better than ns-2 in
to run a simulation scales better in ns-3 terms of memory management.
Performance than ns-2. 2. The aggregation system prevents
2. This is due to the removal of the unneeded parameters from being
overhead associated with stored, and packets don't contain
interfacing oTcl with C++,and the unused reserved header space.
overhead associated with the oTcl
interpreter.

1. ns-2 comes with a package called 1. ns-3 employs a package known as

Simulation Output NAM(Network Animator),it's a Tcl PyViz, which is a python based real-
based animation system that time visualization package
produces a visual representation of
the network described.

Page 5
ns-3 SIMULATION

EXPLANATION OF TERMS

Node

Because in any network simulation, we will need nodes. So ns-3 comes with NodeContainer that
you can use to manage all the nodes (Add, Create, Iterate, etc.).

// Create two nodes to hold.


NodeContainer nodes;
nodes.Create (2);

Channel and NetDevice

In the real world, they correspond to network cables (or wireless media) and peripheral cards
(NIC). Typically these two things are intimately tied together. In the first example, we are
Using PointToPointHelper that wraps the Channel and NetDevice.

// Channel: PointToPoint, a direct link with `DataRate` and `Delay` specified.


PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));

Then we need to install the devices. The internal of Install is actually more complicated, but for
now, let’s just skip the magic behind the scene.

// NetDevice: installed onto the channel


NetDeviceContainer devices;
devices = pointToPoint.Install (nodes);

Page 6
ns-3 SIMULATION

Protocols
Internet and IPv4. Since Internet is the current largest network to study, ns-3 has a particular focus
on it. The InternetStackHelper will install an Internet Stack (TCP, UDP, IP, etc.) on each of the
nodes in the node container.

// Protocol Stack: Internet Stack


InternetStackHelper stack;
stack.Install (nodes);

To assign IP addresses, use a helper and set the base. The low level ns-3 system actually
remembers all of the IP addresses allocated and will generate a fatal error if you accidentally cause
the same address to be generated twice.

// Since IP Address assignment is so common, the helper does the dirty work!
// You only need to set the base.
Ipv4AddressHelper address;
address.SetBase ("10.1.1.0", "255.255.255.0");

// Assign the address to devices we created above


Ipv4InterfaceContainer interfaces = address.Assign (devices);

Applications
Every application needs to have Start and Stop function so that the simulator knows how to
schedule it. Other functions are application-specific. We will use UdpEchoServer and
UdpEchoClientfor now.

// Application layer: UDP Echo Server and Client


// 1, Server:
UdpEchoServerHelper echoServer (9);
ApplicationContainer serverApps = echoServer.Install (nodes.Get (1));
serverApps.Start (Seconds (1.0));

Page 7
ns-3 SIMULATION

serverApps.Stop (Seconds (10.0));

// 2, Client:
UdpEchoClientHelper echoClient (interfaces.GetAddress (1), 9);
echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));

Simulation

// Start Simulation
Simulator::Run ();
Simulator::Destroy ();
return 0;

Page 8
ns-3 SIMULATION

SOURCE 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"

// Default Network Topology


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

using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("SecondScriptExample");

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"));

Page 9
ns-3 SIMULATION

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;
}

Page 10
ns-3 SIMULATION

INFERENCE:

Page 11
ns-3 SIMULATION

Page 12

You might also like