Introduction to ns-3 and AI Prompting
Section outline
-
Problem: You need to set up a basic ns-3 simulation but don't know where to start
Start with simple prompts like, "Write a basic ns-3 C++ script to create two nodes and a point-to-point link between them."
Example Prompt: Generate a simple "Hello World" style ns-3 script and explaining each part of the code.Hello World ns-3 Script
Here is a simple ns-3 script that creates a basic network with two nodes and a point-to-point link. This is a great "Hello World" equivalent for network simulation.
#include "ns3/core-module.h" #include "ns3/network-module.h" #include "ns3/point-to-point-module.h" #include "ns3/internet-module.h" // Bring in the ns3 namespace to avoid prefixing every class with "ns3::" using namespace ns3; int main(int argc, char *argv[]) { // 1. Create two nodes NodeContainer nodes; nodes.Create(2); // 2. Create a point-to-point channel and a link between the nodes PointToPointHelper pointToPoint; pointToPoint.SetDeviceAttribute("DataRate", StringValue("5Mbps")); pointToPoint.SetChannelAttribute("Delay", StringValue("2ms")); NetDeviceContainer devices; devices = pointToPoint.Install(nodes); // 3. Install the Internet stack on the nodes InternetStackHelper stack; stack.Install(nodes); // 4. Assign IP addresses Ipv4AddressHelper address; address.SetBase("10.1.1.0", "255.255.255.0"); Ipv4InterfaceContainer interfaces = address.Assign(devices); // 5. Run the simulation Simulator::Run(); Simulator::Destroy(); return 0; }
Explanation of the Code
1. Header Files and Namespace
-
#include "ns3/...-module.h": These lines include the necessary header files for the ns-3 modules we will be using, such as the core, network, and point-to-point modules. -
using namespace ns3;: This line allows us to use ns-3 classes directly without thens3::prefix, making the code cleaner.
2. Main Function
main()-
The
main()function is the entry point of the program, just like in any other C++ application. -
int argc, char *argv[]: These are the standard command-line arguments that can be used to pass parameters to the simulation.
3. Creating Nodes
-
NodeContainer nodes;: This creates an object to hold our nodes. -
nodes.Create(2);: This command creates two simulation nodes. In ns-3, a node is analogous to a computer or device.
4. Creating the Channel and Devices
-
PointToPointHelper pointToPoint;: This is a "helper" class that simplifies the creation of a point-to-point link. -
SetDeviceAttribute(...)andSetChannelAttribute(...): These lines configure the link's properties, such as the data rate (5 Mbps) and propagation delay (2 ms). -
NetDeviceContainer devices;: This object will hold the network devices (e.g., network interface cards) for the nodes. -
devices = pointToPoint.Install(nodes);: This command creates the link and installs a network device on each of the two nodes.
5. Installing the Internet Stack
-
InternetStackHelper stack;: This helper class simplifies the installation of the network stack (including TCP/IP, UDP, etc.) on the nodes. -
stack.Install(nodes);: This installs the full Internet protocol stack on both of our nodes, which is necessary for them to communicate.
6. Assigning IP Addresses
-
Ipv4AddressHelper address;: This helper is used to assign IP addresses to the network devices. -
address.SetBase("10.1.1.0", "255.255.255.0");: This sets the network base and subnet mask for the IP addresses. -
Ipv4InterfaceContainer interfaces = address.Assign(devices);: This assigns IP addresses (e.g., 10.1.1.1 and 10.1.1.2) from the specified network to the two devices.
7. Running the Simulation
-
Simulator::Run();: This is the most important command. It starts the discrete-event simulation, running all the scheduled events. -
Simulator::Destroy();: This cleans up all resources used by the simulation. It's good practice to call this at the end of every simulation.
Be specific in your prompts. State the ns-3 version you are using.Don't use vague prompts like "make an ns-3 simulation.Try the above simulation on our paid Interactive Playground!.
Note: The interactive simulation playground of this course will be available only after purchasing the playground for this course!.If you run the code, you will not see any output in the terminal
Click on the following image to open iPlaygroundThings that can be tried :
Make some changes in the code, so that you can understand the completion of the each step of the simulation.
Just convert the comments 1 to 5 as print statements and make it print the step on the Console/Terminal before starting a step.
Enable and use the "LOG" system of ns-3 to log some events on the Console/Terminal. -
