Skip to main content

VLAN implementation using NS2

VLAN implementation using NS2

VLAN implementation using NS2


  • Network Simulator-2
  • Operating System – LINUX (UBUNTU)


VLAN is a custom network that is created from one or more local area networks. It enables a group of devices available in multiple networks to be combined into one logical network. The result becomes a virtual LAN that is administered like a physical LAN. The full form of VLAN is defined as Virtual Local Area Network.

The below topology depicts a network having all hosts inside the same virtual LAN:

Without VLANs, a broadcast sent from a host can easily reach all network devices. Each and every device will process broadcast received frames. It can increase the CPU overhead on each device and reduce the overall network security.

In case if you place interfaces on both switches into separate VLANs, a broadcast from host A can reach only devices available inside the same VLAN. Hosts of VLANs will not even be aware that the communication took place. This is shown in the below picture:

VLAN in networking is a virtual extension of LAN. A LAN is a group of computer and peripheral devices that are connected in a limited area such as a school, laboratory, home, or office building. It is a widely useful network for sharing resources like files, printers, games, and other applications.

Here are step-by-step details of how VLAN works:

  • VLANs in networking are identified by a number.
  • A Valid range is 1-4094. On a VLAN switch, you assign ports with the proper VLAN number.
  • The switch then allows data which needs to be sent between various ports having the same VLAN.
  • Since almost all networks are larger than a single switch, there should be a way to send traffic between two switches.
  • One simple and easy way to do this is to assign a port on each network switch with a VLAN and run a cable between them.

Difference between LAN and VLAN



LAN can be defined as a group of computer

and peripheral devices that are connected

in a limited area.

A VLAN can be defined as a custom network that is created from one or more local area networks.

The full form of LAN is Local Area Network

The full form of VLAN is Virtual Local Area Network.

The latency of the LAN is high.

The latency of VLAN is less.

The cost of LAN is high.

The cost of a VLAN is less.

In LAN, the network packet is advertised

 to each and every device.

In VLAN, the network packet is sent to only a specific broadcast domain.

It uses a ring, and FDDI (Fiber

Distributed    Data Interface) is a protocol.

It uses ISP and VTP as a protocol.


If you have a router that supports virtual LANs (VLANs), you can create multiple VLANs from that single router. This can be useful if you want to segment your network traffic for security or performance reasons.

A VLAN is a logical grouping of devices on a network. VLANs are used to segment a network into smaller, more manageable pieces. VLANs are created by configuring a router or switch to divide a physical network into multiple logical networks.

Creating a VLAN is a two-step process. First, you must create the VLAN itself. This is done by configuring a router or switch to create a new logical network. Second, you must add devices to the VLAN. This is done by configuring the router or switch to add devices to the VLAN.
Adding devices to a VLAN is a simple process. First, you must determine which devices you want to add to the VLAN. Second, you must configure the router or switch to add those devices to the VLAN.

Switches generate a VLAN as a broadcast domain. You, the administrator, set up some switch ports in a different VLAN than the default VLAN for the sake of convenience. VLANs are enabled in Cisco switches by default, and ALL devices are already connected to VLAN 1. If there were no additional configuration required, these devices would be unable to communicate. All devices on a switch can communicate with one another using the same ports as a single switch. Consider using VLANs if you’re having difficulty connecting to the Internet. A trunk port is shared by multiple switches or routers and is used to route traffic between them.

VLANs can be configured in a variety of Cisco switches, even if they are in different models. To communicate with other trunks, a trunk port must use a special trunk trunk protocol. Because VLANs limit the number of broadcasts, they provide a higher level of performance to large and medium LANs. As a result, they also provide security because you effectively have one group of devices connected to a VLAN.


It is possible to use multi-VLAN ports to conduct networking. These switches and routers can be used to separate groups of users, departments, and functions from one another without the need for separate switches or routers. There is a significant cost savings, improved network security and performance, and simplified management associated with this model.


  1. Create a simulator object
  2. Open a nam trace file and define the finish procedure then close the trace file, and execute nam on the trace file.
  3. Create VLANs with router(s) that form a logical network
  4. Create duplex links between the VLANs
  5. Schedule events and run the program.

Scheduling Events

NS is a discrete event-based simulation. The TCL script defines when the event should
occur. The initializing command set ns [new Simulator] creates an event scheduler, and events are then scheduled using the format:

The simulation can then begin using the command

$ns run


set opt(tr)        ""

set opt(namtr)     "vlandemo.nam"

set opt(seed)        0

set opt(stop)        1.0

set opt(node)        4

set opt(qsize)        100

set opt(bw)        10Mb

set opt(delay)        1ms

set opt(ll)        LL

set opt(ifq)        Queue/DropTail

set opt(mac)        Mac/Csma/Cd

#set opt(mac)        Mac/Csma/Ca

#set opt(mac)        Mac/802_3

set opt(chan)        Channel

set opt(tcp)        TCP/Newreno

set opt(sink)        TCPSink

set opt(app)        FTP

proc finish {} {

        global ns opt

        $ns flush-trace

#        exec nam $opt(namtr) &

        exit 0


proc create-trace {} {

        global ns opt

        if [file exists $opt(tr)] {

                catch "exec rm -f $opt(tr) $opt(tr)-bw [glob $opt(tr).*]"


        settrfd [open $opt(tr) w]

        $ns trace-all $trfd

        if {$opt(namtr) != ""} {

                $ns namtrace-all [open $opt(namtr) w]


        return $trfd


proc create-topology {} {

        global ns opt

        global lan node source node0 nodex

        set num $opt(node)

        for {set i 0} {$i< $num} {incri} {

                set node($i) [$ns node]

                lappendnodelist $node($i)


        set lan [$ns make-lan $nodelist $opt(bw) \

        $opt(delay) $opt(ll) $opt(ifq) $opt(mac) $opt(chan)]

        set node0 [$ns node]

        $ns duplex-link $node0 $node(1) 20Mb 2ms DropTail

#        $ns duplex-link-op $node0 $node(1) orient right

        set nodex [$ns node]

        $ns duplex-link $nodex $node(2) 20Mb 2ms DropTail

#        $ns duplex-link-op $nodex $node(2) orient left


## MAIN ##

set ns [new Simulator]

settrfd [create-trace]

# create-topology

set tcp0 [$ns create-connection TCP/Newreno $node0 TCPSink $nodex 0]

$tcp0 set window_ 15

set ftp0 [$tcp0 attach-app FTP]

#set udp0 [new Agent/UDP]

#$ns attach-agent $node0 $udp0

#set cbr0 [new Application/Traffic/CBR]

#$cbr0 attach-agent $udp0

#set rcvr0 [new Agent/Null]

#$ns attach-agent $nodex $rcvr0

#$udp0 set dst_ [$rcvr0 set addr_]

$ns at 0.0 "$ftp0 start"

#$ns at 0.0 "$cbr0 start"

$ns at $opt(stop) "finish"

$ns run

# to run the example, use the following line

$ ns filename.tcl

$ nam vlandemo.nam


VLAN in ns2


  1. Sir, can you please explain about the implementation of wireless sensor network in ns3


Post a Comment

Popular posts from this blog

Installing ns3 in Ubuntu 22.04 | Complete Instructions

In this post, we are going to see how to install ns-3.36.1 in Ubuntu 22.04. You can follow the video for complete details Tools used in this simulation: NS3 version ns-3.36.1  OS Used: Ubuntu 22.04 LTS Installation of NS3 (ns-3.36.1) There are some changes in the ns3 installation procedure and the dependencies. So open a terminal and issue the following commands Step 1:  Prerequisites $ sudo apt update In the following packages, all the required dependencies are taken care and you can install all these packages for the complete use of ns3. $ sudo apt install g++ python3 python3-dev pkg-config sqlite3 cmake python3-setuptools git qtbase5-dev qtchooser qt5-qmake qtbase5-dev-tools gir1.2-goocanvas-2.0 python3-gi python3-gi-cairo python3-pygraphviz gir1.2-gtk-3.0 ipython3 openmpi-bin openmpi-common openmpi-doc libopenmpi-dev autoconf cvs bzr unrar gsl-bin libgsl-dev libgslcblas0 wireshark tcpdump sqlite sqlite3 libsqlite3-dev  libxml2 libxml2-dev libc6-dev libc6-dev-i386 libclang-dev llvm-

Installation of NS2 in Ubuntu 22.04 | NS2 Tutorial 2

NS-2.35 installation in Ubuntu 22.04 This post shows how to install ns-2.35 in Ubuntu 22.04 Operating System Since ns-2.35 is too old, it needs the following packages gcc-4.8 g++-4.8 gawk and some more libraries Follow the video for more instructions So, here are the steps to install this software: To download and extract the ns2 software Download the software from the following link Extract it to home folder and in my case its /home/pradeepkumar (I recommend to install it under your home folder) $ tar zxvf ns-allinone-2.35.tar.gz or Right click over the file and click extract here and select the home folder. $ sudo apt update $ sudo apt install build-essential autoconf automake libxmu-dev gawk To install gcc-4.8 and g++-4.8 $ sudo gedit /etc/apt/sources.list make an entry in the above file deb bionic main universe $ sudo apt update Since, it&#

Installation of NS2 (ns-2.35) in Ubuntu 20.04

Installation of NS2 (ns-2.35) in Ubuntu 20.04 LTS Step 1: Install the basic libraries like      $] sudo apt install build-essential autoconf automake libxmu-dev Step 2: install gcc-4.8 and g++-4.8 open the file using sudo mode $] sudo nano /etc/apt/sources.list Include the following line deb bionic main universe $] sudo apt update $] sudo apt install gcc-4.8 g++-4.8 Step 3:  Unzip the ns2 packages to home folder $] tar zxvf ns-allinone-2.35.tar.gz $] cd ns-allinone-2.35/ns-2.35 Modify the following make files. ~ns-2.35/ Change @CC@ to gcc-4.8 change @CXX@ to g++-4.8 ~nam-1.15/ ~xgraph-12.2/ ~otcl-1.14/ Change in all places  @CC@ to gcc-4.8 @CPP@ or @CXX@ to g++-4.8 open the file: ~ns-2.35/linkstate/ls.h Change at the Line no 137  void eraseAll() { erase(baseMap::begin(), baseMap::end()); } to This void eraseAll() { this->erase(baseMap::begin(), baseMap::end()); } All changes made Step 4: Open a new termi