NS-3

https://www.nsnam.org/about/

  • GNU GPL Version 2.0

https://www.nsnam.org/docs/manual/html/python.html

pip install --user ns3

Considerable differences in user experience can be observed between the version installed via pip and built from source with the 'Python Bindings' option.
The source-built version with Python Bindings provides greater flexibility and enables a wider range of functionality.

In case of the pip-installed version:

  • Useful modules provided under 'contrib' like 'internet-apps' and 'flow-monitor' are not available.
  • Sub-modules like ns.core and ns.network cannot be imported in python.
  • Appropriate definitions should be added in ns.cppyy.cppdef if not supported in C++/Cppyy.

Troubleshooting - No Matching Distribution

## ERROR: Could not find a version that satisfies the requirement ns3 (from versions: none)
## ERROR: No matching distribution found for ns3
pip install --upgrade setuptools
## Minimal Requirement
sudo apt install g++ python3 cmake ninja-build git
## Recommended for Compiler Cache Optimization
sudo apt install ccache

Information - Optional Components

## Python Visualizer and Bindings
#python3 -m pip install --user cppyy
#python3 -m pip install --user cppyy==2.4.2
python3 -m pip install --user cppyy==3.1.2
sudo apt install gir1.2-goocanvas-2.0 python3-gi python3-gi-cairo python3-pygraphviz gir1.2-gtk-3.0 ipython3
## Python Development
sudo apt install python3-setuptools git
## Netanim Animator
sudo apt install qtbase5-dev qtchooser qt5-qmake qtbase5-dev-tools
sudo apt install qt5-default
## PyViz Visualizer
sudo apt install gir1.2-goocanvas-2.0 python3-gi python3-gi-cairo python3-pygraphviz gir1.2-gtk-3.0 ipython3
## MPI-Based Distributed Emulation
sudo apt install openmpi-bin openmpi-common openmpi-doc libopenmpi-dev
## Bake Build Tool
sudo apt install mercurial unzip
## Debugging
sudo apt install gdb valgrind
## Code Style Check
sudo apt install clang-format
## Inline Documentation
#sudo apt install doxygen graphviz imagemagick
#sudo apt install texlive texlive-extra-utils texlive-latex-extra texlive-font-utils dvipng latexmk
## Manual and Tutorial
#sudo apt install python3-sphinx dia
## GNU Scientific Library (GSL) for 802.11b WiFi Error Models
sudo apt install gsl-bin libgsl-dev libgslcblas0
## PCAP Packet Traces
sudo apt install tcpdump
## Statistics Framework
sudo apt install sqlite sqlite3 libsqlite3-dev
## Config Store
sudo apt install libxml2 libxml2-dev
## GTK-Based Configuration System
sudo apt install libgtk-3-dev
## Experiment with Virtual Machines
sudo apt install vtun lxc uml-utilities
## Openflow Module and Boost Development Libraries
sudo apt install libxml2 libxml2-dev libboost-all-dev
mkdir ns-3
cd ns-3
git clone https://gitlab.com/nsnam/bake
export BAKE_HOME=`pwd`/bake
export PATH=$PATH:$BAKE_HOME
export PYTHONPATH=$PYTHONPATH:$BAKE_HOME
bake.py check
#bake.py configure -e ns-3.42
bake.py configure -e ns-allinone-3.42
bake.py show
## -- System Dependencies --
##  > automake - OK
##  > cmake - OK
##  > cppyy - OK
##  > g++ - OK
##  > gi-cairo - OK
##  > gir-bindings - OK
##  > libxml2-dev - OK
##  > mercurial - OK
##  > pygobject - OK
##  > pygraphviz - OK
##  > python3-dev - OK
##  > qt - OK
bake.py deploy
#bake.py download
#bake.py build
## Downloading, building and installing the selected modules and dependencies.
## Please, be patient, this may take a while!
##  >> Searching for system dependency libxml2-dev - OK
##  >> Downloading click-ns-3.37 - OK
##  >> Searching for system dependency g++ - OK
##  >> Searching for system dependency cppyy - OK
##  >> Searching for system dependency gi-cairo - OK
##  >> Searching for system dependency gir-bindings - OK
##  >> Searching for system dependency pygobject - OK
##  >> Searching for system dependency pygraphviz - OK
##  >> Searching for system dependency python3-dev - OK
##  >> Searching for system dependency qt - OK
##  >> Searching for system dependency cmake - OK
##  >> Searching for system dependency mercurial - OK
##  >> Searching for system dependency automake - OK
##  >> Downloading openflow-dev - OK
##  >> Downloading netanim-3.109 - OK
##  >> Downloading BRITE - OK
##  >> Downloading ns-3.42 (target directory:ns-3.42) - OK
##  >> Building click-ns-3.37 - OK
##  >> Building openflow-dev - OK
##  >> Building netanim-3.109 - OK
##  >> Building BRITE - OK
##  >> Building ns-3.42 - OK
cd source/ns-3.42
./ns3 show
## Build profile: default
## ns-3 version: ns-3.42
## Reconfigure with './ns3 configure --enable-build-version' to bake the version into the libraries.
## ---- Summary of ns-3 settings:
## Build profile                 : default
## Build directory               : /home/dai/source/ns-3.42/build
## Build with runtime asserts    : ON
## Build with runtime logging    : ON
## Build version embedding       : OFF (not requested)
## BRITE Integration             : ON
## DES Metrics event collection  : OFF (not requested)
## DPDK NetDevice                : OFF (not requested)
## Emulation FdNetDevice         : ON
## Examples                      : ON
## File descriptor NetDevice     : ON
## GNU Scientific Library (GSL)  : ON
## GtkConfigStore                : ON
## LibXml2 support               : ON
## MPI Support                   : OFF (not requested)
## ns-3 Click Integration        : ON
## ns-3 OpenFlow Integration     : ON
## Netmap emulation FdNetDevice  : OFF (missing dependency)
## PyViz visualizer              : OFF (missing Python Bindings)
## Python Bindings               : OFF (missing dependency: cppyy)
## SQLite support                : ON
## Eigen3 support                : OFF (Eigen was not found)
## Tap Bridge                    : ON
## Tap FdNetDevice               : ON
## Tests                         : ON
## 
## Modules configured to be built:
## antenna                   aodv                      applications              
## bridge                    brite                     buildings                 
## click                     config-store              core                      
## csma                      csma-layout               dsdv                      
## dsr                       energy                    fd-net-device             
## flow-monitor              internet                  internet-apps             
## lr-wpan                   lte                       mesh                      
## mobility                  netanim                   network                   
## nix-vector-routing        olsr                      openflow                  
## point-to-point            point-to-point-layout     propagation               
## sixlowpan                 spectrum                  stats                     
## tap-bridge                test                      topology-read             
## traffic-control           uan                       virtual-net-device        
## wifi                      wimax                     
## 
## Modules that cannot be built:
## mpi                       visualizer                
## 
## ---- Summary of buildable targets:
## Buildable targets:            80
## Runnable/Buildable targets:  294
#./ns3 configure --enable-examples --enable-tests
./ns3 configure --enable-examples --enable-tests --enable-python-bindings
./ns3 build

Make sure to check the status before building; the package build will fail or not proceed if the status shows 'OFF'.

Troubleshooting - Cppyy Missing Dependency

## -- Bindings: python bindings disabled due to the following missing dependencies: cppyy
## Python Bindings               : OFF (missing dependency: cppyy)

To be investigated; using pip would be alternatives.

Troubleshooting - Bindings Install Directory

## -- NS3_BINDINGS_INSTALL_DIR was not set. The python bindings won't be installed with ./ns3 install. This setting is meant for packaging and redistribution.
## -- Set NS3_BINDINGS_INSTALL_DIR="/home/user/.local/lib/python3.8/site-packages" to install it to the default location.

To be investigated; just ignoring this warning for the moment.

./test.py
## [  0%] Building CXX object src/antenna/CMakeFiles/libantenna-obj.dir/model/circular-aperture-antenna-model.cc.o
## [  0%] Building CXX object src/antenna/CMakeFiles/libantenna-obj.dir/model/angles.cc.o
## [  0%] Building CXX object src/antenna/CMakeFiles/libantenna-obj.dir/model/antenna-model.cc.o
## [  0%] Building CXX object src/antenna/CMakeFiles/libantenna-obj.dir/model/cosine-antenna-model.cc.o
## [  0%] Building CXX object src/antenna/CMakeFiles/libantenna-obj.dir/model/isotropic-antenna-model.cc.o
## ...
## [773/780] PASS: Example src/buildings/examples/buildings-pathloss-profiler
## [774/780] PASS: Example src/buildings/examples/outdoor-group-mobility-example --useHelper=0
## [775/780] PASS: Example src/buildings/examples/outdoor-group-mobility-example --useHelper=1
## [776/780] PASS: Example src/brite/examples/brite-generic-example
## [777/780] PASS: Example src/bridge/examples/csma-bridge
## [778/780] PASS: Example src/bridge/examples/csma-bridge-one-hop
## [779/780] PASS: Example src/aodv/examples/aodv
## 780 of 780 tests passed (780 passed, 0 skipped, 0 failed, 0 crashed, 0 valgrind errors)
./ns3 run hello-simulator
## [  0%] Building CXX object examples/tutorial/CMakeFiles/hello-simulator.dir/hello-simulator.cc.o
## [  0%] Linking CXX executable ../../../build/examples/tutorial/ns3.42-hello-simulator-default
## Hello Simulator
## Default Network Topology
##       10.1.1.0
## n0 -------------- n1
##    point-to-point
./ns3 run first
## [  0%] Building CXX object examples/tutorial/CMakeFiles/first.dir/first.cc.o
## [  0%] Linking CXX executable ../../../build/examples/tutorial/ns3.42-first-default
## At time +2s client sent 1024 bytes to 10.1.1.2 port 9
## At time +2.00369s server received 1024 bytes from 10.1.1.1 port 49153
## At time +2.00369s server sent 1024 bytes to 10.1.1.1 port 49153
## At time +2.00737s client received 1024 bytes from 10.1.1.2 port 9
../netanim-3.109/NetAnim
## Open animation.xml on NetAnim


Troubleshooting - No Visualizer

https://www.nsnam.org/wiki/PyViz

./ns3 run --vis first
## NS_ASSERT failed, cond="uid != 0", msg="Assert in TypeId::LookupByName: ns3::VisualSimulatorImpl not found", file=source/ns-3.42/src/core/model/type-id.cc, line=840
## NS_FATAL, terminating
## terminate called without an active exception
## Command 'build/examples/tutorial/ns3.42-first-default --SimulatorImplementationType=ns3::VisualSimulatorImpl' died with <Signals.SIGABRT: 6>.
## Check if Python Bindings and PyViz Visualizer are ON
./ns3 show
./ns3 run star-animation
../netanim-3.109/NetAnim
## Open star-animation.xml on NetAnim


./ns3 run dumbbell-animation
../netanim-3.109/NetAnim
## Open dumbbell-animation.xml on NetAnim


python3 ./examples/tutorial/first.py
## At time +2s client sent 1024 bytes to 10.1.1.2 port 9
## At time +2.00369s server received 1024 bytes from 10.1.1.1 port 49153
## At time +2.00369s server sent 1024 bytes to 10.1.1.1 port 49153
## At time +2.00737s client received 1024 bytes from 10.1.1.2 port 9

Troubleshooting - No Mobility Model

## [runStaticInitializersOnce]: Failed to materialize symbols: { (main, { $.cling-module-141.__inits.0, __orc_init_func.cling-module-141, _GLOBAL__sub_I_cling_module_141, _ZN3ns3L16g_timeInitHelperE, __cxx_global_var_initcling_module_141_ }) }
## [runStaticInitializersOnce]: Failed to materialize symbols: { (main, { __orc_init_func.cling-module-141 }) }

To be investigated; just ignoring this failures for the moment.

Troubleshooting - No Mobility Model

## AnimationInterface WARNING:Node:0 Does not have a mobility model. Use SetConstantPosition if it is stationary
## AnimationInterface WARNING:Node:1 Does not have a mobility model. Use SetConstantPosition if it is stationary
mobility = ns.MobilityHelper()
mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel")
mobility.Install(nodes)

ns3_relay.tar.gz

from ns import ns

## ==============================================
## 1. Simulation Setup
## ==============================================
## Logging Configuration
ns.LogComponentEnable("UdpEchoClientApplication", ns.LOG_LEVEL_INFO)
ns.LogComponentEnable("UdpEchoServerApplication", ns.LOG_LEVEL_INFO)

## ==============================================
## 2. Network Topology
## ==============================================
## Node Creation
nodes = ns.NodeContainer()
nodes.Create(4)

## Node Containers for Point-to-Point Links
nodes_01 = ns.NodeContainer()
nodes_01.Add(nodes.Get(0))
nodes_01.Add(nodes.Get(1))
nodes_12 = ns.NodeContainer()
nodes_12.Add(nodes.Get(1))
nodes_12.Add(nodes.Get(2))
nodes_23 = ns.NodeContainer()
nodes_23.Add(nodes.Get(2))
nodes_23.Add(nodes.Get(3))

## Helper Function to Configure Point-to-Point Channels
def mkp2p():
    pointToPoint = ns.PointToPointHelper()
    pointToPoint.SetDeviceAttribute("DataRate", ns.StringValue("5Mbps"))
    pointToPoint.SetChannelAttribute("Delay", ns.StringValue("2ms"))
    return pointToPoint

## Link Installation (Devices)
p2p01 = mkp2p()
devices_01 = p2p01.Install(nodes_01)
p2p12 = mkp2p()
devices_12 = p2p12.Install(nodes_12)
p2p23 = mkp2p()
devices_23 = p2p23.Install(nodes_23)

## ==============================================
## 3. Network Configuration
## ==============================================
## Internet Stack Installation
stack = ns.InternetStackHelper()
stack.Install(nodes)

## IP Address Assignment
address = ns.Ipv4AddressHelper()
address.SetBase(ns.Ipv4Address("10.1.1.0"), ns.Ipv4Mask("255.255.255.0"))

interfaces_01 = address.Assign(devices_01)
interfaces_12 = address.Assign(devices_12)
interfaces_23 = address.Assign(devices_23)

## Global Routing Table Population
ns.Ipv4GlobalRoutingHelper.PopulateRoutingTables()

## Packet Capture (PCAP) Configuration
## EnablePcapAll: [prefix]-[hops]-[type(0=Tx; 1=Rx)].pcap
p2p01.EnablePcapAll("ns3_relay_01")
#p2p12.EnablePcapAll("ns3_relay_12")
#p2p23.EnablePcapAll("ns3_relay_23")

## ==============================================
## 4. Applications
## ==============================================
## Port Definition
port = 9876

## UDP Server (Option A: Echo Server)
#echoServer = ns.UdpEchoServerHelper(port)
#serverApps = echoServer.Install(nodes.Get(3))
#serverApps.Start(ns.Seconds(1.0))
#serverApps.Stop(ns.Seconds(10.0))

## UDP Server (Option B: Simple Server)
udpServer = ns.UdpServerHelper(port)
serverApps = udpServer.Install(nodes.Get(3))
serverApps.Start(ns.Seconds(1.0))
serverApps.Stop(ns.Seconds(10.0))

## Destination Address for Clients
destination = interfaces_23.GetAddress(1).ConvertTo()

## UDP Client (Option A: Echo Client)
#echoClient = ns.UdpEchoClientHelper(destination, port)
#echoClient.SetAttribute("MaxPackets", ns.UintegerValue(1))
#echoClient.SetAttribute("Interval", ns.TimeValue(ns.Seconds(1.0)))
#echoClient.SetAttribute("PacketSize", ns.UintegerValue(1024))
#clientApps = echoClient.Install(nodes.Get(0))
#clientApps.Start(ns.Seconds(2.0))
#clientApps.Stop(ns.Seconds(10.0))

## UDP Client (Option B: Simple Client)
udpClient = ns.UdpClientHelper(destination, port)
udpClient.SetAttribute("MaxPackets", ns.UintegerValue(10))
udpClient.SetAttribute("Interval", ns.TimeValue(ns.Seconds(1.0)))
udpClient.SetAttribute("PacketSize", ns.UintegerValue(1024))
clientApps = udpClient.Install(nodes.Get(0))
clientApps.Start(ns.Seconds(2.0))
clientApps.Stop(ns.Seconds(10.0))

## ==============================================
## 5. Simulation Execution
## ==============================================
## Simulation Execution
def run():
    ## Animation
    animation = ns.AnimationInterface("ns3_relay.xml")
    ns.AnimationInterface.SetConstantPosition(nodes.Get(0), 20, 20)
    ns.AnimationInterface.SetConstantPosition(nodes.Get(1), 40, 40)
    ns.AnimationInterface.SetConstantPosition(nodes.Get(2), 60, 60)
    ns.AnimationInterface.SetConstantPosition(nodes.Get(3), 80, 80)
    ## Run Simulation
    ns.Simulator.Stop(ns.Seconds(10.0))
    ns.Simulator.Run()
    ns.Simulator.Destroy()

run()


Daiphys is a professional services company in research and development of leading-edge technologies in science and engineering.
Get started accelerating your business through our deep expertise in R&D with AI, quantum computing, and space development; please get in touch with Daiphys today!

Name*


Email*


Subject


Message*




* Indicates required field

Daiphys Technologies LLC - https://www.daiphys.com/

  • Last modified: 2025/09/15 10:42
  • by Daiphys