Basics of NS2 and Otcltcl script


NS2 (Network Simulator version 2) is a discrete event network simulator that is widely used to simulate and analyze the behavior of computer networks. It is open-source software and is written in C++ and Otcl (Object-oriented Tool Command Language). The Otcl (Object-oriented Tool Command Language) is an extension of Tcl (Tool Command Language) and is used to create and control network entities and configure network scenarios in NS2.

NS2 has two parts: the C++ part, which provides the underlying simulation engine, and the Otcl part, which provides the user interface and interaction between the user and the C++ part. The C++ part is responsible for scheduling and executing events, maintaining the state of the network, and handling low-level details such as packet processing and bit-level manipulation. The Otcl part is responsible for creating and configuring network entities, such as nodes and links, and for specifying the network scenario.

To use NS2, you need to have a basic understanding of both C++ and Otcl. To write a simulation script, you need to use both languages in conjunction. You write the network topology and scenario in Otcl and write the code for the actual protocol implementation in C++.

Here's a basic example of an Otcl script that creates a simple network scenario with two nodes and a duplex link −

# Create a Simulator object
set ns [new Simulator]

# Create two nodes
set n0 [$ns node]
set n1 [$ns node]

# Create a duplex link between the nodes
$ns duplex-link $n0 $n1 1Mb 10ms DropTail

# Start the simulation
$ns run

This script creates a Simulator object, which is used to control the simulation. Two nodes, n0 and n1, are created, and a duplex link is created between them with a 1 Mbps bandwidth and 10 ms delay. The link uses the DropTail queue management algorithm. The $ns run command starts the simulation.

You can also use NS2 to implement different routing and transport protocols like TCP, UDP, DSR, AODV, DSDV etc.

It would be helpful to look into the documentation and examples provided with NS2, and also online resources, tutorials and forums can be a good place to start.

Basic Commands

Here are some basic commands that you can use in an Otcl script to create and configure network entities in NS2 −

  • set ns [new Simulator] − This command creates a new Simulator object, which is used to control the simulation.

  • set n[i] [$ns node] − This command creates a new node, where i is an integer that uniquely identifies the node. The node is created as an object of the Node class, and it can be used to configure various properties of the node, such as its position in the network, its mobility, and its routing protocol.

  • $ns duplex-link $n0 $n1 bw delay queue-type − This command creates a duplex link between two nodes, $n0 and $n1, with a given bandwidth (bw), delay (delay), and queue management algorithm (queue-type). For example, $ns duplex-link $n0 $n1 1Mb 10ms DropTail creates a duplex link with 1 Mbps bandwidth and 10 ms delay, and uses the DropTail queue management algorithm.

  • $ns simplex-link $n0 $n1 bw delay queue-type − This command creates a unidirectional link between two nodes, $n0 and $n1, with a given bandwidth (bw), delay (delay), and queue management algorithm (queue-type).

  • $ns drop-tail − This command sets the queue management algorithm to DropTail.

  • $ns queue-limit $queue_var limit − This command sets the maximum queue size for the queue with the variable name $queue_var. For example, $ns queue-limit $tcp_queue 1000000 sets the maximum queue size to 1,000,000 bytes.

  • $ns at time "$tcl_command" − This command schedules a Tcl command to be executed at a given simulation time. For example, $ns at 5.0 "$n0 start" starts a node at 5.0 seconds in the simulation.

  • $ns run − This command starts the simulation and runs it for a specified time or until a specific event occurs.

  • $ns stop − This command stops the simulation at a specific time or event.

These are just some basic commands you can use to create and configure network entities in NS2. There are many other commands and options that you can use to fine-tune your simulation and implement more complex network scenarios. It would be a good idea to refer to the documentation and examples provided with NS2, and also online resources, tutorials, and forums for more information on using NS2 and Otcl.

Some examples for basic commands

Sure, here are some examples of how you can use some of the basic commands in an Otcl script to create and configure a simple network scenario in NS2 −

Creating a Simulator object and setting the simulation stop time

set ns [new Simulator]
$ns set stoptime 20

This creates a new Simulator object, and sets the simulation stop time to 20 seconds.

Creating nodes

set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]

This creates three nodes, n0, n1 and n2, as objects of the Node class. You can use these nodes to configure various properties of the nodes, such as their position in the network, their mobility, and their routing protocol.

Creating a duplex link between two nodes

$ns duplex-link $n0 $n1 1Mb 10ms DropTail

This creates a duplex link between nodes n0 and n1 with a 1 Mbps bandwidth and 10 ms delay, and uses the DropTail queue management algorithm.

Creating a simplex-link link between two nodes

$ns simplex-link $n0 $n2 2Mb 15ms DropTail

This creates a unidirectional link between nodes n0 and n2 with a 2Mbps bandwidth and 15 ms delay, and uses the DropTail queue management algorithm.

Setting the queue size limit

set queue_var [$ns queue]
$ns queue-limit $queue_var 200

This creates a queue variable $queue_var and set the limit of the queue to 200 packets.

Scheduling an event to start a node at a certain time

$ns at 1.0 "$n0 start"

This schedules the command $n0 start to be executed at 1 second into the simulation.

Starting the simulation

$ns run

This starts the simulation and runs it for the specified time or until a specific event occurs.

These are just a few examples of how you can use some of the basic commands in Otcl to create and configure a simple network scenario in NS2. As you continue to learn and use NS2, you will be able to build more complex network scenarios and protocols.

Updated on: 06-Feb-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements