Docsity

Network programming ( Ns-3 ), Study Guides, Projects, Research of Network Programming

Partial preview of the text

Download Network programming ( Ns-3 ) and more Study Guides, Projects, Research Network Programming in PDF only on Docsity!

ns-3 Tutorial

Release ns-3.

ns-3 project

March 21, 2018

  • 1 Introduction
    • 1.1 About ns-3
    • 1.2 For ns-2 Users
    • 1.3 Contributing
    • 1.4 Tutorial Organization
  • 2 Resources
    • 2.1 The Web
    • 2.2 Mercurial
    • 2.3 Waf
    • 2.4 Development Environment
    • 2.5 Socket Programming
  • 3 Getting Started
    • 3.1 Overview
    • 3.2 Downloading ns-3
    • 3.3 Building ns-3
    • 3.4 Testing ns-3
    • 3.5 Running a Script
  • 4 Conceptual Overview
    • 4.1 Key Abstractions
    • 4.2 A First ns-3 Script
    • 4.3 Ns-3 Source Code
  • 5 Tweaking
    • 5.1 Using the Logging Module
    • 5.2 Using Command Line Arguments
    • 5.3 Using the Tracing System
  • 6 Building Topologies
    • 6.1 Building a Bus Network Topology
    • 6.2 Models, Attributes and Reality
    • 6.3 Building a Wireless Network Topology
    • 6.4 Queues in ns-3
  • 7 Tracing
    • 7.1 Background
    • 7.2 Overview
    • 7.3 Real Example
    • 7.4 Trace Helpers
    • 7.5 Summary
  • 8 Data Collection
    • 8.1 Motivation
    • 8.2 Example Code
    • 8.3 GnuplotHelper
    • 8.4 Supported Trace Types
    • 8.5 FileHelper
    • 8.6 Summary
  • 9 Conclusion
    • 9.1 Futures
    • 9.2 Closing

ns-3 Tutorial, Release ns-3.

This is the ns-3 Tutorial. Primary documentation for the ns-3 project is available in five forms:

  • ns-3 Doxygen: Documentation of the public APIs of the simulator
  • Tutorial (this document), Manual, and Model Library for the latest release and development tree
  • ns-3 wiki

This document is written in reStructuredText for Sphinx and is maintained in the doc/tutorial directory of ns-3’s

source code.

CONTENTS 1

CHAPTER

ONE

INTRODUCTION

The ns-3 simulator is a discrete-event network simulator targeted primarily for research and educational use. The ns-

project, started in 2006, is an open-source project developing ns-3.

The purpose of this tutorial is to introduce new ns-3 users to the system in a structured way. It is sometimes difficult

for new users to glean essential information from detailed manuals and to convert this information into working

simulations. In this tutorial, we will build several example simulations, introducing and explaining key concepts and

features as we go.

As the tutorial unfolds, we will introduce the full ns-3 documentation and provide pointers to source code for those

interested in delving deeper into the workings of the system.

A few key points are worth noting at the onset:

  • ns-3 is open-source, and the project strives to maintain an open environment for researchers to contribute and

share their software.

  • ns-3 is not a backwards-compatible extension of ns-2; it is a new simulator. The two simulators are both written

in C++ but ns-3 is a new simulator that does not support the ns-2 APIs. Some models from ns-2 have already

been ported from ns-2 to ns-3. The project will continue to maintain ns-2 while ns-3 is being built, and will

study transition and integration mechanisms.

1.1 About ns-

ns-3 has been developed to provide an open, extensible network simulation platform, for networking research and

education. In brief, ns-3 provides models of how packet data networks work and perform, and provides a simulation

engine for users to conduct simulation experiments. Some of the reasons to use ns-3 include to perform studies

that are more difficult or not possible to perform with real systems, to study system behavior in a highly controlled,

reproducible environment, and to learn about how networks work. Users will note that the available model set in ns-

focuses on modeling how Internet protocols and networks work, but ns-3 is not limited to Internet systems; several

users are using ns-3 to model non-Internet-based systems.

Many simulation tools exist for network simulation studies. Below are a few distinguishing features of ns-3 in contrast

to other tools.

  • ns-3 is designed as a set of libraries that can be combined together and also with other external software libraries.

While some simulation platforms provide users with a single, integrated graphical user interface environment in

which all tasks are carried out, ns-3 is more modular in this regard. Several external animators and data analysis

and visualization tools can be used with ns-3. However, users should expect to work at the command line and

with C++ and/or Python software development tools.

  • ns-3 is primarily used on Linux systems, although support exists for FreeBSD, Cygwin (for Windows), and

native Windows Visual Studio support is in the process of being developed.

ns-3 Tutorial, Release ns-3.

  • ns-3 is not an officially supported software product of any company. Support for ns-3 is done on a best-effort

basis on the ns-3-users mailing list.

1.2 For ns-2 Users

For those familiar with ns-2 (a popular tool that preceded ns-3), the most visible outward change when moving to ns-

is the choice of scripting language. Programs in ns-2 are scripted in OTcl and results of simulations can be visualized

using the Network Animator nam. It is not possible to run a simulation in ns-2 purely from C++ (i.e., as a main()

program without any OTcl). Moreover, some components of ns-2 are written in C++ and others in OTcl. In ns-3,

the simulator is written entirely in C++, with optional Python bindings. Simulation scripts can therefore be written in

C++ or in Python. New animators and visualizers are available and under current development. Since ns-3 generates

pcap packet trace files, other utilities can be used to analyze traces as well. In this tutorial, we will first concentrate on

scripting directly in C++ and interpreting results via trace files.

But there are similarities as well (both, for example, are based on C++ objects, and some code from ns-2 has already

been ported to ns-3). We will try to highlight differences between ns-2 and ns-3 as we proceed in this tutorial.

A question that we often hear is “Should I still use ns-2 or move to ns-3?” In this author’s opinion, unless the user is

somehow vested in ns-2 (either based on existing personal comfort with and knowledge of ns-2, or based on a specific

simulation model that is only available in ns-2), a user will be more productive with ns-3 for the following reasons:

  • ns-3 is actively maintained with an active, responsive users mailing list, while ns-2 is only lightly maintained

and has not seen significant development in its main code tree for over a decade.

  • ns-3 provides features not available in ns-2, such as a implementation code execution environment (allowing

users to run real implementation code in the simulator)

  • ns-3 provides a lower base level of abstraction compared with ns-2, allowing it to align better with how real

systems are put together. Some limitations found in ns-2 (such as supporting multiple types of interfaces on

nodes correctly) have been remedied in ns-3.

ns-2 has a more diverse set of contributed modules than does ns-3, owing to its long history. However, ns-3 has

more detailed models in several popular areas of research (including sophisticated LTE and WiFi models), and its

support of implementation code admits a very wide spectrum of high-fidelity models. Users may be surprised to learn

that the whole Linux networking stack can be encapsulated in an ns-3 node, using the Direct Code Execution (DCE)

framework. ns-2 models can sometimes be ported to ns-3, particularly if they have been implemented in C++.

If in doubt, a good guideline would be to look at both simulators (as well as other simulators), and in particular the

models available for your research, but keep in mind that your experience may be better in using the tool that is being

actively developed and maintained (ns-3).

1.3 Contributing

ns-3 is a research and educational simulator, by and for the research community. It will rely on the ongoing contribu-

tions of the community to develop new models, debug or maintain existing ones, and share results. There are a few

policies that we hope will encourage people to contribute to ns-3 like they have for ns-2:

  • Open source licensing based on GNU GPLv2 compatibility
  • wiki
  • Contributed Code page, similar to ns-2‘s popular Contributed Code page
  • Open bug tracker

4 Chapter 1. Introduction

ns-3 Tutorial, Release ns-3.

6 Chapter 1. Introduction

CHAPTER

TWO

RESOURCES

2.1 The Web

There are several important resources of which any ns-3 user must be aware. The main web site is located at

http://www.nsnam.org and provides access to basic information about the ns-3 system. Detailed documentation is

available through the main web site at http://www.nsnam.org/documentation/. You can also find documents relating to

the system architecture from this page.

There is a Wiki that complements the main ns-3 web site which you will find at http://www.nsnam.org/wiki/. You will

find user and developer FAQs there, as well as troubleshooting guides, third-party contributed code, papers, etc.

The source code may be found and browsed at http://code.nsnam.org/. There you will find the current development

tree in the repository named ns-3-dev. Past releases and experimental repositories of the core developers may also

be found there.

2.2 Mercurial

Complex software systems need some way to manage the organization and changes to the underlying code and docu-

mentation. There are many ways to perform this feat, and you may have heard of some of the systems that are currently

used to do this. The Concurrent Version System (CVS) is probably the most well known.

The ns-3 project uses Mercurial as its source code management system. Although you do not need to know much

about Mercurial in order to complete this tutorial, we recommend becoming familiar with Mercurial and using it

to access the source code. Mercurial has a web site at http://www.selenic.com/mercurial/, from which you can get

binary or source releases of this Software Configuration Management (SCM) system. Selenic (the developer of Mer-

curial) also provides a tutorial at http://www.selenic.com/mercurial/wiki/index.cgi/Tutorial/, and a QuickStart guide at

http://www.selenic.com/mercurial/wiki/index.cgi/QuickStart/.

You can also find vital information about using Mercurial and ns-3 on the main ns-3 web site.

2.3 Waf

Once you have source code downloaded to your local system, you will need to compile that source to produce usable

programs. Just as in the case of source code management, there are many tools available to perform this function.

Probably the most well known of these tools is make. Along with being the most well known, make is probably

the most difficult to use in a very large and highly configurable system. Because of this, many alternatives have been

developed. Recently these systems have been developed using the Python language.

The build system Waf is used on the ns-3 project. It is one of the new generation of Python-based build systems. You

will not need to understand any Python to build the existing ns-3 system.

CHAPTER

THREE

GETTING STARTED

This section is aimed at getting a user to a working state starting with a machine that may never have had ns-3 installed.

It covers supported platforms, prerequisites, ways to obtain ns-3, ways to build ns-3, and ways to verify your build and

run simple programs.

3.1 Overview

ns-3 is built as a system of software libraries that work together. User programs can be written that links with (or

imports from) these libraries. User programs are written in either the C++ or Python programming languages.

ns-3 is distributed as source code, meaning that the target system needs to have a software development environment

to build the libraries first, then build the user program. ns-3 could in principle be distributed as pre-built libraries for

selected systems, and in the future it may be distributed that way, but at present, many users actually do their work

by editing ns-3 itself, so having the source code around to rebuild the libraries is useful. If someone would like to

undertake the job of making pre-built libraries and packages for operating systems, please contact the ns-developers

mailing list.

In the following, we’ll look at two ways of downloading and building ns-3. The first is to download and build an

official release from the main web site. The second is to fetch and build development copies of ns-3. We’ll walk

through both examples since the tools involved are slightly different.

3.2 Downloading ns-

The ns-3 system as a whole is a fairly complex system and has a number of dependencies on other components.

Along with the systems you will most likely deal with every day (the GNU toolchain, Mercurial, a text editor) you

will need to ensure that a number of additional libraries are present on your system before proceeding. ns-3 pro-

vides a wiki page that includes pages with many useful hints and tips. One such page is the “Installation” page,

http://www.nsnam.org/wiki/Installation.

The “Prerequisites” section of this wiki page explains which packages are required to support common ns-3 options,

and also provides the commands used to install them for common Linux variants. Cygwin users will have to use the

Cygwin installer (if you are a Cygwin user, you used it to install Cygwin).

You may want to take this opportunity to explore the ns-3 wiki a bit since there really is a wealth of information there.

From this point forward, we are going to assume that the reader is working in Linux or a Linux emulation environment

(Linux, Cygwin, etc.) and has the GNU toolchain installed and verified along with the prerequisites mentioned above.

We are also going to assume that you have Mercurial and Waf installed and running on the target system.

The ns-3 code is available in Mercurial repositories on the server http://code.nsnam.org. You can also download a

tarball release at http://www.nsnam.org/release/, or you can work with repositories using Mercurial. We recommend

ns-3 Tutorial, Release ns-3.

using Mercurial unless there’s a good reason not to. See the end of this section for instructions on how to get a tarball

release.

The simplest way to get started using Mercurial repositories is to use the ns-3-allinone environment. This is a

set of scripts that manages the downloading and building of various subsystems of ns-3 for you. We recommend that

you begin your ns-3 work in this environment.

One practice is to create a directory called workspace in one’s home directory under which one can keep local

Mercurial repositories. Any directory name will do, but we’ll assume that workspace is used herein (note: repos

may also be used in some documentation as an example directory name).

3.2.1 Downloading ns-3 Using a Tarball

A tarball is a particular format of software archive where multiple files are bundled together and the archive possibly

compressed. ns-3 software releases are provided via a downloadable tarball. The process for downloading ns-3 via

tarball is simple; you just have to pick a release, download it and decompress it.

Let’s assume that you, as a user, wish to build ns-3 in a local directory called workspace. If you adopt the

workspace directory approach, you can get a copy of a release by typing the following into your Linux shell

(substitute the appropriate version numbers, of course):

$ cd $ mkdir workspace $ cd workspace $ wget http://www.nsnam.org/release/ns-allinone-3.27.tar.bz $ tar xjf ns-allinone-3.27.tar.bz

If you change into the directory ns-allinone-3.27 you should see a number of files and directories:

$ ls bake constants.py ns-3.27 README build.py netanim-3.108 pybindgen-0.17.0.post58+ngcf00cc0 util.py

You are now ready to build the base ns-3 distribution and may skip ahead to the section on building ns-3.

3.2.2 Downloading ns-3 Using Bake

Bake is a tool for distributed integration and building, developed for the ns-3 project. Bake can be used to fetch

development versions of the ns-3 software, and to download and build extensions to the base ns-3 distribution, such

as the Direct Code Execution environment, Network Simulation Cradle, ability to create new Python bindings, and

others.

In recent ns-3 releases, Bake has been included in the release tarball. The configuration file included in the released

version will allow one to download any software that was current at the time of the release. That is, for example, the

version of Bake that is distributed with the ns-3.21 release can be used to fetch components for that ns-3 release or

earlier, but can’t be used to fetch components for later releases (unless the bakeconf.xml file is updated).

You can also get the most recent copy of bake by typing the following into your Linux shell (assuming you have

installed Mercurial):

$ cd $ mkdir workspace $ cd workspace $ hg clone http://code.nsnam.org/bake

As the hg (Mercurial) command executes, you should see something like the following displayed,

10 Chapter 3. Getting Started

ns-3 Tutorial, Release ns-3.

$ ./bake.py check

You should see something like the following,

Python - OK GNU C++ compiler - OK Mercurial - OK CVS - OK GIT - OK Bazaar - OK Tar tool - OK Unzip tool - OK Unrar tool - is missing 7z data compression utility - OK XZ data compression utility - OK Make - OK cMake - OK patch tool - OK autoreconf tool - OK

Path searched for tools: /usr/lib64/qt-3.3/bin /usr/lib64/ccache /usr/local/bin /bin /usr/bin /usr/local/sbin /usr/sbin /sbin bin

In particular, download tools such as Mercurial, CVS, GIT, and Bazaar are our principal concerns at this point, since

they allow us to fetch the code. Please install missing tools at this stage, in the usual way for your system (if you are

able to), or contact your system administrator as needed to install these tools.

Next, try to download the software:

$ ./bake.py download

should yield something like:

Searching for system dependency setuptools - OK Searching for system dependency pygoocanvas - OK Searching for system dependency pygraphviz - OK Searching for system dependency python-dev - OK Searching for system dependency libxml2-dev - OK Searching for system dependency clang-dev - OK Downloading click-ns-3.25 - OK Downloading BRITE - OK Searching for system dependency qt4 - OK Downloading nsc-0.5.3 - OK Searching for system dependency g++ - OK Downloading castxml - OK Downloading openflow-ns-3.25 - OK Downloading netanim-3.108 - OK Downloading pygccxml-1.9.1 - OK Downloading pygccxml - OK Downloading pybindgen-ns3.27-castxml (target directory:pybindgen) - OK Downloading ns-3.27 - OK

The above suggests that seven sources have been downloaded. Check the source directory now and type ls; one

should see:

$ ls BRITE netanim-3.108 openflow-ns-3.25 pygccxml-1.9. castxml ns-3.27 pybindgen v1.9.1.tar.gz click-ns-3.25 nsc-0.5.3 pygccxml

12 Chapter 3. Getting Started

ns-3 Tutorial, Release ns-3.

You are now ready to build the ns-3 distribution.

3.3 Building ns-

3.3.1 Building with build.py

When working from a released tarball, the first time you build the ns-3 project you can build using a convenience

program found in the allinone directory. This program is called build.py. This program will get the project

configured for you in the most commonly useful way. However, please note that more advanced configuration and

work with ns-3 will typically involve using the native ns-3 build system, Waf, to be introduced later in this tutorial.

If you downloaded using a tarball you should have a directory called something like ns-allinone-3.27 under

your ~/workspace directory. Type the following:

$ ./build.py --enable-examples --enable-tests

Because we are working with examples and tests in this tutorial, and because they are not built by default in ns-3, the

arguments for build.py tells it to build them for us. The program also defaults to building all available modules. Later,

you can build ns-3 without examples and tests, or eliminate the modules that are not necessary for your work, if you

wish.

You will see lots of typical compiler output messages displayed as the build script builds the various pieces you

downloaded. Eventually you should see the following:

Waf: Leaving directory `/path/to/workspace/ns-allinone-3.27/ns-3.27/build' 'build' finished successfully (6m25.032s)

Modules built: antenna aodv applications bridge buildings config-store core csma csma-layout dsdv dsr energy fd-net-device flow-monitor internet internet-apps lr-wpan lte mesh mobility mpi netanim (no Python) network nix-vector-routing olsr openflow (no Python) point-to-point point-to-point-layout propagation sixlowpan spectrum stats tap-bridge test (no Python) topology-read traffic-control uan virtual-net-device visualizer wave wifi wimax

Modules not built (see ns-3 tutorial for explanation): brite click

Regarding the portion about modules not built:

Modules not built (see ns-3 tutorial for explanation): brite click

This just means that some ns-3 modules that have dependencies on outside libraries may not have been built, or that

the configuration specifically asked not to build them. It does not mean that the simulator did not build successfully or

that it will provide wrong results for the modules listed as being built.

3.3. Building ns-3 13

ns-3 Tutorial, Release ns-3.

Setting top to :. Setting out to : build Checking for 'gcc' (c compiler) : /usr/bin/gcc Checking for cc version : 4.2. Checking for 'g++' (c++ compiler) : /usr/bin/g++ Checking boost includes : 1_46_ Checking boost libs : ok Checking for boost linkage : ok Checking for click location : not found Checking for program pkg-config : /sw/bin/pkg-config Checking for 'gtk+-2.0' >= 2.12 : yes Checking for 'libxml-2.0' >= 2.7 : yes Checking for type uint128_t : not found Checking for type __uint128_t : yes Checking high precision implementation : 128-bit integer (default) Checking for header stdint.h : yes Checking for header inttypes.h : yes Checking for header sys/inttypes.h : not found Checking for header sys/types.h : yes Checking for header sys/stat.h : yes Checking for header dirent.h : yes Checking for header stdlib.h : yes Checking for header signal.h : yes Checking for header pthread.h : yes Checking for header stdint.h : yes Checking for header inttypes.h : yes Checking for header sys/inttypes.h : not found Checking for library rt : not found Checking for header netpacket/packet.h : not found Checking for header sys/ioctl.h : yes Checking for header net/if.h : not found Checking for header net/ethernet.h : yes Checking for header linux/if_tun.h : not found Checking for header netpacket/packet.h : not found Checking for NSC location : not found Checking for 'mpic++' : yes Checking for 'sqlite3' : yes Checking for header linux/if_tun.h : not found Checking for program sudo : /usr/bin/sudo Checking for program valgrind : /sw/bin/valgrind Checking for 'gsl' : yes Checking for compilation flag -Wno-error=deprecated-d... support : ok Checking for compilation flag -Wno-error=deprecated-d... support : ok Checking for compilation flag -fstrict-aliasing... support : ok Checking for compilation flag -fstrict-aliasing... support : ok Checking for compilation flag -Wstrict-aliasing... support : ok Checking for compilation flag -Wstrict-aliasing... support : ok Checking for program doxygen : /usr/local/bin/doxygen ---- Summary of optional NS-3 features: Build profile : debug BRITE Integration : not enabled (BRITE not enabled (see option --with-brite)) Build directory : build Build examples : enabled Build tests : enabled Emulated Net Device : enabled (<netpacket/packet.h> include not detected) Emulation FdNetDevice : not enabled (needs netpacket/packet.h) File descriptor NetDevice : enabled GNU Scientific Library (GSL) : enabled

3.3. Building ns-3 15

ns-3 Tutorial, Release ns-3.

GtkConfigStore : enabled MPI Support : enabled NS-3 Click Integration : not enabled (nsclick not enabled (see option --with-nsclick)) NS-3 OpenFlow Integration : not enabled (Required boost libraries not found, missing: system, sig Network Simulation Cradle : not enabled (NSC not found (see option --with-nsc)) PlanetLab FdNetDevice : not enabled (PlanetLab operating system not detected (see option --fo PyViz visualizer : enabled Python Bindings : enabled Real Time Simulator : enabled (librt is not available) SQlite stats data output : enabled Tap Bridge : not enabled (<linux/if_tun.h> include not detected) Tap FdNetDevice : not enabled (needs linux/if_tun.h) Threading Primitives : enabled Use sudo to set suid bit : not enabled (option --enable-sudo not selected) XmlIo : enabled 'configure' finished successfully (1.944s)

Note the last part of the above output. Some ns-3 options are not enabled by default or require support from the

underlying system to work properly. For instance, to enable XmlTo, the library libxml-2.0 must be found on the

system. If this library were not found, the corresponding ns-3 feature would not be enabled and a message would be

displayed. Note further that there is a feature to use the program sudo to set the suid bit of certain programs. This

is not enabled by default and so this feature is reported as “not enabled.” Finally, to reprint this summary of which

optional features are enabled, use the --check-config option to waf.

Now go ahead and switch back to the debug build that includes the examples and tests.

$ ./waf clean $ ./waf configure --build-profile=debug --enable-examples --enable-tests

The build system is now configured and you can build the debug versions of the ns-3 programs by simply typing

$ ./waf

Okay, sorry, I made you build the ns-3 part of the system twice, but now you know how to change the configuration

and build optimized code.

A command exists for checking which profile is currently active for an already configured project:

$ ./waf --check-profile Waf: Entering directory `/path/to/ns-3-allinone/ns-3.27/build' Build profile: debug

The build.py script discussed above supports also the --enable-examples and enable-tests arguments, but

in general, does not directly support other waf options; for example, this will not work:

$ ./build.py --disable-python

will result in

build.py: error: no such option: --disable-python

However, the special operator -- can be used to pass additional options through to waf, so instead of the above, the

following will work:

$ ./build.py -- --disable-python

as it generates the underlying command ./waf configure --disable-python.

Here are a few more introductory tips about Waf.

16 Chapter 3. Getting Started