Tutorial On Event Driven Simulations of Network Protocols
Sample Data Link Layer Protocol Simulation
Written by Tom Laramee, Dec. 1995

Written by Tom Laramee for use in the senior level ECE course in Computer Networks 597A, and for use in the graduate-level course ECE671.

Presented is a sample Data Link Layer protocol (Selective Repeat) simulation with sample output.

[ Tutorial Index | Lecture Notes | Skeleton Simulation Code | Media Access Simulation Code | Data Link Layer Simulation Code ]
Sample Data Link Layer protocol ("Selective repeat")

This example is fairly complex and likely to be of marginal value in helping you write your own simulations (due to the time constraint). I recommend reading this section, running the simulation (all 3), and viewing the OP. The code/scripts can also be used for ideas and enhancements, and for syntactical examples.

Obtaining the example

There are 3 zip files:

Download the 1st file, gunzip it, and untar it.

NOTE: The contents of this tape archive should reside in their own directory. If it is untarred in the same directory as either the skeleton or the other example, there will be a file collision and some files will be overwritten and lost.

The filelist should be as follows:

Running the example

There are 3 scripts for this example:

  • sim1.sh
  • sim2.sh
  • sim3.sh

They all run the same simulation - each collecting different statistics. This means that each has it's own sim.gnuplot.sh script:

  • sim1.gnuplot.sh
  • sim2.gnuplot.sh
  • sim3.gnuplot.sh

If you type:

sim1.sh

You will see the following:

	sim: This script will generate 2 plots:
	sim: 1) Channel utilization vs. Channel Length for varying Window size
	sim: 2) Channel utilization vs. Window Size for varying Channel Length
	sim:
	sim: Command line parameters are as follows...
	sim: ------------------------------------------------
	sim: Bits per ack................................: 8
	sim: Channel capacity (bps)......................: 14400
	sim: Channel length ( in frames )................: 0
	sim: Number of bits / data frame.................: 2000
	sim: Header bits / frame.........................: 400
	sim: Prob that a frame or ack is lost or damaged.: 0.0
	sim: Prob that a data frame is lost or damaged...: 0.0
	sim: Prob that an ACK frame is lost or damaged...: 0.0
	sim: Channel arrival rate........................: 25
	sim: Window size.................................: 1
	sim: Debugging mode..............................: 0
	sim: MAXEVENTS for simulation....................: 32000
	sim: MAXPACKETS for simulation...................: 1000
	sim:
	sim: Ready to begin (y/n) ??
	sim: Reminder: some files are about to be removed here...
	
        y                           ( <----- entered by the user)
	
        sim: Removing old output files...
	sim: Creating permanent data files...
	sim: Creating GNUplot files to generate the plots...
	sim.gnuplot.sh:
	sim.gnuplot.sh: Gnuplot driver file: data1.plot1
	sim.gnuplot.sh: Gnuplot data file: a.Final
	sim.gnuplot.sh: Gnuplot .ps plot: data1.plot1.ps
	sim.gnuplot.sh:
	sim.gnuplot.sh: Gnuplot driver file: data1.plot2
	sim.gnuplot.sh: Gnuplot data file: b.Final
	sim.gnuplot.sh: Gnuplot .ps plot: data1.plot2.ps
	sim.gnuplot.sh:
	sim: Compiling...
	gcc    -c main.c -o main.o
	gcc    -c event.c -o event.o
	gcc -o p5 main.o event.o -lm
	sim:
	sim: Running for several Window sizes (W=1, 3, 7)
	sim: Hit  to continue...

NOTE: If you get:

	sim: Compiling...
	sim.gnuplot.sh:	
	sim: Compiling...
	`sim' is up to date.
	sim:

...instead of:

	sim: Compiling...
	gcc    -c main.c -o main.o
	gcc    -c event.c -o event.o
	gcc -o p5 main.o event.o -lm

...this is o.k. - it just means the program doesn't need to be recompiled - it can be run as is.

The script will produce 2 plots:

  • Channel utilization vs. Channel Length for varying Window size
  • Channel utilization vs. Window Size for varying Channel Length

So, the script runs in 2 cycles.

  • In the 1st cycle, Window size is varied, and statistics are collected.
  • In the 2nd cycle, channel length is varied, and statistics are collected.

Then, the 2 plots may be Ghostviewed.

sim2.sh produces 2 plots:

  • Channel utilization vs. Channel Length for const Window size (7)
  • Channel utilization vs. Window Size for const Channle Length (3)
    ...while varying P1 - the probability that there is a lost/damaged data frame (0.2, 0.4, 0.8)

sim3.sh produces 2 plots:

  • Channel utilization vs. Channel Length for const Window size (7)
  • Channel utilization vs. Window Size for const Channel Length (3)
    ...while varying P2 - the probability that there is a lost/damaged ACK frame (0.2, 0.4, 0.8)

They are run (and they work) similary to sim1.sh.

Background on the protocol

This protocol is one of the "Sliding Window" protocols which is illustrated in the text:

Computer Networks
Andrew S. Tannenbaum

It is outlined in Chapter 4. I specifically used section 4.5 to code this protocol - which is referred to as "Selective Repeat" in the book.

Once again, it is beyond the scope of this module to detail how the actual protocol works.

Explanation of how I took the skeleton and used it to test a protocol

The genEvent? functions I added (to the skeleton) are:

  • gen_nodefree(); The event where a node becomes free to transmit
  • gen_depart(); The event where a packet departs from a node
  • gen_ackarrival(); An ACK arrives to a node
  • gen_timeout(); A node "times out"

The utility functions I added (to the skeleton) are:

  • printQueue(); Print the contents of a nodes queue
  • queueItUp(); Add a packet to the nodes queue
  • postCycleOP(); This is the "printStatus" function used in the skeleton

The processEvent? function I added (to the skeleton) are:

  • processFrameArrival(); Process the arrival of a frame to a node
  • processAckArrival(); Process the arrival of an ACK to a node
  • processTimeOut(); Process a "time out" event
  • processNodeFree(); Process the event where a node becomes free
  • processDeparture(); Process a departure from a node

Again, an explanation of the way these functions work is beyond the scope of this module. They are all quite short, so if you're interested in the data flow, it is obtainable directly from the code.

I modified main.c to add the above functions.

Specifically, I added:

  • Many of these to "initParams", "initAll", "showParams", and "showReport"
  • My "genEvent?" functions and "processEvent?" functions
  • Some utility functions to make the program more modular

The "printstatus()" function of the skeleton is called "postCycleOP()" here

Also, since this program is collecting 2 statistics, in the "showReport" function, I open 2 data files, "a", and "b". Each of these has it's own 2 number pair representing a statistic from the simulation.

After 1 run, the files look something like this:

(a) (b)
1.0 0.0549 1.0 0.0651

Since the program is being run but the script "sim1.sh", (or sim2.sh or sim3.sh) these files are being concatenated to 2 data files for plotting.

  • a.Final
  • b.Final

So, after 5 program runs, those data files look like this:

(a.Final) (b.Final)
1.0 0.0549 1.0 0.0651
2.0 0.0809 3.0 0.1121
3.0 0.1341 5.0 0.1765
4.0 0.3453 7.0 0.2310
5.0 0.4611 9.0 0.3541

These programs will be plotted using 2 GNUplot driver files:

  • data.plot1
  • data.plot2

The sim.sh script required the most extensive modificaiton. Again, if you are unfamiliar with the UNIX operating system, no amount of explanation will make this script clear.

This script esentially works the same way as the skeleton version - except it keeps track of only 2 statistics, and collecting them is more complicated in this example.

The script runs in 2 cycles (this was alluded to earlier).

  • In the 1ST cycle, window size is varied which channel length is held constant.
  • In the 2nd cycle, channel length is varied while window size is held constant.

The statistics are collected separately during each of these 2 cycles for plotting in the end.

Essentially: sim1.sh = sim2.sh = sim3.sh and sim1.gnuplot.sh = sim2.gnuplot.sh = sim3.gnuplot.sh
where "=" means is quite similar to.

The major differences are mentioned above - the plots which are produced vary slightly.

The scripts are liberally commented - so it is entirely possible to understand what it is doing by running it and looking at the source.

Return to the tutorial and examples...