Lab2 Lan 802 3
Lab2 Lan 802 3
Lab2 Lan 802 3
Simulation of LAN
1. To understand the CSMA/CD protocol and see how a LAN shares its workspace.
2. Evaluate the behavior of IEEE 802.3 protocol and analyze delay, jitter and throughput of a nominal
LAN versus its load.
3. To understand how NS simulate a LAN.
Introduction
The MAC (Medium Access) is a sublayer of data link layer and it mainly deals with protocols that are
used to share the resources and determine who could use them next on a multi access channel.
The simplest algorithm in MAC layer is pure ALOHA. In this protocol, stations transmit whenever they
have something to send and then listen to channel to find out if any frames are destroyed by collision,
then retransmit those frames.
The next version is slotted ALOHA at which the time is divided up into discrete intervals and stations
are allowed to send only at the beginning of each slot. As you know the efficiency of slotted ALOHA is
twice the pure ALOHA.
To improve the efficiency of the MAC layer a carrier sense protocol can be used to reduce the number
of packets that are lost due to collision. Using this approach, when a station has something to send it
first listens to channel to make sure that nobody else is transmitting at the moment. If the channel is
idle, it begins transmitting its packets, if not, the station waits until the channel becomes free. This
protocol is called “1-persistent CSMA”.
Another carrier sense protocol is “non persistent CSMA”. In this protocol stations sense the channel
and begin transmitting if the channel is detected to be free as in “1-persistent CSMA”. However, if the
channel is busy, the station does not continually sense it but waits a random amount of time and then
senses the channel again.
The third carrier sense protocol is “p-persistent CSMA” that is used for slotted channels. In this protocol
even if the channel is sensed to be idle, the transmission is done with the probability p.
It’s obvious that that non-persistent CSMA has the best efficiency, but it can have a large delay in
processing packets and sending them.
The most widely used protocol in MAC layer is CSMA/CD which is an improved version of CSMA
because stations abruptly stop transmitting when they detect collision and avoid the waste that is
caused by transmitting the remaining bytes of a destroyed frame. Ethernet, which is the most dominant
LAN protocol, uses 1-persistent CSMA/CD protocol which is standardized by IEEE as 802.3.
In this experiment we want to simulate a LAN with multiple stations. In the first part you will make a
typical LAN and see the packets that are exchanged between LAN’s nodes.
In the second part you will increase the load of the LAN and find out the effect of load on delay,
throughput and jitter of the receiving packets. And in the last part we examine the effect of increasing
the number of stations and so the load till the pipe becomes full and the packets dropped.
In all of above parts we use Trace Graph that is a MATLAB based software and it could manipulate
with NS trace files and easily analyze them. It has three pages:
• The main page, in which you open the trace file, choose the proper nodes and the packet types.
Data Networks
Dr. M. Pakravan 1
Department of Electrical Engineering
Sharif University of Technology
• The Network information page, that gives you lots of useful text based information about the
imported network.
• The Graph page in witch you could easily plot so many properties of the network vs. others.
You will find that Trace Graph is a powerful software for analyzing '.tr' files.
Simulation parameters in NS
set lan [$ns make-lan $nodelist 1Mb 10ms LL Queue/DropTail Mac/802_3 Channel]
or
set lan [$ns newLan $nodelist 1Mb 10ms LL Queue/DropTail Mac/802_3 Channel]
By this command NS will create a LAN between the nodes specified in nodelist or nodes between
quotations. (For example you could write “$n0 $n1 $n2” instead of $nodlist but it's recommended to not
to do so.) The other options are bandwidth, delay, data link layer type, interface queue, MAC layer type
and type of channel. (Fig. 1)
In this example, we have set the total bandwidth of the LAN to be Rtotal =1 Mbps.
Data Networks
Dr. M. Pakravan 2
Department of Electrical Engineering
Sharif University of Technology
The $nodelist is the list of nodes in the LAN that you should specify. For example, to create a list of
nodes containing 4 nodes you can use the following commands:
In this part we build a LAN with 4 nodes, and connect a single node to the LAN (Fig. 2) and also define
different color for data follows in NAM:
Data Networks
Dr. M. Pakravan 3
Department of Electrical Engineering
Sharif University of Technology
Figure 2 : Part I
It’s recommended using newLan instruction in this part because it has better visualization for NAM. If
you want NAM arrange nodes well you could write: (you should define the link between node n(0) and
n(1) before writing this line.
Define two UDP agents on nodes n(0) and n(2) and connect them to the Null agents on nodes n(4) and
n(3), then define two CBR traffic sources (these produce constant bit rate traffic) on UDPs. You should
define two different Flow IDs for the UDP agents (for ins. 1 and 2) for Trace Graph.
Data Networks
Dr. M. Pakravan 4
Department of Electrical Engineering
Sharif University of Technology
Then run NS, and see what happens to the packets, explain what you see in your report.
In this part we want to examine the effect of load on delay and jitter (change in receiving delay) of
receiving bytes, by Trace Graph.
To do so, change number of LAN nodes to 33, change the code that the CBR traffic on n(0) send to
n(33), then divide other LAN node into two groups. The first group contains transmitters, which are
UDP agents and have CBR traffic sources on them and the second are Null agents connected to the
first.
So define an array of transmitters with a given traffic rate. (For this step let’s assume R=0.05 mbps for
each station). You can use a FOR loop to define an array of transmitters and attach traffic sources to
them. Use next code instead of previous one you wrote for n(2) and n(4).
By the time we want to monitor the packets node n(0) send to n(33), in many different loads, so the
first time you won’t start any CBR on LAN, save the trace file, then run NS many times and each time
Data Networks
Dr. M. Pakravan 5
Department of Electrical Engineering
Sharif University of Technology
start more traffic sources (in a for loop) on LAN and save their trace files. (For a proper trace file size
you could force NS to finish at 5 seconds).
Note: In this part and the next part it's recommended to use make-lan instead of newLan, because it
makes a more reliable LAN.
If you notice the rates are properly defined, so if all of traffic sources start, their rate are still below LAN
limit.
Figure 3: Part II
Open trace files one by one in Trace Graph, in the main page set Current Node to 0, and other node to
33, check the box near other node.
In the Network Information page you could find many useful information about your network, by the
time, see the “Delay between current and other node” and “Simulation End2End delay”. (You should
see the increasing delay time as you open other trace files and when load is increasing.)
Data Networks
Dr. M. Pakravan 6
Department of Electrical Engineering
Sharif University of Technology
Write the number of traffic sources started in one column and the delay in another, then plot the
resulting delay vs. No. of nodes by Excel or MATLAB, and paste it in your report.
In the Graphs page you could plot many different properties of network, open each trace files you have,
then set the current node to 33 and plot: “Jitter of received packets on current node” and “Throughput of
receiving bits vs. average End2End delay” and at last Number of send/received packets from 3D
graphs menu. Explain your conclusion on your report.
Till now, you are familiar with a typical LAN and its properties in NS, so in this part we want to improve
our simulation method and attain the relationship between throughput and attempts.
We simulate a typical LAN with 32 nodes (16 Trs. and 16 Rvs.) and then run simulation and find out
throughput of LAN and attempts for a typical throughput.
We use Poisson traffic on source nodes to have better results. And also we should use output trace
files and extract proper result from them.
To do so, at first enable MAC layer tracing by addition of “-trace on” in your script as below:
set lan [$ns make-lan -trace on $nodelist 10Mb 1ms LL Queue/DropTail Mac/802_3 Channel ]
Then we should put Poisson sources on nodes, so we introduce the Exponential On/Off traffic source,
and then set its properties to behave like a Poisson source.
ton ton
toff toff
Hence a new Exponential On/Off traffic generator can be created and parameterized as follows:
Data Networks
Dr. M. Pakravan 7
Department of Electrical Engineering
Sharif University of Technology
NOTE: The Exponential On/Off generator can be configured to behave as a Poisson process by setting
the variable burst_time_ to 0 and the variable rate_ to a very large value. The C++ code guarantees
that even if the burst time is zero, at least one packet is sent. Additionally, the next inter-arrival time is
the sum of the assumed packet transmission time (governed by the variable rate_) and the random
variable corresponding to idle_time_. Therefore, to make the first term in the sum very small, make the
burst rate very large so that the transmission time is negligible compared to the typical idle times.
So you see if we chose burst_time_ to zero, and rate_ to a large number, the time between two
independent packets has an exponential distribution with idle_time_ mean.
Now we implement a Poisson traffic source over UDP agents and connect them to Null agents of
receivers. (Use the following code)
We can run the simulation for 5 seconds many times, Change the value of idle_time_ in each execution
to gather information of collisions and throughput of LAN. But it would be better if we write a procedure
to call itself periodically, increase traffic of LAN in each period, and then force NS to build many trace
files.
To do so, we should define an array of files, and when the procedure calls assign a new file to NS for
trace output. Write the following code to the beginning of you TCL code:
set num 1
set file($num) [open out$num.tr w]
set timefile [open timefile.txt w]
$ns trace-all $file(1)
Data Networks
Dr. M. Pakravan 8
Department of Electrical Engineering
Sharif University of Technology
Our goal is to force traffic sources sending for 5 seconds, stop them, save the trace file, increase the
load (by decreasing idle_time_ of sources) and then start sources again for 5 seconds and save
another trace file.
Start the sources at 0.0 and stop them at 5.0 for initial value of idle_time_ and then call increaserate{}
procedure at 6.0, if we start sources at 10 in this procedure again for 5 seconds and also force
increaserate{} to call itself every 10 seconds. By the time in every 10 seconds we have 5 seconds traffic
and also a trace file.
proc increaserate {} {
global ns psn file num timefile
set time 10
set now [$ns now]
close $file($num)
incr num
set file($num) [open out$num.tr w]
$ns trace-all $file($num)
Data Networks
Dr. M. Pakravan 9
Department of Electrical Engineering
Sharif University of Technology
}
for {set i 1} { $i < 17 } {incr i} {
$ns at [expr $now + 9] "$psn($i) stop"
}
$ns at [expr $now + $time ] "increaserate"
}
$ns at 6 "increaserate"
$ns at 200 "finish"
Now you have about 20 trace files, you must write a MATLAB program to process these trace files and
extract the following parameters:
• Generated Packets (that is represented by “h”).
• Received Packets (that’s represented by “r”).
• Collided Packets (that’s represented by “c”).
By the time you have three numbers for each simulation period, but in lower loads you might have no
collision, so set it to 0. And also we show total simulation time in each load by Ts (that is 5 seconds in
this part) and Frame time by Tf (each packet is 1000 bytes and so 8000 bits on a 10Mbps LAN so Tf is
equal to 800 micro sec.)
Note: You might also use MS. EXCEL for this but it’s recommended to do this by MATLAB, in Excel
you could open trace files in tables and then use AutoFilter function and extract proper data.
-----------------------------------------------------------------------------------------------------------------------------------
Number of Total Attempts in Simulation Time (Ts) =
No. of total received packets (r) + No. of Collided packets (c).
-----------------------------------------------------------------------------------------------------------------------------------
Received Packets per Frame Time =
No. of Total Received Packets * (Tf / Ts)
-----------------------------------------------------------------------------------------------------------------------------------
No. of Attempt per Frame Time =
No. of Total Attempts in Simulation Time * (Tf / Ts)
-----------------------------------------------------------------------------------------------------------------------------------
Throughput per Frame Time = Received Packets per Frame Time
-----------------------------------------------------------------------------------------------------------------------------------
Plot the Throughput per Frame Time versus Attempts per Frame Time for proper loads.
If you do it right, you will have a familiar graph.
Add your comments and explain why the graph is not just like the one in the text book. (For example a
difference between 802.3 and pure CSMA/CD is use of Exponential back off and so on.)
Also add your tables and graphs in the report.
Data Networks
Dr. M. Pakravan 10
Department of Electrical Engineering
Sharif University of Technology
If you look at the trace file after simulation or in the Trace Graph, you will see another node that you
haven’t defined before.
It is like a virtual node that stands between the nodes, to transmit data to another node on the LAN,
each node first send it’s packet to this virtual node called LANnode. Then the LANnode transfers it to
the desired destination and that’s why you see the packets are transferred twice in NAM.
The primary purpose of LANnode is to make LANs work with NS routing. NS topology is stored in link
array indexed by source and destination. To represent connectivity of a LAN one can either set a mesh
of links or create another virtual node. (Fig. 4)
For Example:
mesh of links to show that any node can send packets to any other node:
Data Networks
Dr. M. Pakravan 11
Department of Electrical Engineering
Sharif University of Technology
"virtual node": 4
Note that “b” scales better than “a” for larger number of nodes.
Aside from that, LANnode is a container for all shared LAN objects, including LAN-Router. In the
example above, suppose that there is another node 5 which is not on the LAN, but connected by a
point to point link to 1. Suppose 2 wants to send a packet to 5. It knows that the next hop is node 4 (the
virtual node). It sends it to 4 thereby handing it off to the link layer. LL is supposed to set a MAC
destination address for the packet, so it needs to know who the next hop is (which LAN node) but all it
can see is the packet destination address.
Since LL by itself doesn't have any connectivity info, it goes off and asks LANRouter for the next hop
then sets the MAC destination address and gives the packet to the MAC layer.
Experiment Report
Write a proper report using MS Word and include the results and discussions of your results in the
report. You should then pack this report with the programs you have written, zip them and send one file
to the E-mail address of the course. The received file should contain:
• The scripts written for each part
• The report that includes the results of simulations in the form of plots of NAM, tracegraph or other
forms of plots suitable. For each part, explain the results you have found and how they are related
to the theories discussed in the course.
Data Networks
Dr. M. Pakravan 12