Multihop wifi static routing

686 views
Skip to first unread message

vahid Saber

unread,
Jan 12, 2018, 6:53:44 AM1/12/18
to ns-3-users
I have a simple question,
I am modifying wifi-simple-adhoc.cc : To have 3 nodes instead of 2. And last node sends packet to first node via middle node using static routing. It doesn't work. This is different from another example static-routing-slash32.cc. I have only one interface on each node. It doesnt work. Can you please help me with it? I am commenting the modifications to the code.
Thank you


#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/mobility-module.h"
#include "ns3/config-store-module.h"
#include "ns3/wifi-module.h"
#include "ns3/internet-module.h"
#include "ns3/ipv4-static-routing-helper.h"//Added


#include <iostream>
#include <fstream>
#include <vector>
#include <string>


using namespace ns3;


NS_LOG_COMPONENT_DEFINE
("wsa");


void ReceivePacket (Ptr<Socket> socket)
{
 
while (socket->Recv ())
   
{
      NS_LOG_UNCOND
("Received one packet!");
   
}
}


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


 
if (pktCount > 0)
   
{
      std
::cout << "Generating Traffic: Sent: " << socket->Send (Create<Packet> (pktSize)) << std::endl;
     
Simulator::Schedule (pktInterval, &GenerateTraffic,
                           socket
, pktSize,pktCount-1, pktInterval);
   
}
 
else
   
{
      socket
->Close ();
   
}
}




int main (int argc, char *argv[])
{
  std
::string phyMode ("DsssRate1Mbps");
 
double rss = -80;
  uint32_t packetSize
= 1000;
  uint32_t numPackets
= 10;
 
double interval = 1.0;
 
bool verbose = false;


 
CommandLine cmd;


  cmd
.AddValue ("phyMode", "Wifi Phy mode", phyMode);
  cmd
.AddValue ("rss", "received signal strength", rss);
  cmd
.AddValue ("packetSize", "size of application packet sent", packetSize);
  cmd
.AddValue ("numPackets", "number of packets generated", numPackets);
  cmd
.AddValue ("interval", "interval (seconds) between packets", interval);
  cmd
.AddValue ("verbose", "turn on all WifiNetDevice log components", verbose);


  cmd
.Parse (argc, argv);
 
Time interPacketInterval = Seconds (interval);


 
Config::SetDefault ("ns3::WifiRemoteStationManager::FragmentationThreshold", StringValue ("2200"));
 
Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue ("2200"));
 
Config::SetDefault ("ns3::WifiRemoteStationManager::NonUnicastMode",
                     
StringValue (phyMode));


 
NodeContainer c;
  c
.Create (3);


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


 
YansWifiPhyHelper wifiPhy =  YansWifiPhyHelper::Default ();
  wifiPhy
.Set ("RxGain", DoubleValue (0) );
  wifiPhy
.SetPcapDataLinkType (YansWifiPhyHelper::DLT_IEEE802_11_RADIO);


 
YansWifiChannelHelper wifiChannel;
  wifiChannel
.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
 
//  wifiChannel.AddPropagationLoss ("ns3::FixedRssLossModel","Rss",DoubleValue (rss));
  wifiChannel.AddPropagationLoss ("ns3::RangePropagationLossModel", "MaxRange", DoubleValue (100));//Modified
  wifiPhy
.SetChannel (wifiChannel.Create ());


 
NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default ();
  wifi
.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
                               
"DataMode",StringValue (phyMode),
                               
"ControlMode",StringValue (phyMode));
  wifiMac
.SetType ("ns3::AdhocWifiMac");
 
NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, c);


 
MobilityHelper mobility;
 
Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
 
//Modified:
  positionAlloc
->Add (Vector (0.0, 0.0, 0.0));
  positionAlloc
->Add (Vector (90.0, 0.0, 0.0));
  positionAlloc
->Add (Vector (150.0, 0.0, 0.0));
  mobility
.SetPositionAllocator (positionAlloc);
  mobility
.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
  mobility
.Install (c);


 
InternetStackHelper internet;
  internet
.Install (c);


 
Ipv4AddressHelper ipv4;
  NS_LOG_INFO
("Assign IP Addresses.");
  ipv4
.SetBase ("10.1.1.0", "255.255.255.0");
 
Ipv4InterfaceContainer i = ipv4.Assign (devices);


 
//Modified(Added)
 
Ptr<Ipv4> ipv4c2 = c.Get (2)->GetObject<Ipv4> ();
 
Ipv4StaticRoutingHelper ipv4RoutingHelper;
 
Ptr<Ipv4StaticRouting> staticRoutingC2 = ipv4RoutingHelper.GetStaticRouting (ipv4c2);
  staticRoutingC2
->AddHostRouteTo (Ipv4Address ("10.1.1.1"), Ipv4Address ("10.1.1.2"), 0);


 
TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
 
Ptr<Socket> recvSink = Socket::CreateSocket (c.Get (0), tid);
 
InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), 80);
  recvSink
->Bind (local);
  recvSink
->SetRecvCallback (MakeCallback (&ReceivePacket));


//  Ptr<Socket> source = Socket::CreateSocket (c.Get (1), tid);
 
Ptr<Socket> source = Socket::CreateSocket (c.Get (2), tid);//Modified


 
InetSocketAddress remote = InetSocketAddress (Ipv4Address ("255.255.255.255"), 80);
  source
->SetAllowBroadcast (true);
  source
->Connect (remote);


  wifiPhy
.EnablePcap ("wifi-simple-adhoc", devices);


  NS_LOG_UNCOND
("Testing " << numPackets  << " packets sent with receiver rss " << rss );


 
Simulator::ScheduleWithContext (source->GetNode ()->GetId (),
                                 
Seconds (1.0), &GenerateTraffic,
                                  source
, packetSize, numPackets, interPacketInterval);


 
Simulator::Run ();
 
Simulator::Destroy ();


 
return 0;
}


Konstantinos

unread,
Jan 12, 2018, 10:56:27 AM1/12/18
to ns-3-users
Hi,

I think you need another set of the route from the middle node to the receiver. 

vahid Saber

unread,
Jan 12, 2018, 5:02:57 PM1/12/18
to ns-3-users
Hi Konstantinos,
OK I guess I am going to learn some other new concept today, some basics that I had to learn sometime ago :)
Just like your suggestion, I added another routing like the following but it didn't work:
 
 //Modified(Added)
 
Ptr<Ipv4> ipv4c1 = c.Get (1)->GetObject<Ipv4> ();
 
Ptr<Ipv4StaticRouting> staticRoutingC1 = ipv4RoutingHelper.GetStaticRouting (ipv4c1);  staticRoutingC1->AddHostRouteTo (Ipv4Address ("10.1.1.1"), 1);

Then I removed it and this time replaced the source&sink sockets with an onoff application. So the end of the script looks now like this:

  //Modified(Added)
 
Ipv4StaticRoutingHelper ipv4RoutingHelper;

 
Ptr<Ipv4> ipv4c2 = c.Get (2)->GetObject<Ipv4> ();

 
Ptr<Ipv4StaticRouting> staticRoutingC2 = ipv4RoutingHelper.GetStaticRouting (ipv4c2);

  staticRoutingC2
->AddHostRouteTo (Ipv4Address ("10.1.1.1"), Ipv4Address ("10.1.1.2"), 1);

//  TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
//  Ptr<Socket> recvSink = Socket::CreateSocket (c.Get (0), tid);
//  InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), 80);
//  recvSink->Bind (local);
//  recvSink->SetRecvCallback (MakeCallback (&ReceivePacket));
//
////  Ptr<Socket> source = Socket::CreateSocket (c.Get (1), tid);
//  Ptr<Socket> source = Socket::CreateSocket (c.Get (2), tid);//Modified
//
//  InetSocketAddress remote = InetSocketAddress (Ipv4Address ("255.255.255.255"), 80);
//  source->SetAllowBroadcast (true);
//  source->Connect (remote);
//
//  wifiPhy.EnablePcap ("wifi-simple-adhoc", devices);
//
//  NS_LOG_UNCOND ("Testing " << numPackets  << " packets sent with receiver rss " << rss );
//
//  Simulator::ScheduleWithContext (source->GetNode ()->GetId (),
//                                  Seconds (1.0), &GenerateTraffic,
//                                  source, packetSize, numPackets, interPacketInterval);


  uint16_t port
= 9;   // Discard port (RFC 863)
 
OnOffHelper onoff ("ns3::UdpSocketFactory",
                     
Address (InetSocketAddress (i.GetAddress(0), port)));
  onoff
.SetConstantRate (DataRate (6000));
 
ApplicationContainer apps = onoff.Install (c.Get(2));
  apps
.Start (Seconds (1.0));
  apps
.Stop (Seconds (10.0));


 
// Create a packet sink to receive these packets
 
PacketSinkHelper sink ("ns3::UdpSocketFactory",
                         
Address (InetSocketAddress (Ipv4Address::GetAny (), port)));
  apps
= sink.Install (c.Get(0));
  apps
.Start (Seconds (1.0));
  apps
.Stop (Seconds (10.0));



 
Simulator::Run ();
 
Simulator::Destroy ();



 
//  //Modified(Added)
 
Ptr<PacketSink> sink1 = DynamicCast<PacketSink> (apps.Get (0));
  std
::cout << "Total Bytes Received: " << sink1->GetTotalRx () << std::endl;

The last std::cout outputted like this: Total Bytes Received: 6656
Which means I am getting something. To double check, I commented out my first original static routing and the above number turned Zero.

It might look like I have solved my problem, however, further down the line, those sink/source sockets are probably what I will need in my application and not an onoff application. So, can you please help me complete my script using the original  udp source/sink socket?
I really appreciate your help.

Thanks
Vahid

hasti pashazadeh

unread,
Jan 2, 2020, 11:07:35 AM1/2/20
to ns-3-users
hi
i want to print next hop in olsr routing in a csv file .
how can i do that?

best regards,
hasti
Reply all
Reply to author
Forward
0 new messages