MCSL-223 Section-1 Computer Networks Lab
MCSL-223 Section-1 Computer Networks Lab
COMPUTER
NETWORKS AND
DATA MINING LAB
SECTION 1 COMPUTER NETWORKS LAB NE
1.0 INTRODUCTION
This is the lab course, wherein you will have the hands on experience. You have
studied the support course material (MCS-218 Data Communication and Computer
Networks). In this section, Computer Network Programming using an Open Source
platform, Network Simpulator-3 (NS-3) is provided illustratively. A list of
programming problems is also provided at the end for each session. You are also
provided some example codes in the end for your reference (Appendix). Please go
through the general guidelines and the program documentation guidelines carefully.
1.1 OBJECTIVES
After completing this lab course you will be able to:
Explore and understand the features available in the NS-3 software
Create wired and wireless connection
Test and verify various routing/ communication protocols
Test and verify user defined routing protocols
Perform analysis and evaluation of various available protocols
You may seek assistance in doing the lab exercises from the concerned
lab instructor. Since the assignments have credits, the lab instructor is
obviously not expected to tell you how to solve these, but you may ask
questions concerning a technical problem.
For each program you should add comments above each function in the
code, including the main function. This should also include a description
of the function written.
18
ND
These descriptions should be placed in the comment block immediately COMPUTER
NETWORKS LAB
LAB above the relevant function source code.
The comment block above the main function should describe the purpose
of the program. Proper comments are to be provide where and when
necessary in the programming.
The list of the programs (list of programs given at the end, session-wise)
is available to you in this lab manual. For each session, you must come
prepare with the algorithms and the programs written in the Observation
Book. You should utilize the lab hours for executing the programs,
testing for various desired outputs and enhancements of the programs.
As soon as you have finished a lab exercise, contact one of the lab
instructor / in-charge in order to get the exercise evaluated and also get
the signature from him/her on the Observation book.
The total no. of lab sessions (3 hours each) are 10 and the list of
assignments is provided session-wise. It is important to observe the
deadline given for each assignment.
The Dynamips emulator is accessed and run with the help of a text based
front end known as Dynagen. This helps in interacting with Dynamips using
a OOP application programming interface (API). These APIs are used to
communicate and perform various tasks in Dynamips like instance reload,
reboot, loading devices etc.
However, the core NS-3 is a C++ based library and can be used using
scripting also. The scripting also helps in working with the standard NS-3
tracefile format i.e. .pcap files for various applications. The new features or
modules in NS-3 are implemented and complied in C++ and merged into the
system. In our case the basic NS-3 distribution is more than enough to work
upon and therefore we will be limiting ourselves to this only. We
recommend you to install “allinone” distribution for simplicity in installation
and can be installed with a simple install script.
Prerequisites: To run NS-3 you should have C++ compiler (g++ or other),
Python (3.6 or above), CMake and a build system (make, xcode etc.).
As mentioned this is an open source project and hence you are not
required to pay any amount to use or to implement this library at your
own. It is dependent on the research and community to be maintained and
further developed.
20
ND
You can download NS-3 freely from NS-3 website and following the COMPUTER
NETWORKS LAB
LAB installation guidelines, can easily install on different OS platforms like Mac
OS, Linux and Windows. Originally, the NS-3 is based on the Linux
environment, but if you have Windows environment, you can use cygwin to
create a running environment for Linux based applications on windows as
this provides the necessary environment for such applications. You can also
use other hypervisor tools to create Linux environment in Windows and then
install NS-3.
For using NS-3 all-in-one installation file GCC and other dependencies must
be installed on the system. These dependencies can be found on the NS-3
official website.
Prerequisite Package/version
C++ compiler clang++ or g++ (g++ version 8 or greater)
Python python3 version >=3.6
CMake cmake version >=3.10
Build system make, ninja, xcodebuild (XCode)
any recent version (to access ns-3 from
Git
GitLab.com)
tar any recent version (to unpack an ns-3 release)
bunzip2 any recent version (to uncompress an ns-3 release)
1.6 EXAMPLES
Let us now try to run an example on the recently installed system. Create a file with
any name and save it with an extension as .cc or you can just go to
examples/tutorials directory and see first.cc example file. 21
COMPUTER To run the first example, copy the first.cc file to scratch directory and run the
NETWORKS AND NE
DATA MINING LAB following commands in the NS-2 terminal (cygwin) from the parent directory
Argument Passing: NS-3 also supports the command line options and can pass the
arguments to the program using command line. The following example with file
second.cc program shows the argument value 3 given to nCsma variable.
./waf --run "second --nCsma=3"
Error Compilation: NS-3 by default treat the warnings as errors (enabling –Werror
option) which is a good sign for professional but a little more exercise for beginners.
You can disable this option by editing the waf-tools/cflags.py and change the
following line:
self.warnings_flags = [['-Wall'], ['-Werror'], ['-Wextra']]
Once you are aware about few of the basic commands, you can see the
examples given in the documentation of the NS-3. The link for the
documentation is given below. A sample program is provided at the end of
this file for tracing packets.
https://www.nsnam.org/doxygen/tcp-bbr-example_8cc_source.html
Application: The software with interacts with the operating systems and does
the specified job is termed as an application. NS-3 refers to application as a
user program which generates an activity which is further simulated. The
Application class provides various associated methods to managing the user
level applications.
22
ND
Net Device: A net device is usually an abstraction of both simulated COMPUTER
NETWORKS LAB
LAB hardware and software driver. After installation, the net device enables the
node to communicate with other nodes using channels (in simulations).
Figure 1, shows the basic data flow model in simulator using ns-3.
Applications talk with protocol stalks using APIs which are then used to
communicate using NetDevices via channels.
There are many other terms and concepts which you can refer online on at the
following link:
https://www.nsnam.org/docs/release/3.35/tutorial/html/conceptual-
overview.html#key-abstractions
Application Application
Application
Application Application
Application
Packet(s)
Protocol Protocol
stack stack
Node Node
NetDevice NetDevice
NetDevice Channel NetDevice
NetDevice Channel NetDevice
23
COMPUTER
NETWORKS AND
3. Measure the throughput (end to end) while varying latency in the
NE
DATA MINING LAB network created in Session -1.
4. Create a simple network topology having two client node on left side and
two server nodes on the right side. Both clients are connected with
another node n1. Similarly both server node connecting to node n2. Also
connect node n1 and n2 thus forming a dumbbell shape topology. Use
point to point link only.
Session - 3
5. Install a TCP socket instance connecting either of the client node with
either of the server node in session 2’s network topology.
6. Install a TCP socket instance connecting other remaining client node
with the remaining server node in session 2’s network topology.
7. Start TCP application and monitor the packet flow.
Session - 4
8. Take three nodes n1, n2 and n3 and create a wireless mobile ad-hoc
network.
9. Install the optimized Link State Routing protocol on these nodes.
Session – 5
10. Create a UDP client on a node n1 and a UDP server on a node n2.
11. Send packets to node n2 from node n1 and plot the number of bytes
received with respect to time at node n2.
12. Show the pcap traces at node n2’s WiFi interface.
Session – 6
13. Use 2 nodes to setup a wireless ad-hoc network where nodes are
placed at a fixed distance in a 3D plane.
14. Install UDP server and Client at these two nodes.
15. Setup a CBR transmission between these nodes.
Session – 7
16. Setup 4 nodes, two TCP client and server pair and two UDP client
and server pair.
17. Send packets to respective clients from both the servers.
18. Monitor the traffic for both the pair and plot the no. of bytes received.
Session – 8
19. Use the setup made in session 2 and monitor the traffic flow, plot the
packets received.
20. Start the TCP application at Time 1 second.
21. After 20 seconds, start the UDP application at Rate1 which clogs the
24
half of the dumbbell bridge capacity.
ND
22. Using ns-3 tracing mechanism, plot the changes in the TCP window COMPUTER
NETWORKS LAB
LAB size over the time.
Session – 9
23. In the last session 8, Increase the UDP rate at 30 second to Rate2 such
that it clogs whole of the dumbbell bridge capacity.
24. Use MatPlotlLib or GNUPlot to visualize cwnd vs time, also mention
Rate1 and Rate2.
Session -10
25. Create a point to pint network between two nodes with the following
parameters.
Link bandwidth between the two nodes. Default is 5 Mbps.
One way delay of the link. Default is 5 milliseconds.
Loss rate of packets on the link. Default is 0.000001. (This covers
losses other than those that occur due to buffer drops at node0.)
Queue size of the buffer at node 0. Default is 10 packets.
Simulation time. Default is 10 seconds.
2. https://www.cse.iitb.ac.in/~mythili/teaching/cs224m_autumn2017/tcpsimpa/
index.html
3. http://intronetworks.cs.luc.edu/current/html/ns3.html
4. https://www.nsnam.org/doxygen
5. https://www.nsnam.org/doxygen/csma-bridge_8cc_source.html
6. https://www.nsnam.org/docs/release/3.35/tutorial/html/conceptual-
overview.html#key-abstractions
7. https://www.nsnam.org/wiki/Installation#Windows
8. https://www.wireshark.org/docs/wsug_html_chunked/ChapterIO.html
25
COMPUTER
NETWORKS AND NE
DATA MINING LAB Appendix
29
COMPUTER
NETWORKS AND NE
DATA MINING LAB
*
*/
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-apps-module.h"
#include "ns3/csma-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("DhcpExample");
int
main (int argc, char *argv[])
{
CommandLine cmd (__FILE__);
bool verbose = false;
bool tracing = false;
cmd.AddValue ("verbose", "turn on the logs", verbose);
cmd.AddValue ("tracing", "turn on the tracing", tracing);
cmd.Parse (argc, argv);
// GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true));
if (verbose)
{
LogComponentEnable ("DhcpServer", LOG_LEVEL_ALL);
LogComponentEnable ("DhcpClient", LOG_LEVEL_ALL);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
}
Time stopTime = Seconds (20);
NS_LOG_INFO ("Create nodes.");
NodeContainer nodes;
NodeContainer router;
nodes.Create (3);
router.Create (2);
NodeContainer net (nodes, router);
NS_LOG_INFO ("Create channels.");
30 CsmaHelper csma;
csma.SetChannelAttribute ("DataRate", StringValue ("5Mbps")); COMPUTER
ND
LAB csma.SetChannelAttribute ("Delay", StringValue ("2ms")); NETWORKS LAB
csma.SetDeviceAttribute ("Mtu", UintegerValue (1500));
NetDeviceContainer devNet = csma.Install (net);
NodeContainer p2pNodes;
p2pNodes.Add (net.Get (4));
p2pNodes.Create (1);
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
NetDeviceContainer p2pDevices;
p2pDevices = pointToPoint.Install (p2pNodes);
InternetStackHelper tcpip;
tcpip.Install (nodes);
tcpip.Install (router);
tcpip.Install (p2pNodes.Get (1));
Ipv4AddressHelper address;
address.SetBase ("172.30.1.0", "255.255.255.0");
Ipv4InterfaceContainer p2pInterfaces;
p2pInterfaces = address.Assign (p2pDevices);
// manually add a routing entry because we don't want to add a dynamic routing
Ipv4StaticRoutingHelper ipv4RoutingHelper;
Ptr<Ipv4> ipv4Ptr = p2pNodes.Get (1)->GetObject<Ipv4> ();
Ptr<Ipv4StaticRouting> staticRoutingA = ipv4RoutingHelper.GetStaticRouting (ipv4Ptr);
staticRoutingA->AddNetworkRouteTo (Ipv4Address ("172.30.0.0"), Ipv4Mask ("/24"),
Ipv4Address ("172.30.1.1"), 1);
NS_LOG_INFO ("Setup the IP addresses and create DHCP applications.");
DhcpHelper dhcpHelper;
// The router must have a fixed IP.
Ipv4InterfaceContainer fixedNodes = dhcpHelper.InstallFixedAddress (devNet.Get (4),
Ipv4Address ("172.30.0.17"), Ipv4Mask ("/24"));
// Not really necessary, IP forwarding is enabled by default in IPv4.
fixedNodes.Get (0).first->SetAttribute ("IpForward", BooleanValue (true));
// DHCP server
ApplicationContainer dhcpServerApp = dhcpHelper.InstallDhcpServer (devNet.Get (3),
Ipv4Address ("172.30.0.12"),
Ipv4Address ("172.30.0.0"), Ipv4Mask ("/24"),
Ipv4Address ("172.30.0.10"), Ipv4Address ("172.30.0.15"),
Ipv4Address ("172.30.0.17"));
// This is just to show how it can be done.
DynamicCast<DhcpServer> (dhcpServerApp.Get (0))->AddStaticDhcpEntry (devNet.Get
(2)->GetAddress (), Ipv4Address ("172.30.0.14"));
dhcpServerApp.Start (Seconds (0.0));
dhcpServerApp.Stop (stopTime);
// DHCP clients
NetDeviceContainer dhcpClientNetDevs;
dhcpClientNetDevs.Add (devNet.Get (0));
dhcpClientNetDevs.Add (devNet.Get (1));
dhcpClientNetDevs.Add (devNet.Get (2));
ApplicationContainer dhcpClients = dhcpHelper.InstallDhcpClient (dhcpClientNetDevs);
dhcpClients.Start (Seconds (1.0));
dhcpClients.Stop (stopTime);
UdpEchoServerHelper echoServer (9);
ApplicationContainer serverApps = echoServer.Install (p2pNodes.Get (1));
serverApps.Start (Seconds (0.0));
serverApps.Stop (stopTime);
31
COMPUTER UdpEchoClientHelper echoClient (p2pInterfaces.GetAddress (1), 9);
NETWORKS AND
DATA MINING LAB echoClient.SetAttribute ("MaxPackets", UintegerValue (100)); NE
echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
ApplicationContainer clientApps = echoClient.Install (nodes.Get (1));
clientApps.Start (Seconds (10.0));
clientApps.Stop (stopTime);
Simulator::Stop (stopTime + Seconds (10.0));
if (tracing)
{
csma.EnablePcapAll ("dhcp-csma");
pointToPoint.EnablePcapAll ("dhcp-p2p");
}
NS_LOG_INFO ("Run Simulation.");
Simulator::Run ();
Simulator::Destroy ();
NS_LOG_INFO ("Done.");
}
34