Ns-3-Network-Simulator/C3/Infrastructure-based-Wireless-Network/English

From Script | Spoken-Tutorial
Revision as of 12:27, 12 April 2024 by Madhurig (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Visual Cue Narration
Show Slide: Title Slide Welcome to Spoken tutorial on Infrastructure based wireless network.
Show Slide:

Learning Objectives

In this tutorial, we will learn to
  • Create an infrastructure based wireless network.
  • Create three UDP flows in the channels.
  • Analyze the flow using a flow monitor.
  • Visualize the network using NetAnim.
Show Slide:

System Requirements

To record this tutorial, I am using
  • Ubuntu Linux OS version 22.04.
  • ns-3 version 3.38.
  • NetAnim visualizer tool.
Show slide:

Pre-requisites

To follow this tutorial:
  • The learner must have basic knowledge of ns-3 software
  • For pre-requisite ns-3 tutorials, please visit this website https://spoken-tutorial.org
Show Slide:

Code Files

  • The files used in this tutorial are provided in theCode files link.
  • Please download and extract the files.
  • Make a copy and then use them while practicing.
Show Slide:

Infrastructure based Wifi network

For this tutorial, we would be creating the below infrastructure.

In this diagram, the nodes 0, 2 and 3 are station nodes.

Node 1 is the access point of the infrastructure.

Now, let us learn about DSSS.
Show Slide:

About DSSS

DSSS rate is data rate in a Wifi system.

It uses Direct Sequence Spread Spectrum modulation.

In a DSSS system, data is spread over a wider bandwidth using a spreading code.

Show Slide:

About DSSS

The spreading code is a sequence of chips that modulates the data signal.

DSSS rate represents the speed of information transmitted after spreading.

DSSS can transfer up to 11 Mbps of data.

Open code editor I have created the source file wifi-simple-infra.cc for this program.

The source code contains the required functions to create an infrastructure based wireless network.

Now we will go through the source code in the text editor.

Highlight std::string phyMode("DsssRate1Mbps"); We set the dsss rate of the physical layer to 1 Mbps.
Highlight double rss = -80 Next, we will set the Received Signal Strength or RSS to -80 dBm.
Highlight uint32_t packetSize = 1000;

uint32_t numPackets = 100;

Now we shall set the packet size to 1000 bytes.

Then we will set the number of packets to be transmitted as 100.

Highlight Time interval = Seconds(0.003); Further, we set the time interval between the packet transfer as 0.003 seconds.
Highlight uint32_t sinkNode = 1;

uint32_t sourceNode = 0;

Let us set up the source and sink nodes for the flow that we are yet to create.
Highlight Config::SetDefault("ns3::WifiRemoteStationManager::NonUnicastMode", StringValue(phyMode)); Then we configure non-unicast mode of the wifi channel.

Non-unicast mode means that the data may be sent from one node to many nodes.

Highlight NodeContainer c;

c.Create(4);

Now, let us create a node container with four nodes.
Highlight WifiHelper wifi;

if (verbose) { WifiHelper::EnableLogComponents(); } wifi.SetStandard(WIFI_STANDARD_80211b);

We shall now put the wifi network interface cards together.

For that, let us use the WifiHelper class.

Using Wifi-helper class, we enable logging components and set the wifi standard.

Here, the standard set is IEEE 802.11b.

Highlight YansWifiPhyHelper wifiPhy;

wifiPhy.Set("RxGain", DoubleValue(0));

Then we shall use YansWifiPhyHelper class to set the RxGain attribute to zero.

RxGain is used to adjust the sensitivity of the receiving signal.

Highlight YaifiChannelHelper wifiChannel;

wifiChannel.SetPropagationDelay("ns3::ConstantSpeedPropagationDelayModel");

wifiChannel.AddPropagationLoss("ns3::FixedRssLossModel", "Rss", DoubleValue(rss));

We use YansWifiChannelHelper class to set attributes of the transmitting channel.

Let us set the Propagation delay and add the propagation loss to our model.

Highlight wifiPhy.SetChannel(wifiChannel.Create()); Next, we connect the wifi physical layer to the wifi-channel.
Highlight WifiMacHelper wifiMac;

wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager","DataMode", StringValue(phyMode), "ControlMode", StringValue(phyMode));

Now let us add MAC protocol to the Network Interface Cards that we installed.

Then we set a constant rate for data and control modes of wifi stations.

Highlight Ssid ssid = Ssid("wifi-default"); Then, we assign SSID to the network.

SSID or Service Set Identifier is a sequence of characters uniquely identifying a network.

Highlight wifiMac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid));

NetDeviceContainer staDevice = wifi.Install(wifiPhy, wifiMac, c);

NetDeviceContainer devices = staDevice;

Let us now set up the station node by setting up MAC protocol and installing NIC.

We create a container staDevice to hold the installed Wifi devices.

As a next step, we shall take a copy of staDevice container in devices container.

Highlight

wifiMac.SetType("ns3::ApWifiMac", "Ssid", SsidValue(ssid));

NetDeviceContainer apDevice = wifi.Install(wifiPhy, wifiMac, c.Get(1));

devices.Add(apDevice);

Similarly, we shall set up the Access Point of the wireless network.

We have set up the AP node of the wireless network using ApWifiMac protocol.

We are also installing wifiphy to the node1 of the wireless network.

Then ssid is assigned to the AP node of the network.

Highlight MobilityHelper mobility;

Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator>();

positionAlloc->Add(Vector(3.0, 4.0, 3.0));

mobility.SetPositionAllocator(positionAlloc);

Now set up mobility for the nodes by allocating positions for the nodes created.
Highlight mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");

mobility.Install(c);

Let us set the model to be ConstantPositionMobilityModel.

It represents the 'stationary or static nodes in a network simulation.

Also, install the model that we set, to all the nodes that we created.

Highlight InternetStackHelper internet;

internet.Install(c);

Then let us install the internet stack on all the nodes.
Highlight Ipv4AddressHelper ipv4;

ipv4.SetBase("10.1.1.0", "255.255.255.0");

Ipv4InterfaceContainer i = ipv4.Assign(devices);

Now, we shall assign IP addresses to the nodes that we created
Highlight TypeId tid = TypeId::LookupByName("ns3::UdpSocketFactory"); The highlighted command is used to get the TypeId of UdpSocketFactory.
Highlight Ptr<Socket> recvSink = Socket::CreateSocket(c.Get(sinkNode), tid); We shall create a receiving socket for the UDP Flow.
Highlight InetSocketAddress local = InetSocketAddress(Ipv4Address::GetAny(), 80); Let us create an internet socket address using any IPv4 address and port 80.
Highlight recvSink->Bind(local); Then bind the receiving socket to the local address and to the port 80.
Highlight recvSink->SetRecvCallback(MakeCallback(&ReceivePacket)); Now set the receive callback function for the receiving socket.

When the packet is received in the sinknode, we call RecievePacket function.

Highlight Ptr<Socket> source = Socket::CreateSocket(c.Get(sourceNode), tid); Let us then create an UDP socket on node 0, which is our sourceNode.
Highlight InetSocketAddress remote = InetSocketAddress(i.GetAddress(sinkNode, 0), 80); We shall create an internet socket using Ipv4 address of sinknode and port 80.
Highlight source->Connect(remote); Let us connect the source socket to the remote address and port 80.
Highlight void

ReceivePacket(Ptr<Socket> socket)

{

while (socket->Recv()){ }

}

Now let’s define the function to call when the packets are received.

Here, we are invoking the receive functionality of the socket.

Highlight static void

GenerateTraffic(Ptr<Socket> socket, uint32_t pktSize, uint32_t pktCount, Time pktInterval)

Then we define a function called generate packet traffic.
Highlight if (pktCount > 0)

{

socket->Send(Create<Packet>(pktSize));

Simulator::Schedule(pktInterval,

&GenerateTraffic,

socket,

pktSize,

pktCount - 1,

pktInterval);

}

else

{

socket->Close();

}

When the number of packets is not zero, we invoke the send function.

We also schedule the simulator to invoke the GenerateTraffic function.

When the number of packets is zero, we close the socket.

Then, we stop invoking send function.

Only Narration We have created two other UDP flows as well.
Highlight Simulator::Schedule(Seconds(

10),&GenerateTraffic,source,packetSize,numPackets,interval);

Let us schedule the simulator to callback the generate_traffic function.

This procedure happens after ten seconds from the start of execution.

Highlight Ptr<FlowMonitor> flowMonitor; Then, Initialize flow monitor for tracing.
HIghlight FlowMonitorHelper flowHelper;

flowMonitor= flowHelper.InstallAll();

We shall then install a flow monitor on all the nodes.
Highlight AnimationInterface anim ("animationwifi-infra-2.xml"); Let us create the XML output of the program.
HIghlight Ptr<Ipv4FlowClassifier> classifier=DynamicCast<Ipv4FlowClassifier> (flowHelper.GetClassifier()); Let us further create an object of the Ipv4FlowClassifier class.

The created object returns a pointer to the flow classifier.

Highlight std::map<FlowId,FlowMonitor::FlowStats> stats=flowMonitor->GetFlowStats(); To store the information about the network flow, let us use this command.
Highlight the entire for loop. Let’s retrieve the details of the flow using a for loop.

iter arrow first is used to collect the ID of the flow.

iter arrow second is used to collect the data associated with the flow.

We will analyze the type of flow and print the delay and throughput.

Highlight Simulator::Destroy() The Destroy() function ends the simulation.
Press Ctrl, Alt and T keys simultaneously

Navigate to the ns-allinone-3.38/ns3.38 directory

Type mv ~/Downloads/wifi-simple-infra.cc scratch/wifi-simple-infra.cc to move the source file to scratch directory.

Now we will observe the simulation.

Open the terminal by pressing Ctrl, Alt and T keys simultaneously.

Using the cd command, navigate to the installation directory of ns-3.

Go to the ns-3.38 directory.

Move your source file to the scratch directory within the ns-3.38 directory.

Type ./ns3 run scratch/wifi-simple-infra.cc Run the command ./ns3 space run space scratch/wifi-simple-infra.cc

wifi-simple-infra.cc is the name of the source file.

Keep the terminal open and show the output of the command After compilation, we get the details of each packet transfer.

TxPackets refers to the packets sent whereas RxPackets refers to the packets received.

We also see the average end to end delay and throughput of the wireless network.

Navigate to netanim directory under ns-allinone-3.38

Type cd ..

Type cd netanim

Now, to visualize the network, we will use NetAnim.

Navigate to the netanim directory under ns-allinone-3.38.

For that, type cd .. in the terminal window.

Then type cd netanim

Type ./NetAnim Now type ./NetAnim.

The NetAnim window should open.

Click on the Open XML trace file icon on the top left corner of the window. Click on the Open XML trace file icon on the top left corner of the window.
In the file picker, navigate to the ns-allinone-3.38/ns3.38 directory and select the animationwifi-infra-2.xml file. In the file picker, navigate to the ns-allinone-3.38, then to ns3.38 directory.

Then select the animationwifi-infra-2.xml file.

On the toolbar, click on the play button

Close the NetAnim window

On the toolbar, click on the play button to view the simulation.

We see the flow details of the wireless network.


Show Slide: Summary This brings us to the end of this tutorial. Let us summarize.

In this tutorial, we have

  • Created an infrastructure based wireless network.
  • Created three UDP flows in the channels.
  • Analyzed the flow using a flow monitor.
  • Visualized the network using NetAnim.
Show Slide : Assignment As an assignment, please do the following
  • Create a simulation for a wireless network with three nodes.
  • Create one access point (AP) and two stations.
  • The AP and stations should use an 802.11g physical layer.
  • Implement UDP traffic between the stations and the AP.
  • Set packet size as 1500 bytes and send 200 packets from each station to the AP.
  • Set the RSS at the AP to -70 dBm.
  • Set the interval between packets to 0.005 seconds.
  • Analyze the flow using flow monitor.
  • Visualize the network using NetAnim.
Show Slide:

Assignment- Observation

In the terminal window, you will get the following output.
Show Slide:

Assignment- Observation

In the NetAnim window, you would observe the following network.

Observe the packet transfer.

Show Slide:

About Spoken Tutorial Project

The video at the following link summarizes theSpoken Tutorial project.

Please download and watch it.

Show Slide:

Spoken Tutorial Workshops

TheSpoken Tutorial Project team conducts workshops and gives certificates.

For more details, please write to us.

Show Slide:

Answers for THIS Spoken Tutorial

Please post your timed queries in this forum.
Show Slide:

FOSSEE Forum

For any general or technical questions onns-3, visit the FOSSEE forum and post your question.
Show Slide:Acknowledgement Spoken Tutorial Project was established by theMinistry of Education, Government of India.
Show Slide: Acknowledgement We thankDr.Moyukh Laha fromIIT Kharagpur for his domain support.

We would also like to thankDr. R. Radha, Dr. X. Anita, and Dr.T.Subbulakshmi from VIT, Chennai for their support.

Show Slide: Thank you


This isArun Santhosh,a FOSSEE Summer Fellow 2023, IIT Bombay signing off.

Thank you for joining.

Contributors and Content Editors

Madhurig, Nirmala Venkat