Understanding an ns-3 program’s structure
Before writing any simulation program, it is important to understand the ns-3 simulation program structure. By doing so, it is possible to avoid common mistakes and save a lot of time in debugging your simulation issues. Here, we describe how to create the basic and advanced ns-3 simulation program structures, as follows:
- Import all necessary ns-3 packages.
- Define the logging component for your program. Your simulation program allows you to include necessary log statements, such as information, warnings, errors, exceptions, assertions, and debug details. While running a simulation, you can enable or disable any of the log statements.
- Set up a simulation topology and configure it. Before writing your simulation program, it is recommended to draw your simulation network topology:
- Create nodes using the ns-3
NodeContainer
class. In ns-3, hosts and other network equipment are referred to as nodes, and users can install the necessary protocols to configure them as a specific piece of networking equipment. Identify the type of nodes, such as hosts, routers, switches, access points (APs), and BSs, involved in your topology and create the necessary number of nodes for each type using the ns-3NodeContainer
class. - Connect nodes using suitable communication channels and install the necessary communication protocols to get the
NetDevices
(network interface cards (NIC)) associated with the nodes. For example, you'll need to collect the necessary wired or wireless/mobile network devices using the ns-3NetDeviceContainer
class. - Install the Transmission Control Protocol (TCP)/Internet Protocol (IP) on nodes using the ns-3
InternetStackHelper
class. Hence, we can assign IP addresses and install routing protocols. - Configure IP addresses. We need to identify and create types of network addresses as required by setting suitable network IDs and subnet masks. Then, we can configure the respective IP address to respective
NetDevices
using the ns-3NetDeviceContainer
class. - Configure routing protocols to interconnect different networks involved in your topology.
- Create nodes using the ns-3
- Next, identify the necessary network applications to install and run on various nodes:
- Identify on which nodes you need to install your client or server applications, then configure suitable IP addresses and port numbers for those client and server applications.
- Configure client and server application traffic flow characteristics, such as packet size, packet interval, and number of packets.
- Install the network applications on the selected nodes.
- Configure the start and stop timings of your applications.
- Install
FlowMonitor
(an ns-3 class) on all nodes to collect all flow statistics. Although it is not necessary to install aFlowMonitor
application on nodes, it is important to analyze your network application’s performance on the simulation topology in terms of flow level throughput, delay, packet loss, and jitter metrics. - Configure packet capturing and traces on nodes (this is optional). This helps you to collect a variety of packets exchanged across your simulation topology and possibly to inspect packet details using the
wireshark
ortcpdump
packet analysis tools. - Configure the Animation Interface (this is optional). It helps you to view the following simulation execution and results using the NetAnim visualization tool:
- Enable various packets exchange among nodes tracking feature.
- Enable the routing tables tracking feature.
- Enable the counter tables tracking feature.
- Enable flow monitor statistics capturing.
- Configure the stopping time of your simulation using
Simulator::Stop()
. It is necessary to stop the simulation when you use a flow monitor; otherwise, your simulation never ends. - Start running your simulation using
Simulator::Run()
. This statement starts your simulation execution. - Convert the flow monitor results into an XML file to visualize using NetAnim.
- Clean up your simulation resources using
Simulator::Destroy()
. It is important to clean up your simulation resources such as memory, devices, and files.
We now understand a typical ns-3 simulation program structure. This common structure is suitable for writing any wired, wireless, or mobile networking topology simulation. With this knowledge, in the next section, we will start writing our first simulation program (pkt_first.cc
) by referring to the existing tutorial/first.cc
simulation program.