NS3 Static Arp Example
In this lab, I will show how to add the static arp record in the node so that the node does not need to send out the arp request packet.
test-static-arp.cc (put this file under scratch)
#include "ns3/core-module.h" #include "ns3/global-route-manager.h" #include "ns3/network-module.h" #include "ns3/internet-module.h" #include "ns3/wifi-module.h" #include "ns3/mobility-module.h" #include "ns3/netanim-module.h" #include "ns3/applications-module.h"
using namespace ns3;
int main(int argc, char *argv[]) { LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO); LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
CommandLine cmd; cmd.Parse (argc, argv);
NodeContainer n; n.Create (3);
WifiHelper wifi; wifi.SetStandard (WIFI_STANDARD_80211a); WifiMacHelper wifiMac; YansWifiPhyHelper wifiPhy; YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default (); wifiMac.SetType ("ns3::AdhocWifiMac"); wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode", StringValue ("OfdmRate54Mbps")); wifiPhy.SetChannel (wifiChannel.Create ()); NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, n); MobilityHelper mobility; Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> (); positionAlloc->Add (Vector (0.0, 0.0, 0.0)); positionAlloc->Add (Vector (5.0, 0.0, 0.0)); positionAlloc->Add (Vector (10.0, 0.0, 0.0)); mobility.SetPositionAllocator (positionAlloc); mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); mobility.Install (n); InternetStackHelper stack; Ipv4ListRoutingHelper list; Ipv4StaticRoutingHelper staticRouting; list.Add (staticRouting, 0); stack.SetRoutingHelper (list); stack.Install (n);
Ipv4AddressHelper ipv4; ipv4.SetBase ("10.1.1.0", "255.255.255.0"); Ipv4InterfaceContainer i = ipv4.Assign (devices);
//add the static arp to N0
Ptr<ArpCache> arp = CreateObject<ArpCache>(); arp->SetAliveTimeout(Seconds(3600 * 24 * 365)); ArpCache::Entry
* entry = arp->Add(Ipv4Address
("10.1.1.2")); entry->SetMacAddress(Mac48Address("00:00:00:00:00:02")); entry->MarkPermanent(); std::pair<Ptr<Ipv4>,
uint32_t> returnValue = i.Get
(0); Ptr<Ipv4>
myipv4 = returnValue.first; uint32_t index = returnValue.second; Ptr<Ipv4Interface>
iface =
myipv4->GetObject<Ipv4L3Protocol>
()->GetInterface (index); iface->SetAttribute("ArpCache",
PointerValue(arp));
Ptr<Ipv4> ipv4N0 = n.Get (0)->GetObject<Ipv4> (); Ptr<Ipv4> ipv4N1 = n.Get (1)->GetObject<Ipv4> (); Ptr<Ipv4> ipv4N2 = n.Get (2)->GetObject<Ipv4> ();
Ipv4StaticRoutingHelper ipv4RoutingHelper; Ptr<Ipv4StaticRouting> staticRoutingN0 = ipv4RoutingHelper.GetStaticRouting (ipv4N0); staticRoutingN0->AddHostRouteTo (Ipv4Address ("10.1.1.3"), Ipv4Address ("10.1.1.2"), 1);
Ptr<Ipv4StaticRouting> staticRoutingN1 = ipv4RoutingHelper.GetStaticRouting (ipv4N1); staticRoutingN1->AddHostRouteTo (Ipv4Address ("10.1.1.3"), Ipv4Address ("10.1.1.3"), 1); staticRoutingN1->AddHostRouteTo (Ipv4Address ("10.1.1.1"), Ipv4Address ("10.1.1.1"), 1);
Ptr<Ipv4StaticRouting> staticRoutingN2 = ipv4RoutingHelper.GetStaticRouting (ipv4N2); staticRoutingN2->AddHostRouteTo (Ipv4Address ("10.1.1.1"), Ipv4Address ("10.1.1.2"), 1);
UdpEchoServerHelper echoServer (9); ApplicationContainer serverApps = echoServer.Install (n.Get(2)); serverApps.Start (Seconds (0.0)); serverApps.Stop (Seconds (10.0)); UdpEchoClientHelper echoClient (i.GetAddress (2), 9); echoClient.SetAttribute ("MaxPackets", UintegerValue (1)); echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0))); echoClient.SetAttribute ("PacketSize", UintegerValue (1024)); ApplicationContainer clientApps = echoClient.Install (n.Get(0)); clientApps.Start (Seconds (2.0)); clientApps.Stop (Seconds (10.0));
AsciiTraceHelper ascii; wifiPhy.EnableAsciiAll (ascii.CreateFileStream ("test-static-arp.tr"));
uint32_t stopTime = 11; Simulator::Stop (Seconds (stopTime)); Simulator::Run (); Simulator::Destroy (); } |
Execution
First remove the red lines. Run the simulation.
You will get test-static-arp.tr and open this file.
Record 1: N0 (NodeList/0/) will send out the ARP request packet.
Record 2: N1 (NodeList/1/) receives the ARP request packet.
Record 3: N2 (NodeList/2/) receives the ARP request packet.
Record 4: N1 (NodeList/1/) sends out the ARP reply packet.
Record 5: N0 (NodeList/0/) receives the ARP reply packet.
Now add the red lines. Run the simulation again.
N0 does not need to send out the arp request packet first. N0 can directly send out the data packet.
Last Modified: 2022/2/12 done
[Author]
Dr. Chih-Heng Ke
Department
of Computer Science and Information Engineering, National Quemoy
University, Kinmen, Taiwan
Email:
smallko@gmail.com