In previous post, we wrote an NS2 simulation program that fits the project specification, except that we're using the standard 802.11 MAC protocol. In this post, we'll discuss how to add our own MAC protocol to NS2.
Compile NS2 from Source
To add an new protocol to NS2, we actually need to download the whole NS2 source tree and add some extra CPP files there, which is embarrassingly inconvenient. But for now, we have to live with it.
Anyways, download the NS2 all-in-one package from here, put the
tarball somewhere in your home, say
~/projects/, then extract it.
cd ~/projects tar xvf ns2-allinone-2.35.tar.gz cd ns2-allinone-2.35 ./install
The install script will only generate the binaries in current directory, and
will NOT actually copy them to anywhere. After the compilation is done, you'll
will find the
ns executable in the
Suppose you put your project files (e.g., the TCL file we wrote) in
~/projects/network/ns2, then it's convenient to have an symbol link to the
cd ~/projects/network/ns2 ln -svf ~/projects/ns2-allinone-2.35/ns-2.35/ns myns
The you'll have an symbol link called
myns, which points the actual
executable. Then you can run your simulation this way.
In which the
random_mac.tcl is the TCL file we wrote in last post.
Add a New Mac Protocol
To add a new MAC protocol, say
RMAC, we need to do the following. Suppose
you're in the
rmac.hfile in the
macsubdirectory, for now, just leave them empty.
Makefile, find the line contains
mac/smac.o(around line 249), add one line like this
..... mac/mac-802_3.o mac/mac-tdma.o mac/smac.o \ mac/rmac.o\ .....
So now, when you do
make inside the
ns-2.35 directory, our source
rmac.h will be compiled. Of course, at this point, there is
no content at those two files, which we'll add later.
The NS2 source contains a simple MAC protocol called
SimpleMac, which is a
good start point for us to adapt.
Just copy all the contents in
mac/rmac.cc. Then change
line 60 of the
rmac.cc file. You should be able to compile using the
If everything is OK, go back to the project directory
change the MAC protocol to
Mac/802.11), you should be
able to run the simulation using
myns, which points to the
ns binary we just