Section outline

  • Let us kickstart our ns-3 learning by directly experimenting with the following ns-3 simulation.

    This script is a classic, simple ns-3 simulation that demonstrates a basic client-server communication using a UDP echo protocol over a point-to-point link. The comments have been added to explain each major step of the process.

    #include "ns3/core-module.h"
    #include "ns3/network-module.h"
    #include "ns3/point-to-point-module.h"
    #include "ns3/internet-module.h"
    #include "ns3/applications-module.h"
    
    using namespace ns3;
    
    int main(int argc, char *argv[])
    {
        // Sets the logging level
        LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
    
        // Create a container to hold our two nodes
        NodeContainer nodes;
        nodes.Create(2);
    
        // Create a point-to-point channel and configure its attributes
        PointToPointHelper channel;
        channel.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
        channel.SetChannelAttribute("Delay", StringValue("2ms"));
    
        // Install the channel and devices on our nodes
        NetDeviceContainer netDevices;
        netDevices = channel.Install(nodes);
    
        // Install the Internet stack (TCP/IP) on our nodes
        InternetStackHelper ipStack;
        ipStack.Install(nodes);
    
        // Assign IP addresses to the devices
        Ipv4AddressHelper ipAddresses;
        ipAddresses.SetBase("192.168.1.0", "255.255.255.0");
    
        // Assign the IP addresses and store the interfaces
        Ipv4InterfaceContainer ipinterfaces = ipAddresses.Assign(netDevices);
    
        // Set up a UDP Echo Server on node 1, listening on port 9
        UdpEchoServerHelper udpEchoServer(9);
    
        ApplicationContainer serverApps = udpEchoServer.Install(nodes.Get(1));
        serverApps.Start(Seconds(1.0));
    
        // Set up a UDP Echo Client on node 0
        // It will send packets to the server's IP address (node 1) on port 9
        UdpEchoClientHelper udpEchoClient(ipinterfaces.GetAddress(1), 9);
    
        ApplicationContainer clientApps = udpEchoClient.Install(nodes.Get(0));
        clientApps.Start(Seconds(2.0));
    
        // Run the simulation
        Simulator::Run();
    
        return 0;
    }

    Here is a breakdown of what the sections of code does

     

    1. Setup and Topology

    The script begins by setting up the foundational elements of the simulation.

    • Node Creation: NodeContainer nodes; nodes.Create(2); creates two virtual nodes. A NodeContainer is simply a list that holds these nodes, making it easy to manage them.

    • Channel and Device Configuration: A PointToPointHelper is used to create and configure a point-to-point communication channel. The key attributes, DataRate (5 Mbps) and Delay (2 ms), are set for this channel. The Install(nodes) function then creates a virtual network interface card (NIC) on each node and connects them via this channel. The resulting devices are stored in a NetDeviceContainer.


    2. Network Stack and Addressing

    This section transforms the generic nodes into functional network devices.

    • IP Stack Installation: An InternetStackHelper is used to install the full Internet Protocol (IP) stack on both nodes. This gives them the ability to handle protocols like TCP and UDP and to understand IP addressing.

    • IP Address Assignment: An Ipv4AddressHelper assigns IP addresses to the network devices. In this case, it assigns addresses from the 192.168.1.0/24 subnet. The first node gets 192.168.1.1 and the second gets 192.168.1.2, which are stored in an Ipv4InterfaceContainer.


    3. Application Layer

    This is where the actual traffic generation and reception are configured.

    • UDP Echo Server: A UdpEchoServerHelper is used to set up an echo server on the second node (nodes.Get(1)), which listens for packets on port 9.

    • UDP Echo Client: A UdpEchoClientHelper is used to set up a client on the first node (nodes.Get(0)). The client is configured to send traffic to the server's IP address (ipinterfaces.GetAddress(1)) on the correct port.

    • Event Scheduling: The Start() function is called for both the server and client applications. This schedules the server to begin listening at 1 second and the client to start sending traffic at 2 seconds. The staggered start times ensure the server is ready to receive packets before the client starts sending.


    4. Starting the Discrete-event Simulation

    • Running the Simulation: The line Simulator::Run(); is the core command that starts the discrete-event simulation. It processes all the scheduled events (like starting applications, sending packets, etc.) until there are no more events to run.


    5. Enable Logging.

    To enable the logging level of UdpEchoClientApplication to level_info we  need to use the LogComponentEnable() function.

    LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);

    This function call directly sets the logging level for a specific ns-3 component, bypassing the need to use an environment variable.

    • LogComponentEnable: This is the function that controls logging in ns-3.

    • "UdpEchoClientApplication": This is the string name of the component for which you want to enable logging.

    • LOG_LEVEL_INFO: This macro corresponds to the level_info you would set with the environment variable. Other common levels include LOG_LEVEL_DEBUG, LOG_LEVEL_WARN, and LOG_LEVEL_ERROR.

    By placing this line in your code, the log component will be enabled regardless of whether the NS_LOG environment variable is set. This is particularly useful for hardcoding specific debugging settings for a script.