How to Simulate Wireless Routing Protocol Projects Using NS3

To simulate wireless routing protocols in NS3 usually includes working with protocols intended for dynamic, wireless networks, in which nodes are mobile or form ad-hoc networks. NS3 supports numerous wireless routing protocols for MANETs (Mobile Ad-hoc Networks) such as:

  • AODV (Ad-hoc On-demand Distance Vector)
  • DSDV (Destination-Sequenced Distance Vector)
  • OLSR (Optimized Link State Routing)
  • DSR (Dynamic Source Routing)

These protocols handle the routing in wireless networks, and NS3 deliver modules to emulate each one. This guide will show how to configure wireless nodes, install a routing protocol, setup wireless communication, and generate applications to create traffic.

Steps to Simulate Wireless Routing Protocols in NS3

  1. Install NS3

Make sure NS3 is installed.

  1. Understanding Wireless Routing Protocols in NS3
  • AODV (Ad-hoc On-demand Distance Vector): A reactive protocol that generates routes only when requested by the source node.
  • DSDV (Destination-Sequenced Distance Vector): A proactive protocol that maintains up-to-date routing tables for all nodes.
  • OLSR (Optimized Link State Routing): A proactive protocol that occasionally interchange the routing information using link-state techniques.
  • DSR (Dynamic Source Routing): A reactive protocol that utilizes source routing to introduce routes.
  1. Create a Simulation Script

Here we will show on how to replicate a wireless ad-hoc network using AODV as the routing protocol. We can switch to other routing protocols such as OLSR, DSDV, or DSR by altering the routing helper.

Example Simulation with AODV Protocol

  1. Include Necessary Headers

We need to contain headers for the core network, wireless devices, internet stack, mobility, and AODV routing.

#include “ns3/core-module.h”

#include “ns3/network-module.h”

#include “ns3/internet-module.h”

#include “ns3/wifi-module.h”

#include “ns3/mobility-module.h”

#include “ns3/aodv-module.h”       // For AODV routing

#include “ns3/applications-module.h”

#include “ns3/netanim-module.h”    // For animation

  1. Create and Install Nodes

Generate a set of nodes that signify wireless devices or mobile nodes.

NodeContainer nodes;

nodes.Create(10);  // Create 10 nodes

  1. Configure Wireless Communication (Wi-Fi)

To permit wireless communication, we want to setup Wi-Fi devices and channels. NS3’s WifiHelper helps generate an ad-hoc Wi-Fi communication.

YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default();

YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default();

wifiPhy.SetChannel(wifiChannel.Create());

WifiHelper wifi;

wifi.SetStandard(WIFI_PHY_STANDARD_80211g);  // Use 802.11g

wifi.SetRemoteStationManager(“ns3::AarfWifiManager”);

WifiMacHelper wifiMac;

wifiMac.SetType(“ns3::AdhocWifiMac”);  // Ad-hoc MAC for mobile nodes

NetDeviceContainer devices = wifi.Install(wifiPhy, wifiMac, nodes);  // Install Wi-Fi on all nodes

  1. Install Internet Stack and AODV Routing Protocol

Install the internet stack (IP, TCP/UDP) and the AODV routing protocol on all the nodes. The routing protocol handles the packet forwarding in the network.

AodvHelper aodv;  // AODV routing protocol

InternetStackHelper internet;

internet.SetRoutingHelper(aodv);  // Set AODV as the routing protocol

internet.Install(nodes);  // Install internet stack on all nodes

  1. Assign IP Addresses

Allocate IP addresses to all the wireless devices. This step enables the nodes to interact using IP.

Ipv4AddressHelper address;

address.SetBase(“10.1.1.0”, “255.255.255.0”);  // Class C network

Ipv4InterfaceContainer interfaces = address.Assign(devices);

  1. Set up Mobility Model

For ad-hoc networks, mobility plays a key role. We will utilize the RandomWaypointMobilityModel to replicate mobile nodes moving around randomly in a predefined area.

MobilityHelper mobility;

mobility.SetPositionAllocator(“ns3::RandomRectanglePositionAllocator”,

“X”, StringValue(“ns3::UniformRandomVariable[Min=0.0|Max=100.0]”),

“Y”, StringValue(“ns3::UniformRandomVariable[Min=0.0|Max=100.0]”));

mobility.SetMobilityModel(“ns3::RandomWaypointMobilityModel”,

“Speed”, StringValue(“ns3::UniformRandomVariable[Min=1.0|Max=10.0]”),

“Pause”, StringValue(“ns3::ConstantRandomVariable[Constant=2.0]”),

“PositionAllocator”, StringValue(“ns3::RandomRectanglePositionAllocator”));

mobility.Install(nodes);

  1. Set up Applications

We can mimic the traffic using applications such as UDP Echo. Here, one node will behave like server and another node as the client.

  • Server (Sink Application):

uint16_t port = 8080;

UdpEchoServerHelper echoServer(port);

ApplicationContainer serverApp = echoServer.Install(nodes.Get(0));  // Install on node 0

serverApp.Start(Seconds(1.0));

serverApp.Stop(Seconds(20.0));

  • Client (OnOff Application):

UdpEchoClientHelper echoClient(interfaces.GetAddress(0), port);  // Client communicating with server (node 0)

echoClient.SetAttribute(“MaxPackets”, UintegerValue(50));

echoClient.SetAttribute(“Interval”, TimeValue(Seconds(1.0)));

echoClient.SetAttribute(“PacketSize”, UintegerValue(1024));

ApplicationContainer clientApp = echoClient.Install(nodes.Get(1));  // Client is node 1

clientApp.Start(Seconds(2.0));

clientApp.Stop(Seconds(20.0));

  1. Enable Tracing and Animation

NS3 supports packet capture (PCAP) and animation. We can envision the node movements and packet flows using NetAnim.

wifiPhy.EnablePcapAll(“aodv_wireless_simulation”);  // Enable pcap trace for all nodes

AnimationInterface anim(“aodv_wireless_simulation.xml”);  // Enable NetAnim animation output

  1. Run the Simulation

Set the stop time and execute the simulation.

Simulator::Stop(Seconds(20.0));

Simulator::Run();

Simulator::Destroy();

Example Full Script for AODV Protocol

Here is a full NS3 script simulating a wireless ad-hoc network with AODV routing:

#include “ns3/core-module.h”

#include “ns3/network-module.h”

#include “ns3/internet-module.h”

#include “ns3/wifi-module.h”

#include “ns3/mobility-module.h”

#include “ns3/aodv-module.h”

#include “ns3/applications-module.h”

#include “ns3/netanim-module.h”

using namespace ns3;

int main(int argc, char *argv[]) {

// Step 1: Create nodes

NodeContainer nodes;

nodes.Create(10);  // Create 10 nodes

// Step 2: Configure Wi-Fi devices

YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default();

YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default();

wifiPhy.SetChannel(wifiChannel.Create());

WifiHelper wifi;

wifi.SetStandard(WIFI_PHY_STANDARD_80211g);  // 802.11g standard

wifi.SetRemoteStationManager(“ns3::AarfWifiManager”);

WifiMacHelper wifiMac;

wifiMac.SetType(“ns3::AdhocWifiMac”);  // Ad-hoc MAC

NetDeviceContainer devices = wifi.Install(wifiPhy, wifiMac, nodes);

// Step 3: Install AODV routing protocol

AodvHelper aodv;

InternetStackHelper internet;

internet.SetRoutingHelper(aodv);  // Use AODV for routing

internet.Install(nodes);

// Step 4: Assign IP addresses

Ipv4AddressHelper address;

address.SetBase(“10.1.1.0”, “255.255.255.0”);  // Class C network

Ipv4InterfaceContainer interfaces = address.Assign(devices);

// Step 5: Set up mobility model

MobilityHelper mobility;

mobility.SetPositionAllocator(“ns3::RandomRectanglePositionAllocator”,

“X”, StringValue(“ns3::UniformRandomVariable[Min=0.0|Max=100.0]”),

“Y”, StringValue(“ns3::UniformRandomVariable[Min=0.0|Max=100.0]”));

mobility.SetMobilityModel(“ns3::RandomWaypointMobilityModel”,

“Speed”, StringValue(“ns3::UniformRandomVariable[Min=1.0|Max=10.0]”),

“Pause”, StringValue(“ns3::ConstantRandomVariable[Constant=2.0]”),

“PositionAllocator”, StringValue(“ns3::RandomRectanglePositionAllocator”));

mobility.Install(nodes);

// Step 6: Install applications

// Server (Sink) on node 0

uint16_t port = 8080;

UdpEchoServerHelper echoServer(port);

ApplicationContainer serverApp = echoServer.Install(nodes.Get(0));

serverApp.Start(Seconds(1.0));

serverApp.Stop(Seconds(20.0));

// Client on node 1

UdpEchoClientHelper echoClient(interfaces.GetAddress(0), port);

echoClient.SetAttribute(“MaxPackets”, UintegerValue(50));

echoClient.SetAttribute(“Interval”, TimeValue(Seconds(1.0)));

echoClient.SetAttribute(“PacketSize”, UintegerValue(1024));

ApplicationContainer clientApp = echoClient.Install(nodes.Get(1));

clientApp.Start(Seconds(2.0));

clientApp.Stop(Seconds(20.0));

// Step 7: Enable tracing and animation

wifiPhy.EnablePcapAll(“aodv_wireless_simulation”);

AnimationInterface anim(“aodv_wireless_simulation.xml”);

// Step 8: Run simulation

Simulator::Stop(Seconds(20.0));

Simulator::Run();

Simulator::Destroy();

return 0;

}

  1. Running the Simulation
  • Build and run the script using NS3’s waf system:

./waf build

./waf –run <your_script_name>

  1. Analyse Results
  • PCAP Tracing: The pcap files generated can be measured using tools such as Wireshark.
  • NetAnim: We can open the created XML file in NetAnim to envision the simulation, that contains the node movements and packet flows.
  1. Switching Wireless Routing Protocols

We can easily switch among different wireless routing protocols by altering the routing helper in the script.

  • For DSDV (Destination-Sequenced Distance Vector):

#include “ns3/dsdv-helper.h”

DsdvHelper dsdv;

internet.SetRoutingHelper(dsdv);  // Use DSDV for routing

  • For OLSR (Optimized Link State Routing):

#include “ns3/olsr-helper.h”

OlsrHelper olsr;

internet.SetRoutingHelper(olsr);  // Use OLSR for routing

  • For DSR (Dynamic Source Routing):

#include “ns3/dsr-module.h”

DsrMainHelper dsrMain;

DsrHelper dsr;

internet.Install(nodes);  // Install the internet stack first

dsrMain.Install(dsr, nodes);  // Install DSR after

In the end of the manual, we had utterly deliver the comprehensive details about how to setup and how to simulate the wireless routing protocols in ns3 simulator. More information will be shared about how the wireless routing protocols will perform in other simulation tool.

If you require any research assistance, please contact phdprime.com  with your questions, and we will offer you with the finest results possible. Working with us will ensure a hassle-free research process.

Opening Time

9:00am

Lunch Time

12:30pm

Break Time

4:00pm

Closing Time

6:30pm

  • award1
  • award2