To implement LAN (Local Area Network) protocols in ns-3, we have to set up a network which contains nodes that connected through LAN technologies such as ethernet, configures routing and addresses, and simulates particular LAN protocols
. In this guide, let’s walk through the LAN protocols by creating a basic set up that uses ethernet in ns3.
Steps to implement LAN protocol in ns3
- Set up your environment
Make sure that ns3 is installed in the computer. If not, install it from the official ns3 website.
- Create a new ns3 script
On your ns3 installation, create a new simulation script in the scratch directory.
- Implement the LAN network
Below is the example script for setting up a LAN network using ethernet in ns3.
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/internet-module.h”
#include “ns3/point-to-point-module.h”
#include “ns3/csma-module.h”
#include “ns3/applications-module.h”
using namespace ns3;
int main (int argc, char *argv[])
{
CommandLine cmd;
cmd.Parse (argc, argv);
// Create nodes
NodeContainer nodes;
nodes.Create (5);
// Set up a CSMA (Ethernet) channel
CsmaHelper csma;
csma.SetChannelAttribute (“DataRate”, StringValue (“100Mbps”));
csma.SetChannelAttribute (“Delay”, TimeValue (NanoSeconds (6560)));
// Install devices on nodes
NetDeviceContainer devices = csma.Install (nodes);
// Install the internet stack on nodes
InternetStackHelper stack;
stack.Install (nodes);
// Assign IP addresses to the devices
Ipv4AddressHelper address;
address.SetBase (“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces = address.Assign (devices);
// Set up applications (e.g., a UDP echo server and client)
UdpEchoServerHelper echoServer (9);
ApplicationContainer serverApps = echoServer.Install (nodes.Get (4));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
UdpEchoClientHelper echoClient (interfaces.GetAddress (4), 9);
echoClient.SetAttribute (“MaxPackets”, UintegerValue (1));
echoClient.SetAttribute (“Interval”, TimeValue (Seconds (1.0)));
echoClient.SetAttribute (“PacketSize”, UintegerValue (1024));
ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
// Enable tracing
AsciiTraceHelper ascii;
csma.EnableAsciiAll (ascii.CreateFileStream (“lan-simulation.tr”));
csma.EnablePcapAll (“lan-simulation”);
// Run the simulation
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
Explanation
- Node creation :
For the LAN, create five nodes.
- CSMA channel set up :
With specific data rate and delay attribute, configure a CSMA (ethernet) channel.
- Device installation :
On the nodes, install CSMA devices.
- Internet stack installation :
On the nodes, install the internet stack.
- IP address assignment :
Assigns IP addresses to the CSMA devices.
- Application setup :
On one node, set up a UDP echo server and on the another node, setup a UDP echo client to test connectivity.
- Tracing :
To capture packet information, enable ASCII and pcap tracing for analysis.
- Simulation execution :
Run the simulation.
- Build and run the simulation
Build and run your script after writing.
./waf build
./waf –run scratch/lan-simulation
- Analyze the results
You can analyze the results after running the simulation, by generating trace files (lan-simulation.tr and lan-simulation-0-0.pcap).
Example script for LAN protocol
Below is the example script for your reference:
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/internet-module.h”
#include “ns3/point-to-point-module.h”
#include “ns3/csma-module.h”
#include “ns3/applications-module.h”
using namespace ns3;
int main (int argc, char *argv[])
{
CommandLine cmd;
cmd.Parse (argc, argv);
// Create nodes
NodeContainer nodes;
nodes.Create (5);
// Set up a CSMA (Ethernet) channel
CsmaHelper csma;
csma.SetChannelAttribute (“DataRate”, StringValue (“100Mbps”));
csma.SetChannelAttribute (“Delay”, TimeValue (NanoSeconds (6560)));
// Install devices on nodes
NetDeviceContainer devices = csma.Install (nodes);
// Install the internet stack on nodes
InternetStackHelper stack;
stack.Install (nodes);
// Assign IP addresses to the devices
Ipv4AddressHelper address;
address.SetBase (“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces = address.Assign (devices);
// Set up applications (e.g., a UDP echo server and client)
UdpEchoServerHelper echoServer (9);
ApplicationContainer serverApps = echoServer.Install (nodes.Get (4));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
UdpEchoClientHelper echoClient (interfaces.GetAddress (4), 9);
echoClient.SetAttribute (“MaxPackets”, UintegerValue (1));
echoClient.SetAttribute (“Interval”, TimeValue (Seconds (1.0)));
echoClient.SetAttribute (“PacketSize”, UintegerValue (1024));
ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
// Enable tracing
AsciiTraceHelper ascii;
csma.EnableAsciiAll (ascii.CreateFileStream (“lan-simulation.tr”));
csma.EnablePcapAll (“lan-simulation”);
// Run the simulation
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
Overall, we had a deep guide on implementing LAN (Local Area Network) in ns3 by creating a basic set up which has a network of nodes connected through the ethernet. Also, we provide more related information on LAN (Local Area Network) protocol.
Performance Analysis on LAN (Local Area Network) protocols in ns-3, will be done by us based on you project with best implementation, reach us at ns3simualtion.com