17 The ns-3 Network Simulator
In this chapter we take a somewhat cursory look at the ns-3 simulator, intended as a replacement for ns-2. The project is managed by the NS-3 Consortium, and all materials are available at nsnam.org.
Ns-3 represents a rather sharp break from ns-2. Gone is the Tcl programming interface; instead, ns-3 simulation programs are written in the C++ language, with extensive calls to the ns-3 library, although they are often still referred to as simulation “scripts”. As the simulator core itself is also written in C++, this in some cases allows improved interaction between configuration and execution. However, configuration and execution are still in most cases quite separate: at the end of the simulation script comes a call
Simulator::Run() – akin to ns-2’s
$ns run – at which point the user-written C++ has done its job and the library takes over.
To configure a simple simulation, an ns-2 Tcl script had to create nodes and links, create network-connection “agents” attached to nodes, and create traffic-generating applications attached to agents. Much the same applies to ns-3, but in addition each node must be configured with its network interfaces, and each network interface must be assigned an IP address.
17.1 Installing and Running ns-3
We here outline the steps for installing ns-3 under Linux from the “allinone”
tar file, assuming that all prerequisite packages (such as gcc) are already in place. Much more general installation instructions can be found at www.nsnam.org. In particular, serious users are likely to want to download the current Mercurial repository directly. Information is also available for Windows and Macintosh installation, although perhaps the simplest option for Windows users is to run ns-3 in a Linux virtual machine.
The first step is to unzip the
tar file; this should leave a directory named ns-allinone-3.nn, where nn reflects the version number (20 in the author’s installation as of this 2014 writing). This directory is the root of the ns-3 system; it contains a
build.py (python) script and the primary ns-3 directory ns-3.nn. All that is necessary is to run the build.py script:
Considerable configuration and then compiler output should ensue, hopefully terminating with a list of “Modules built” and “Modules not built”.
From this point on, most ns-3 work will take place in the subdirectory ns-3.nn, that is, in ns-allinone-3.nn/ns-3.nn. This development directory contains the source directory
src, the script directory
scratch, and the execution script waf .
The development directory also contains a directory
examples containing a rich set of example scripts. The scripts in
examples/tutorial are described in depth in the ns-3 tutorial in
17.1.1 Running a Script
Let us now run a script, for example, the file first.cc included in the
examples/tutorial directory. We first copy this file into the directory “scratch”, and then, in the parent development directory, enter the command
./waf --run first
The program is compiled and, if compilation is successful, is run.
In fact, every uncompiled program in the scratch directory is compiled, meaning that projects in progress that are not yet compilable must be kept elsewhere. One convenient strategy is to maintain multiple project directories, and link them symbolically to
scratch as needed.
The ns-3 system includes support for command-line options; the following example illustrates the passing by command line of the value 3 for the variable
./waf --run "second --nCsma=3"