Ns-3-Network-Simulator/C2/Creating-Point-to-Point-Network-in-ns-3/English

From Script | Spoken-Tutorial
Jump to: navigation, search
Visual Cue Narration
Slide:1

Title Slide

Welcome to the Spoken Tutorial on Creating Point-to-Point Network in ns-3.
Show slide:

Learning Objectives

In this tutorial, we will learn
  • About structure of an ns-3 program
  • About the classes and the methods
  • To create nodes
  • To establish a point-to-point connection between two nodes
  • To configure client and server applications on the nodes
  • To visualize the network using NetAnim
Show slide:

System Requirements

To record this tutorial, I am using
  • Ubuntu Linux operating system version 22.04
  • ns-3.38
  • NetAnim visualizer tool
Show slide:

Prerequisite

https://www.spoken-tutorial.org

To follow this tutorial:
  • You must have basic knowledge of using Linux terminal
  • For pre-requisite Linux tutorials, please visit this website
Show slide:

Code files

  • The files used in this tutorial are provided in the Code files link.
  • Please download and extract the files.
  • Make a copy and then use them while practicing.
Show slide:

Point-to-Point Topology

A point-to-point topology is a communication link between two hosts in a network.
Show diagram: Point-to-Point network with two nodes In this tutorial, we will be creating the topology shown below.

In this network, Node 0 is the transmitter and node 1 is the receiver.

Open point-to-point.cc source file I have created the source file point_to_point.cc for this program.

Now we will go through the source code in the text editor.

The source code contains the required functions to implement a point-to-point network.

Highlight the lines:

#include "ns3/applications-module.h"

#include "ns3/core-module.h"

#include "ns3/internet-module.h"

#include "ns3/network-module.h"

#include "ns3/point-to-point-module.h"

#include "ns3/netanim-module.h"

#include "ns3/mobility-helper.h"

#include "ns3/mobility-model.h"

First we include the header files.
Highlight CommandLine cmd(__FILE__) cmd is a CommandLine class instance method.

It is used to get optional arguments for the program from the command line.

In this program we would not be using any optional argument.

Highlight cmd.Parse(argc, argv) The Parse method of the CommandLine class parses program arguments.

The first argument is the number of arguments to parse.

The second argument is the array of arguments.

Highlight LogComponentEnable on both the lines The LogComponentEnable function is used to enable logging.
Highlight NodeContainer nodes Next we create a NodeContainer class instance named nodes.

A NodeContainer class instance is used to store nodes in the network.

Highlight nodes.Create(2) Using the Create method, we create 2 nodes in the nodes container.
Highlight PointToPointHelper pointToPoint Now let's initialize the network and set parameters.

Create an instance of the PointToPointHelper class with the name pointToPoint.

PointToPointHelper is a topology helper class.

Helper class instances assist in creating and initializing networks.

Highlight pointToPoint.SetDeviceAttribute("DataRate", StringValue("100Mbps")) The SetDeviceAttribute sets the attributes for the devices or the nodes.

Here we set the data rate of the devices to 100 Mbps.

Highlight pointToPoint.SetChannelAttribute("Delay", TimeValue(NanoSeconds(6560))) The SetChannelAttribute sets attributes for the communication channel.

We set the delay of the communication channel to 6560 nanoseconds.

Highlight NetDeviceContainer devices The nodes are not yet ready to communicate.

We need to install net devices on the nodes.

So we create a container for the net devices.

The NetDeviceContainer class instance holds net devices.

Highlight devices = pointToPoint.Install(nodes) Now let’s install the net devices on the node, using the Install method.
Highlight InternetStackHelper stack Next we shall install internet stack on the nodes.

The InternetStackHelper class instance installs internet stack on the nodes.

Highlight stack.Install(nodes) The stack includes implementations of TCP, UDP etc.

The nodes are passed as the parameter, to the Install method,

Highlight Ipv4AddressHelper address and address.SetBase("10.1.1.0", "255.255.255.0") Ipv4AddressHelper class instance assigns IP addresses to the net devices.

Let's set the base IP address for the network using the SetBase method.

Highlight Ipv4InterfaceContainer interfaces = address.Assign(devices) The Assign method assigns IP addresses sequentially to the net devices.

The Ipv4InterfaceContainer class instance stores the interfaces.

Highlight UdpEchoServerHelper echoServer(9) The UdpEchoServerHelper instance configures an echo server on a node.

An echo server receives data from its client and echoes it back.

It takes an integer as a parameter that represents the port number. ] Here, we create an instance of UdpEchoServerHelper with port number 9.

Highlight ApplicationContainer serverApps = echoServer.Install(nodes.Get(1)) The ApplicationContainer class instance stores server or client applications.

Create an ApplicationContainer instance named serverApps.

Install a server application on the second node of the network with index 1.

Highlight serverApps.Start(Seconds(1.0)) The Start method specifies the start time in seconds for the applications.

Set the start time of the applications to 1 second.

Highlight serverApps.Stop(Seconds(10.0)) The Stop method specifies the stop time in seconds.

Set the stop time to 10 seconds.

The time values are counted from the start of the simulation.

Highlight UdpEchoClientHelper echoClient(interfaces.GetAddress(1), 9) The UdpEchoClientHelper instance configures an echo client on a node.

An echo client sends data to an echo server.

The constructor takes two parameters.

The first one is the interface on which the application is to be installed.

The second is the port number.

Let’s create the client application helper instance.

The attributes are the client node and port number.

Highlight echoClient.SetAttribute("MaxPackets", UintegerValue(10)) The SetAttribute method sets attributes for the client.

The attributes include packet size, number of packets etc.

Hence here we set the maximum number of packets, to 10.

Highlight echoClient.SetAttribute("Interval", TimeValue(Seconds(0.5))) Then we set the time interval between the packet transmissions.

Here the value is 0.5 seconds.

Highlight echoClient.SetAttribute("PacketSize", UintegerValue(1024)) Next, set the packet size to 1024 bytes.
Highlight ApplicationContainer clientApps = echoClient.Install(nodes.Get(0)) Install the client application on the first node with index 0.
Highlight clientApps.Start(Seconds(2.0)) Then set the start time of the client application.

Here the start time is 2 seconds from the start of the simulation.

Highlight clientApps.Stop(Seconds(10.0)) Set the stop time to 10 seconds.
Highlight mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel") Let’s set a mobility model for the nodes.

The details about mobility shall be covered in future tutorials.

Highlight mobility.Install(nodes) Here we install the mobility model on the nodes.
Highlight AnimationInterface anim ("point-to-point.xml") Here we create an object of the AnimationInterface class.

The AnimationInterface class instance creates an XML file.

This file would be imported into NetAnim for animation.

The constructor takes the file name as a parameter.

Here the name of the file is point-to-point.xml.

Highlight anim.SetConstantPosition() The SetConstantPosition method sets the X-Y position for a node.

Using the anim object, we set the position for both the nodes.

The first argument is the node whose position is being set.

The second argument is the X coordinate.

The third argument is the Y coordinate.

Highlight Simulator::Run() The Run function is used to run the simulation.
Highlight Simulator::Destroy() And Destroy function ends the simulation.

Now close the text editor.

Press Ctrl,Alt and T keys simultaneously.

Type cd ns-allinone-3.38/ns-3.38 to navigate to the ns3 installation directory

Type mv ~/Downloads/point-to-point.cc scratch/point-to-point.cc to move the source file to scratch directory.

Now we will observe the simulation.

Open the terminal by pressing Ctrl, Alt and T keys simultaneously.

Using the cd command, navigate to the installation directory of ns-3.

Go to the ns-3.38 directory.

Move your source file to the scratch directory.

The ns3 batch file is configured to run files from the scratch directory.

Type ./ns3 run scratch/point-to-point.cc Run the command dot forward slash ns3 run space scratch forward slash point hyphen to hyphen point dot cc.

point to point dot cc is the name of the source file.

Keep the terminal open and show the output of the command After compilation, we get the details of each packet transfer.
Navigate to netanim directory under ns-allinone-3.38

Type cd ~/ns-allinone-3.38/netanim

Now, to visualize the network, we will use NetAnim.

Navigate to the netanim directory under ns-allinone-3.38.

Type ./NetAnim Now type dot forward slash NetAnim. The NetAnim window will open.
Click on the Open XML trace file icon on the top left corner of the window. Click on the Open XML trace file button on the top left corner of the window.
In the file picker, navigate to the ns-allinone-3.38/ns3.38 directory and select the point-to-point.xml file. In the file picker, navigate to the ns-3.38 directory.

Select the point-to-point.xml file.

Click on open at the bottom.

On the toolbar, click on the Node Size drop down button, and select size 10. On the toolbar, click on the Node Size drop down button.

Select size 10.

On the toolbar, click on the play button On the toolbar, click on the play animation button to view the simulation.

We see the circles representing the nodes.

The lines with arrows represent the transfer of packets.

Hover over node 0. Node 0 is the transmitter or the source node.
Hover over node 1. Node 1 is the receiver or the server node.
Show slide:

Summary

This brings us to the end of the tutorial.

Let us summarize.

In this tutorial, we have learnt

  • About the structure of an ns-3 program
  • About the classes and the methods
  • To create nodes
Show slide:

Summary

  • To establish a point-to-point connection between two nodes
  • To configure client and server applications on the nodes
  • To visualize the network using NetAnim
Show slide:
Assignment
As an assignment, please do the following:
  • Write an ns-3 program with two point-to-point networks
  • Set IP address 10.1.0.0 for the first network
  • Set IP address 10.2.0.0 for the second network
Show slide:

Assignment

  • Set different values for data rate, delay and packet size for each network
  • Install client and server applications in each network
Show slide:
Assignment
  • Start transfer of packets in each network simultaneously
  • Generate an XML file for NetAnim animation
Show slide: Assignment
  • Run the simulation in the terminal
  • Import the XML file in NetAnim and play the animation
Show slide:

Assignment - Observation

  • In the terminal, you will get this output.
  • Observe the differences for the two networks.
Show slide:

Assignment - Observation

  • In the NetAnim window, you will see two different networks.
  • Observe the packet transfer rate for each network.
Show slide:

About the Spoken Tutorial Project

The video at the following link summarizes the Spoken Tutorial project.

Please download and watch it.

Show Slide:

Spoken Tutorial Workshops

The Spoken Tutorial Project team conducts workshops and gives certificates.

For more details, please write to us.

Show Slide:

Answers for THIS Spoken Tutorial

Please post your timed queries in this forum.
Show Slide:

FOSSEE Forum

For any general or technical questions on ns-3, visit the FOSSEE forum and post your questions.
Show slide

Acknowledgement

Spoken Tutorial Project was established by the Ministry of Education, Government of India.
Show slide

Acknowledgement

We thank Dr.Moyukh Laha from IIT Kharagpur for his domain support.

We would also like to thank Dr. R. Radha, Dr. X. Anita, and Dr. T. Subbulakshmi from VIT, Chennai for their support.

Show slide:

Thank You

This is Josiga, a FOSSEE summer fellow 2023, IIT Bombay signing off.

Thanks for joining.

Contributors and Content Editors

Madhurig, Nirmala Venkat