2010-12-30 23:39:27 +01:00
.. include :: replace.txt
2013-07-17 17:09:36 -07:00
.. highlight :: python
2010-12-30 23:39:27 +01:00
2017-11-13 14:39:51 -08:00
.. heading hierarchy:
------------- Chapter
***** ***** *** Section (#.#)
============= Subsection (#.#.#)
############# Paragraph (no number)
2011-06-17 13:36:07 -07:00
Using Python to Run |ns3|
-------------------------
2010-12-30 23:39:27 +01:00
2011-06-17 13:36:07 -07:00
Python bindings allow the C++ code in |ns3| to be called from Python.
2010-12-30 23:39:27 +01:00
2011-06-17 13:36:07 -07:00
This chapter shows you how to create a Python script that can run |ns3| and also the process of creating Python bindings for a C++ |ns3| module.
Introduction
***** ***** **
2016-10-03 15:51:13 -07:00
Python bindings provide support for importing |ns3| model libraries as Python
modules. Coverage of most of the |ns3| C++ API is provided. The intent
has been to allow the programmer to write complete simulation scripts in
Python, to allow integration of |ns3| with other Python tools and workflows.
The intent is not to provide a different language choice to author new
|ns3| models implemented in Python.
2011-06-17 13:36:07 -07:00
2021-11-29 21:58:30 -03:00
Python bindings for |ns3| use a tool called PyBindGen (https://github.com/gjcarneiro/pybindgen)
to create Python modules from the C++ libraries built by CMake. The Python bindings that PyBindGen
uses are maintained in a `` bindings `` directory in each module, and must be maintained to match the
2017-09-18 13:46:16 -07:00
C++ API of that ns-3 module. If the C++ API changes, the Python bindings file
must either be modified by hand accordingly, or the bindings must be
regenerated by an automated scanning process.
2022-04-17 15:46:35 -07:00
If a user is not interested in Python, no action is needed; by default, Python
bindings are disabled (and must be explicitly enabled at
CMake configure time). In this case, changes to the C++
2017-09-18 13:46:16 -07:00
API of a provided module will not cause the module to fail to compile.
The process for automatically generating Python bindings relies on a toolchain
involving a development installation of the Clang compiler, a program called
CastXML (https://github.com/CastXML/CastXML), and a program called
pygccxml (https://github.com/gccxml/pygccxml). The toolchain can be installed
using the ns-3 `` bake `` build tool.
2011-06-17 13:36:07 -07:00
2022-04-17 15:46:35 -07:00
As of ns-3.36, the Python bindings framework (including many of its
constituent tools) has lacked active maintainers for several years, and there
are constraints on the compatible Python version and on the C++ language
features that may be used in the C++ header files. The API scanning framework
(in particular, pygccxml) requires Python 3.6 through 3.8 but is
not compatible with Python 3.9 or 3.10. Compiling the existing bindings
should work through Python 3.10. Additionally, many newer C++ language features
beyond C++11 may not be supported by pybindgen.
Python bindings may be removed from future ns-3 releases if new maintainers
are not found.
2011-06-17 13:36:07 -07:00
An Example Python Script that Runs |ns3|
***** ***** ***** ***** ***** ***** ***** *****
Here is some example code that is written in Python and that runs |ns3|, which is written in C++. This Python example can be found in `` examples/tutorial/first.py `` :
::
import ns.applications
import ns.core
import ns.internet
import ns.network
import ns.point_to_point
ns.core.LogComponentEnable("UdpEchoClientApplication", ns.core.LOG_LEVEL_INFO)
ns.core.LogComponentEnable("UdpEchoServerApplication", ns.core.LOG_LEVEL_INFO)
nodes = ns.network.NodeContainer()
nodes.Create(2)
pointToPoint = ns.point_to_point.PointToPointHelper()
pointToPoint.SetDeviceAttribute("DataRate", ns.core.StringValue("5Mbps"))
pointToPoint.SetChannelAttribute("Delay", ns.core.StringValue("2ms"))
devices = pointToPoint.Install(nodes)
stack = ns.internet.InternetStackHelper()
stack.Install(nodes)
address = ns.internet.Ipv4AddressHelper()
address.SetBase(ns.network.Ipv4Address("10.1.1.0"), ns.network.Ipv4Mask("255.255.255.0"))
interfaces = address.Assign (devices);
echoServer = ns.applications.UdpEchoServerHelper(9)
serverApps = echoServer.Install(nodes.Get(1))
serverApps.Start(ns.core.Seconds(1.0))
serverApps.Stop(ns.core.Seconds(10.0))
echoClient = ns.applications.UdpEchoClientHelper(interfaces.GetAddress(1), 9)
echoClient.SetAttribute("MaxPackets", ns.core.UintegerValue(1))
echoClient.SetAttribute("Interval", ns.core.TimeValue(ns.core.Seconds (1.0)))
echoClient.SetAttribute("PacketSize", ns.core.UintegerValue(1024))
clientApps = echoClient.Install(nodes.Get(0))
clientApps.Start(ns.core.Seconds(2.0))
clientApps.Stop(ns.core.Seconds(10.0))
ns.core.Simulator.Run()
ns.core.Simulator.Destroy()
Running Python Scripts
***** ***** ***** ***** **
2022-04-17 15:46:35 -07:00
First, we need to enable the build of Python bindings:
2022-01-31 20:02:10 -03:00
.. sourcecode :: bash
$ ./ns3 configure --enable-python-bindings
2022-04-17 15:46:35 -07:00
Other options such as `` --enable-examples `` may be passed to the above command.
2021-11-29 21:58:30 -03:00
ns3 contains some options that automatically update the python path to find the ns3 module. To run example programs, there are two ways to use ns3 to take care of this. One is to run a ns3 shell; e.g.:
2011-06-17 13:36:07 -07:00
2013-07-17 17:09:36 -07:00
.. sourcecode :: bash
2011-06-17 13:36:07 -07:00
2021-11-29 21:58:30 -03:00
$ ./ns3 shell
2022-04-17 15:46:35 -07:00
$ python3 examples/wireless/mixed-wireless.py
2011-06-17 13:36:07 -07:00
2022-01-31 20:02:10 -03:00
and the other is to use the 'run' option to ns3:
2011-06-17 13:36:07 -07:00
2013-07-17 17:09:36 -07:00
.. sourcecode :: bash
2011-06-17 13:36:07 -07:00
2022-01-13 23:59:59 -03:00
$ ./ns3 run examples/wireless/mixed-wireless.py
2011-06-17 13:36:07 -07:00
2022-01-31 20:02:10 -03:00
Use the `` --no-build `` option to run the program without invoking a project rebuild.
This option may be useful to improve execution time when running the same program
repeatedly but with different arguments, such as from scripts.
2019-04-03 15:34:32 -07:00
.. sourcecode :: bash
2022-04-17 15:46:35 -07:00
$ ./ns3 run --no-build examples/wireless/mixed-wireless.py
2019-04-03 15:34:32 -07:00
2011-06-17 13:36:07 -07:00
To run a python script under the C debugger:
2013-07-17 17:09:36 -07:00
.. sourcecode :: bash
2011-06-17 13:36:07 -07:00
2021-11-29 21:58:30 -03:00
$ ./ns3 shell
2022-04-17 15:46:35 -07:00
$ gdb --args python3 examples/wireless/mixed-wireless.py
2011-06-17 13:36:07 -07:00
To run your own Python script that calls |ns3| and that has this path, `` /path/to/your/example/my-script.py `` , do the following:
2013-07-17 17:09:36 -07:00
.. sourcecode :: bash
2011-06-17 13:36:07 -07:00
2021-11-29 21:58:30 -03:00
$ ./ns3 shell
2022-04-17 15:46:35 -07:00
$ python3 /path/to/your/example/my-script.py
2011-06-17 13:36:07 -07:00
Caveats
***** **
2022-04-17 15:46:35 -07:00
In addition to limitations in Python and C++ versions supported (as discussed
above), some additional limitations are listed here.
2011-06-17 13:36:07 -07:00
Incomplete Coverage
2017-11-13 14:39:51 -08:00
===================
2011-06-17 13:36:07 -07:00
First of all, keep in mind that not 100% of the API is supported in Python. Some of the reasons are:
#. some of the APIs involve pointers, which require knowledge of what kind of memory passing semantics (who owns what memory). Such knowledge is not part of the function signatures, and is either documented or sometimes not even documented. Annotations are needed to bind those functions;
#. Sometimes a unusual fundamental data type or C++ construct is used which is not yet supported by PyBindGen;
2021-03-19 07:42:45 -07:00
#. CastXML does not report template based classes unless they are instantiated.
2011-06-17 13:36:07 -07:00
2022-04-17 15:46:35 -07:00
Most of the missing APIs can be wrapped, given enough time, patience, and expertise, and will likely be wrapped if bug reports are submitted. However, don't file a bug report saying "bindings are incomplete", because the project does not have maintainers to maintain every API.
2011-06-17 13:36:07 -07:00
Conversion Constructors
2017-11-13 14:39:51 -08:00
=======================
2011-06-17 13:36:07 -07:00
2013-07-17 17:09:36 -07:00
`Conversion constructors <http://publib.boulder.ibm.com/infocenter/compbgpl/v9v111/topic/com.ibm.xlcpp9.bg.doc/language_ref/cplr384.htm> `_ are not fully supported yet by PyBindGen, and they always act as explicit constructors when translating an API into Python. For example, in C++ you can do this:
2011-06-17 13:36:07 -07:00
2013-07-17 17:09:36 -07:00
.. sourcecode :: cpp
2011-06-17 13:36:07 -07:00
Ipv4AddressHelper ipAddrs;
ipAddrs.SetBase ("192.168.0.0", "255.255.255.0");
ipAddrs.Assign (backboneDevices);
2022-04-17 15:46:35 -07:00
In Python, you have to do:
2011-06-17 13:36:07 -07:00
::
2016-10-03 15:51:13 -07:00
ipAddrs = ns.internet.Ipv4AddressHelper()
ipAddrs.SetBase(ns.network.Ipv4Address("192.168.0.0"), ns.network.Ipv4Mask("255.255.255.0"))
2011-06-17 13:36:07 -07:00
ipAddrs.Assign(backboneDevices)
CommandLine
2017-11-13 14:39:51 -08:00
===========
2011-06-17 13:36:07 -07:00
:cpp:func: `CommandLine::AddValue` works differently in Python than it does in |ns3|. In Python, the first parameter is a string that represents the command-line option name. When the option is set, an attribute with the same name as the option name is set on the :cpp:func: `CommandLine` object. Example:
::
NUM_NODES_SIDE_DEFAULT = 3
cmd = ns3.CommandLine()
cmd.NumNodesSide = None
cmd.AddValue("NumNodesSide", "Grid side number of nodes (total number of nodes will be this number squared)")
cmd.Parse(argv)
[...]
if cmd.NumNodesSide is None:
num_nodes_side = NUM_NODES_SIDE_DEFAULT
else:
num_nodes_side = int(cmd.NumNodesSide)
Tracing
2017-11-13 14:39:51 -08:00
=======
2011-06-17 13:36:07 -07:00
Callback based tracing is not yet properly supported for Python, as new |ns3| API needs to be provided for this to be supported.
Pcap file writing is supported via the normal API.
2022-04-02 13:32:30 -07:00
ASCII tracing is supported via the normal C++ API translated to Python. However, ASCII tracing requires the creation of an ostream object to pass into the ASCII tracing methods. In Python, the C++ std::ofstream has been minimally wrapped to allow this. For example:
2011-06-17 13:36:07 -07:00
::
ascii = ns3.ofstream("wifi-ap.tr") # create the file
ns3.YansWifiPhyHelper.EnableAsciiAll(ascii)
ns3.Simulator.Run()
ns3.Simulator.Destroy()
ascii.close() # close the file
There is one caveat: you must not allow the file object to be garbage collected while |ns3| is still using it. That means that the 'ascii' variable above must not be allowed to go out of scope or else the program will crash.
Working with Python Bindings
***** ***** ***** ***** ***** ***
2016-10-03 15:51:13 -07:00
Python bindings are built on a module-by-module basis, and can be found in each module's `` bindings `` directory.
2011-06-17 13:36:07 -07:00
2016-10-03 15:51:13 -07:00
Overview
2017-11-13 14:39:51 -08:00
========
2011-06-17 13:36:07 -07:00
2016-10-03 15:51:13 -07:00
The python bindings are generated into an 'ns' namespace. Examples:
2011-06-17 13:36:07 -07:00
2016-10-03 15:51:13 -07:00
::
2011-06-17 13:36:07 -07:00
2016-10-03 15:51:13 -07:00
from ns.network import Node
n1 = Node()
2011-06-17 13:36:07 -07:00
2016-10-03 15:51:13 -07:00
or
2011-06-17 13:36:07 -07:00
2016-10-03 15:51:13 -07:00
::
2011-06-17 13:36:07 -07:00
2016-10-03 15:51:13 -07:00
import ns.network
n1 = ns.network.Node()
2011-06-17 13:36:07 -07:00
2016-10-03 15:51:13 -07:00
The best way to explore the bindings is to look at the various example
programs provided in |ns3|; some C++ examples have a corresponding Python
example. There is no structured documentation for the Python bindings
like there is Doxygen for the C++ API, but the Doxygen can be consulted
to understand how the C++ API works.
2011-06-17 13:36:07 -07:00
2017-11-13 14:39:51 -08:00
Python Bindings Workflow
========================
2011-06-17 13:36:07 -07:00
2017-11-13 14:39:51 -08:00
The process by which Python bindings are handled is the following:
2011-06-17 13:36:07 -07:00
2017-11-13 14:39:51 -08:00
#. Periodically a developer uses a CastXML (https://github.com/CastXML/CastXML) based API scanning script, which saves the scanned API definition as `` bindings/python/ns3_module_*.py `` files or as Python files in each modules' `` bindings `` directory. These files are kept under version control in the main |ns3| repository;
#. Other developers clone the repository and use the already scanned API definitions;
#. When configuring |ns3|, pybindgen will be automatically downloaded if not already installed. Released |ns3| tarballs will ship a copy of pybindgen.
2011-06-17 13:36:07 -07:00
2022-04-17 15:46:35 -07:00
If something goes wrong with compiling Python bindings and you just want to ignore them and move on with C++, you can disable Python by configuring |ns3| without Python bindings enabled.
2017-11-13 14:39:51 -08:00
Regenerating the Python bindings
================================
|ns3| will fail to successfully compile the Python bindings if the C++
headers are changed and no longer align with the stored Python bindings.
In this case, the developer has two main choices: 1) disable Python
as described above, or 2) update the bindings to align with the new C++
API.
Process Overview
################
|ns3| has an automated process to regenerate Python bindings from the C++
2018-07-21 16:31:05 -07:00
header files. The process is only supported for Linux at the moment
2022-04-02 13:32:30 -07:00
because the project has not found a contributor yet to test and
2021-04-30 22:08:23 -07:00
document the capability on macOS. In short, the process currently
requires the following steps on a Linux machine.
2017-11-13 14:39:51 -08:00
1. Prepare the system for scanning by installing the prerequisites,
including a development version of `` clang `` , the `` CastXML `` package,
2021-04-30 22:08:23 -07:00
`` pygccxml `` , and `` pybindgen `` .
2018-07-21 16:31:05 -07:00
2. Perform a scan of the module of interest or all modules
2017-11-13 14:39:51 -08:00
Installing a clang development environment
##########################################
Make sure you have a development version of the clang compiler installed
on your system. This can take a long time to build from source. Linux
2021-04-30 22:08:23 -07:00
distributions provide binary library packages such as `` libclang-dev ``
(Ubuntu) or `` clang-devel `` (Fedora).
2017-11-13 14:39:51 -08:00
Installing other prerequisites
##############################
2021-04-30 22:08:23 -07:00
`` cxxfilt `` is a new requirement, typically installed using `` pip `` or `` pip3 `` ; e.g.
2011-06-17 13:36:07 -07:00
2018-08-31 16:58:43 +02:00
.. sourcecode :: bash
2011-06-17 13:36:07 -07:00
2021-08-20 14:42:44 -07:00
pip3 install --user cxxfilt
2011-06-17 13:36:07 -07:00
2017-11-13 14:39:51 -08:00
See also the wiki for installation notes for your system.
Set up a `` bake `` build environment
###################################
2018-08-31 16:58:43 +02:00
Try the following commands:
2017-11-13 14:39:51 -08:00
2018-08-05 17:12:43 -07:00
.. sourcecode :: bash
2017-11-13 14:39:51 -08:00
$ cd bake
2021-03-19 07:42:45 -07:00
$ export PATH=`pwd` /build/bin:$PATH
$ export LD_LIBRARY_PATH=`pwd` /build/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
$ export PYTHONPATH=`pwd` /build/lib${PYTHONPATH:+:$PYTHONPATH}
2017-11-13 14:39:51 -08:00
$ mkdir -p build/lib
Configure
#########
2011-06-17 13:36:07 -07:00
2018-08-31 16:58:43 +02:00
Perform a configuration at the bake level:
2011-06-17 13:36:07 -07:00
2018-08-05 17:12:43 -07:00
.. sourcecode :: bash
2021-03-19 07:42:45 -07:00
$ ./bake.py configure -e ns-3-dev -e pygccxml
2017-11-13 14:39:51 -08:00
2021-03-19 07:42:45 -07:00
The output of `` ./bake.py show `` should show something like this:
2018-08-31 16:58:43 +02:00
2016-10-03 15:51:13 -07:00
2018-08-05 17:12:43 -07:00
.. sourcecode :: bash
2017-11-13 14:39:51 -08:00
$ ./bake.py show
2016-10-03 15:51:13 -07:00
2021-03-19 07:42:45 -07:00
Should say (note: some are OK to be 'Missing' for Python bindings scanning):
2011-06-17 13:36:07 -07:00
2018-08-05 17:12:43 -07:00
.. sourcecode :: text
2017-11-13 14:39:51 -08:00
-- System Dependencies --
> clang-dev - OK
2021-03-19 07:42:45 -07:00
> cmake - OK
> cxxfilt - OK
2017-11-13 14:39:51 -08:00
> g++ - OK
2021-03-19 07:42:45 -07:00
> gi-cairo - OK or Missing
> gir-bindings - OK or Missing
> llvm-dev - OK
> pygobject - OK or Missing
> pygraphviz - OK or Missing
> python3-dev - OK
> python3-setuptools - OK
> qt - OK or Missing
2017-11-13 14:39:51 -08:00
> setuptools - OK
2011-06-17 13:36:07 -07:00
2021-03-19 07:42:45 -07:00
Note that it is not harmful for Python bindings if some of the items above
report as Missing. For Python bindings, the important
prerequisites are clang-dev, cmake, cxxfilt, llvm-dev, python3-dev,
and python3-setuptools. In the following process, the following programs
and libraries will be locally installed: castxml, pybindgen, pygccxml,
and |ns3|.
Note also that the `ns-3-allinone` target for bake will also include the
`pygccxml` and `ns-3-dev` targets (among other libraries) and can be
used instead, e.g.:
.. sourcecode :: bash
$ ./bake.py configure -e ns-3-allinone
2017-11-13 14:39:51 -08:00
Download
########
2011-06-17 13:36:07 -07:00
2021-03-19 07:42:45 -07:00
Issue the following download command. Your output may vary depending on what
is present or missing on your system.
2011-06-17 13:36:07 -07:00
2018-08-05 17:12:43 -07:00
.. sourcecode :: bash
2017-11-13 14:39:51 -08:00
$ ./bake.py download
2021-03-19 07:42:45 -07:00
>> Searching for system dependency llvm-dev - OK
2017-11-13 14:39:51 -08:00
>> Searching for system dependency clang-dev - OK
2021-03-19 07:42:45 -07:00
>> Searching for system dependency qt - Problem
> Problem: Optional dependency, module "qt" not available
This may reduce the functionality of the final build.
However, bake will continue since "qt" is not an essential dependency.
For more information call bake with -v or -vvv, for full verbose mode.
2017-11-13 14:39:51 -08:00
>> Searching for system dependency g++ - OK
2021-03-19 07:42:45 -07:00
>> Searching for system dependency cxxfilt - OK
2017-11-13 14:39:51 -08:00
>> Searching for system dependency setuptools - OK
2021-03-19 07:42:45 -07:00
>> Searching for system dependency python3-setuptools - OK
>> Searching for system dependency gi-cairo - Problem
> Problem: Optional dependency, module "gi-cairo" not available
This may reduce the functionality of the final build.
However, bake will continue since "gi-cairo" is not an essential dependency.
For more information call bake with -v or -vvv, for full verbose mode.
>> Searching for system dependency gir-bindings - Problem
> Problem: Optional dependency, module "gir-bindings" not available
This may reduce the functionality of the final build.
However, bake will continue since "gir-bindings" is not an essential dependency.
For more information call bake with -v or -vvv, for full verbose mode.
>> Searching for system dependency pygobject - Problem
> Problem: Optional dependency, module "pygobject" not available
This may reduce the functionality of the final build.
However, bake will continue since "pygobject" is not an essential dependency.
For more information call bake with -v or -vvv, for full verbose mode.
>> Searching for system dependency pygraphviz - Problem
> Problem: Optional dependency, module "pygraphviz" not available
This may reduce the functionality of the final build.
However, bake will continue since "pygraphviz" is not an essential dependency.
For more information call bake with -v or -vvv, for full verbose mode.
>> Searching for system dependency python3-dev - OK
>> Searching for system dependency cmake - OK
2017-11-13 14:39:51 -08:00
>> Downloading castxml - OK
>> Downloading netanim - OK
>> Downloading pybindgen - OK
2021-03-19 07:42:45 -07:00
>> Downloading pygccxml - OK
2017-11-13 14:39:51 -08:00
>> Downloading ns-3-dev - OK
Build
#####
2017-09-18 13:46:16 -07:00
2021-03-19 07:42:45 -07:00
Next, try the following command:
2011-06-17 13:36:07 -07:00
2018-08-31 16:58:43 +02:00
.. sourcecode :: bash
2018-08-05 17:12:43 -07:00
2017-11-13 14:39:51 -08:00
$ ./bake.py build
2011-06-17 13:36:07 -07:00
2021-03-19 07:42:45 -07:00
A build report should be printed for each package, such as:
::
>> Building castxml - OK
>> Building netanim - OK
>> Building pybindgen - OK
>> Building pygccxml - OK
>> Building ns-3-dev - OK
2017-11-13 14:39:51 -08:00
2021-03-19 07:42:45 -07:00
However, if there is a problem with the bindings compilation (or with the
C++ code), it will report a failure instead:
::
>> Building ns-3-dev - Problem
> Error: Critical dependency, module "ns-3-dev" failed
For more information call Bake with --debug and/or -v, -vvv, for full verbose mode (bake --help)
At this point, it is recommended to change into the ns-3-dev directory and
work further from there, because the API scanning dependencies have been
built and installed successfully into the `build` directory.
2021-11-29 21:58:30 -03:00
The output of './ns3 configure' can be inspected to see if Python API scanning
2017-11-13 14:39:51 -08:00
support is enabled:
2011-06-17 13:36:07 -07:00
2018-08-05 17:12:43 -07:00
.. sourcecode :: text
2017-11-13 14:39:51 -08:00
Python API Scanning Support : enabled
2011-06-17 13:36:07 -07:00
2021-03-19 07:42:45 -07:00
It may say something like this, if the support is not active or something
went wrong in the build process:
2017-11-13 14:39:51 -08:00
2018-08-05 17:12:43 -07:00
.. sourcecode :: text
2017-11-13 14:39:51 -08:00
Python API Scanning Support : not enabled (Missing 'pygccxml' Python module)
2021-03-19 07:42:45 -07:00
In this case, the user must take additional steps to resolve. For the
API scanning support to be detected, the castxml binary must be in the
shell's PATH, and pygccxml must be in the PYTHONPATH.
2017-11-13 14:39:51 -08:00
2018-07-21 16:31:05 -07:00
LP64 vs ILP32 bindings
######################
2017-09-18 13:46:16 -07:00
Linux (64-bit, as most modern installations use) and MacOS use different
data models, as explained here: https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.3.0/com.ibm.zos.v2r3.cbcpx01/datatypesize64.htm
Linux uses the LP64 model, and MacOS (as well as 32-bit Linux) use the ILP32
model. Users will note that there are two versions of bindings files in
each ns-3 module directory; one with an ILP32.py suffix and one with
an LP64.py suffix. Only one is used on any given platform. The main
difference is in the representation of the 64 bit integer type as either
a 'long' (LP64) or 'long long' (ILP32).
2018-07-21 16:31:05 -07:00
The process (only supported on Linux at present) generates the LP64
bindings using the toolchain and then copies the LP64 bindings to the
2021-11-29 21:58:30 -03:00
ILP32 bindings with some type substitutions automated by CMake scripts.
2017-09-18 13:46:16 -07:00
2018-07-21 16:31:05 -07:00
Rescanning a module
###################
2022-04-02 13:32:30 -07:00
To re-scan a module, it is recommended to clean the installation, and then
to configure with Python scanning enabled:
2017-09-18 13:46:16 -07:00
2018-08-05 17:12:43 -07:00
.. sourcecode :: bash
2018-07-21 16:31:05 -07:00
$ cd source/ns-3-dev
2022-04-02 13:32:30 -07:00
$ ./ns3 clean
$ ./ns3 configure -- -DNS3_SCAN_PYTHON_BINDINGS=ON
Ensure in the configure output that pygccxml and castxml were found. To
scan an individual module, such as wifi, append `-apiscan` to the module
name:
.. sourcecode :: bash
2022-01-31 20:02:10 -03:00
$ ./ns3 build wifi-apiscan
2017-09-18 13:46:16 -07:00
2022-04-02 13:32:30 -07:00
To re-scan all modules (which can take some time):
2017-09-18 13:46:16 -07:00
2018-08-31 16:58:43 +02:00
.. sourcecode :: bash
2017-09-18 13:46:16 -07:00
2018-07-21 16:31:05 -07:00
$ cd source/ns-3-dev
2022-01-31 20:02:10 -03:00
$ ./ns3 apiscan-all
2017-09-18 13:46:16 -07:00
2022-04-02 13:32:30 -07:00
Then, to check whether the rescanned bindings can be compiled, enable
the Python bindings in the build:
.. sourcecode :: bash
$ ./ns3 configure --enable-python-bindings
$ ./ns3 build
2017-11-13 14:39:51 -08:00
Generating bindings on MacOS
############################
In principle, this should work (and should generate the 32-bit bindings).
2021-03-19 07:42:45 -07:00
However, maintainers have not been available to complete this port to date.
2017-11-13 14:39:51 -08:00
We would welcome suggestions on how to enable scanning for MacOS.
2022-04-17 15:46:35 -07:00
Regenerating the Python bindings using Docker
=============================================
An alternative to the above Bake install is to use a Docker container.
The following steps are used by the |ns3| CI system on an Ubuntu 20.04 image.
As a prerequisite, install Docker using a package manager; on Ubuntu, use:
.. sourcecode bash
$ apt install docker.io
To allow an unprivileged user to use Docker, perform the following
.. sourcecode bash
$ apt install docker.io
Then execute the following steps to get a shell:
.. sourcecode bash
$ docker run -it ubuntu:20.04
If you prefer to work on your current directory from the container shell,
instead do:
.. sourcecode bash
$ docker run -it -v /path/to/your/current/directory:/path/to/container/mount ubuntu:20.04
$ cd /path/to/container/mount
where `` /path/to/container/mount `` can be something like `` /ns-3-dev `` .
Then, from within a directory that contains ns-3, perform the following
steps to rescan the APIs and test them:
.. sourcecode bash
$ apt-get update
$ apt-get install -y g++ cmake ninja-build ccache libgsl-dev libgtk-3-dev libboost-dev wget git python3 python3-pip
$ pip install cxxfilt pygccxml pybindgen castxml
$ ./ns3 clean
$ ./ns3 configure -G Ninja -- -DNS3_SCAN_PYTHON_BINDINGS=ON
$ ./ns3 build apiscan-all
$ ./ns3 configure --enable-python-bindings
$ ./ns3 build
$ ./ns3 run ./utils/python-unit-tests.py
If you exit the container, the package state will be lost, but from another
terminal window, you may be able to further test and evaluate the new bindings.
Note that the new bindings and build files will have root file ownership.
2011-06-17 13:36:07 -07:00
Organization of the Modular Python Bindings
2017-11-13 14:39:51 -08:00
===========================================
2011-06-17 13:36:07 -07:00
The `` src/<module>/bindings `` directory may contain the following files, some of them optional:
* `` callbacks_list.py `` : this is a scanned file, DO NOT TOUCH. Contains a list of Callback<...> template instances found in the scanned headers;
* `` modulegen__gcc_LP64.py `` : this is a scanned file, DO NOT TOUCH. Scanned API definitions for the GCC, LP64 architecture (64-bit)
* `` modulegen__gcc_ILP32.py `` : this is a scanned file, DO NOT TOUCH. Scanned API definitions for the GCC, ILP32 architecture (32-bit)
* `` modulegen_customizations.py `` : you may optionally add this file in order to customize the pybindgen code generation
* `` scan-header.h `` : you may optionally add this file to customize what header file is scanned for the module. Basically this file is scanned instead of ns3/<module>-module.h. Typically, the first statement is #include "ns3/<module>-module.h", plus some other stuff to force template instantiations;
2022-01-31 20:02:10 -03:00
* `` module_helpers.cc `` : you may add additional files, such as this, to be linked to python extension module. They will be automatically scanned;
2011-06-17 13:36:07 -07:00
* `` <module>.py `` : if this file exists, it becomes the "frontend" python module for the ns3 module, and the extension module (.so file) becomes _<module>.so instead of <module>.so. The <module>.py file has to import all symbols from the module _<module> (this is more tricky than it sounds, see src/core/bindings/core.py for an example), and then can add some additional pure-python definitions.
2021-03-19 07:42:45 -07:00
Historical Information
***** ***** ***** ***** **
2011-06-17 13:36:07 -07:00
2021-03-19 07:42:45 -07:00
If you are a developer and need more background information on |ns3|'s Python bindings, please see the `Python Bindings wiki page <http://www.nsnam.org/wiki/NS-3_Python_Bindings> `_ . Please note, however, that some information on that
page is stale.