A Discrete-Event Network Simulator
API
neighbor-cache-example.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022 ZHIHENG DONG
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Zhiheng Dong <dzh2077@gmail.com>
18  */
19 
281 #include "ns3/applications-module.h"
282 #include "ns3/core-module.h"
283 #include "ns3/csma-module.h"
284 #include "ns3/internet-module.h"
285 #include "ns3/network-module.h"
286 
287 using namespace ns3;
288 
289 NS_LOG_COMPONENT_DEFINE("NeighborCacheExample");
290 
297 {
298  public:
300 
304  void Run();
305 
311  void CommandSetup(int argc, char** argv);
312 
313  private:
322  const Address& from,
323  const Address& dst,
324  const SeqTsSizeHeader& header);
325 
326  bool m_useIpv6{false};
327  bool m_enableLog{false};
328  bool m_useChannel{false};
329  bool m_useNetDeviceContainer{
330  false};
331  bool m_useInterfaceContainer{
332  false};
333  bool m_noGenerate{false};
334  bool m_sendTraffic{false};
335 };
336 
338 {
339  NS_LOG_FUNCTION(this);
340 }
341 
342 void
344  const Address& from,
345  const Address& dst,
346  const SeqTsSizeHeader& header)
347 {
348  std::cout << "Rx pkt from " << from << " to " << dst << " -> " << header << std::endl;
349 }
350 
351 void
353 {
354  CommandLine cmd(__FILE__);
355  cmd.AddValue("useIPv6", "Use IPv6 instead of IPv4", m_useIpv6);
356  cmd.AddValue("enableLog", "Enable ArpL3Protocol and Icmpv6L4Protocol logging", m_enableLog);
357  cmd.AddValue("useChannel", "Generate neighbor cache for specific Channel", m_useChannel);
358  cmd.AddValue("useNetDeviceContainer",
359  "Generate neighbor cache for specific netDeviceContainer",
360  m_useNetDeviceContainer);
361  cmd.AddValue("useInterfaceContainer",
362  "Generate neighbor cache for specific interfaceContainer",
363  m_useInterfaceContainer);
364  cmd.AddValue("noGenerate", "do not generate neighbor cache automatically", m_noGenerate);
365  cmd.AddValue("sendTraffic", "send data stream from n0 to n1", m_sendTraffic);
366 
367  cmd.Parse(argc, argv);
368 }
369 
370 int
371 main(int argc, char* argv[])
372 {
373  NeighborCacheExample example;
374  example.CommandSetup(argc, argv);
375  example.Run();
376  return 0;
377 }
378 
379 void
381 {
382  if (m_enableLog)
383  {
384  LogComponentEnable("ArpL3Protocol", LOG_LEVEL_LOGIC);
385  LogComponentEnable("Icmpv6L4Protocol", LOG_LEVEL_LOGIC);
386  }
387  uint32_t nCsmaLeft = 2;
388  uint32_t nCsmaRight = 2;
389 
390  NodeContainer csmaNodesLeft;
391  csmaNodesLeft.Create(nCsmaLeft);
392  NodeContainer csmaNodesRight;
393  csmaNodesRight.Add(csmaNodesLeft.Get(1));
394  csmaNodesRight.Create(nCsmaRight);
395 
396  CsmaHelper csmaLeft;
397  csmaLeft.SetChannelAttribute("DataRate", StringValue("20Gbps"));
398  // The 1 microSeconds delay is only for showing packets dropped effect without generating
399  // neighbor caches.
400  csmaLeft.SetChannelAttribute("Delay", TimeValue(MicroSeconds(1)));
401  CsmaHelper csmaRight;
402  csmaRight.SetChannelAttribute("DataRate", StringValue("20Gbps"));
403  csmaRight.SetChannelAttribute("Delay", TimeValue(MicroSeconds(1)));
404 
405  NetDeviceContainer csmaDevicesLeft;
406  csmaDevicesLeft = csmaLeft.Install(csmaNodesLeft);
407  NetDeviceContainer csmaDevicesRight;
408  csmaDevicesRight = csmaRight.Install(csmaNodesRight);
409 
411  if (!m_useIpv6)
412  {
413  stack.SetIpv6StackInstall(false);
414  }
415  else
416  {
417  stack.SetIpv4StackInstall(false);
418  }
419  // disabled Ipv4ArpJitter and Ipv6NsRsJitter to avoid the influence on packet dropped
420  stack.SetIpv4ArpJitter(false);
421  stack.SetIpv6NsRsJitter(false);
422  stack.Install(csmaNodesLeft.Get(0));
423  stack.Install(csmaNodesRight);
424 
425  if (!m_useIpv6)
426  {
428  address.SetBase("10.1.1.0", "255.255.255.0");
429  Ipv4InterfaceContainer csmaInterfacesLeft;
430  csmaInterfacesLeft = address.Assign(csmaDevicesLeft);
431  address.SetBase("10.1.2.0", "255.255.255.0");
432  Ipv4InterfaceContainer csmaInterfacesRight;
433  csmaInterfacesRight = address.Assign(csmaDevicesRight);
434 
435  // Populate ARP caches
436  NeighborCacheHelper neighborCache;
437  if (m_useChannel)
438  {
439  // Populate ARP caches for given channel
440  Ptr<Channel> csmaChannel = csmaDevicesLeft.Get(0)->GetChannel();
441  neighborCache.PopulateNeighborCache(csmaChannel);
442  }
443  else if (m_useNetDeviceContainer)
444  {
445  // Populate ARP caches for given netDeviceContainer
446  neighborCache.PopulateNeighborCache(csmaDevicesRight);
447  }
448  else if (m_useInterfaceContainer)
449  {
450  std::pair<Ptr<Ipv4>, uint32_t> txInterface = csmaInterfacesLeft.Get(0);
451  Ptr<Ipv4> ipv41 = txInterface.first;
452  uint32_t index1 = txInterface.second;
453  std::pair<Ptr<Ipv4>, uint32_t> rxInterface = csmaInterfacesRight.Get(nCsmaRight);
454  Ptr<Ipv4> ipv42 = rxInterface.first;
455  uint32_t index2 = rxInterface.second;
456 
457  // Populate ARP caches for given interfaceContainer
459  interfaces.Add(ipv41, index1);
460  interfaces.Add(ipv42, index2);
461  neighborCache.PopulateNeighborCache(interfaces);
462  }
463  else if (!m_noGenerate)
464  {
465  // Populate ARP caches for all devices
466  neighborCache.PopulateNeighborCache();
467  }
468 
469  if (m_sendTraffic)
470  {
471  // send Packet from n0 to n1
472  uint16_t port = 9; // Discard port (RFC 863)
473  OnOffHelper onoff("ns3::UdpSocketFactory",
474  Address(InetSocketAddress(csmaInterfacesLeft.GetAddress(1), port)));
475  onoff.SetConstantRate(DataRate("10Gbps"));
476  onoff.SetAttribute("EnableSeqTsSizeHeader", BooleanValue(true));
477  ApplicationContainer apps = onoff.Install(csmaNodesLeft.Get(0));
478  apps.Start(Seconds(1.0));
479 
480  // Create a packet sink to receive these packets
481  PacketSinkHelper sink("ns3::UdpSocketFactory",
483  sink.SetAttribute("EnableSeqTsSizeHeader", BooleanValue(true));
484  apps = sink.Install(csmaNodesLeft);
486  "RxWithSeqTsSize",
488  AsciiTraceHelper ascii;
489  Ptr<OutputStreamWrapper> stream = ascii.CreateFileStream("neighbor-cache-example.tr");
490  csmaLeft.EnableAsciiAll(stream);
491  csmaLeft.EnablePcapAll("neighbor-cache-example");
492  }
493  else
494  {
495  Ptr<OutputStreamWrapper> outputStream = Create<OutputStreamWrapper>(&std::cout);
497  }
498  }
499  else
500  {
502  address.SetBase(Ipv6Address("2001:1::"), Ipv6Prefix(64));
503  Ipv6InterfaceContainer csmaInterfacesLeft;
504  csmaInterfacesLeft = address.Assign(csmaDevicesLeft);
505  csmaInterfacesLeft.SetForwarding(1, true);
506  csmaInterfacesLeft.SetDefaultRouteInAllNodes(1);
507 
508  address.SetBase(Ipv6Address("2001:2::"), Ipv6Prefix(64));
509  Ipv6InterfaceContainer csmaInterfacesRight;
510  csmaInterfacesRight = address.Assign(csmaDevicesRight);
511  csmaInterfacesRight.SetForwarding(0, true);
512  csmaInterfacesRight.SetDefaultRouteInAllNodes(0);
513 
514  // Populate neighbor NDISC caches
515  NeighborCacheHelper neighborCache;
516  if (m_useChannel)
517  {
518  // Populate NDISC caches for given channel
519  Ptr<Channel> csmaChannel = csmaDevicesLeft.Get(0)->GetChannel();
520  neighborCache.PopulateNeighborCache(csmaChannel);
521  }
522  else if (m_useNetDeviceContainer)
523  {
524  // Populate NDISC caches for given netDeviceContainer
525  neighborCache.PopulateNeighborCache(csmaDevicesRight);
526  }
527  else if (m_useInterfaceContainer)
528  {
529  std::pair<Ptr<Ipv6>, uint32_t> txInterface = csmaInterfacesLeft.Get(0);
530  Ptr<Ipv6> ipv61 = txInterface.first;
531  uint32_t index1 = txInterface.second;
532  std::pair<Ptr<Ipv6>, uint32_t> rxInterface = csmaInterfacesRight.Get(nCsmaRight);
533  Ptr<Ipv6> ipv62 = rxInterface.first;
534  uint32_t index2 = rxInterface.second;
535 
536  // Populate NDISC caches for given interfaceContainer
538  interfaces.Add(ipv61, index1);
539  interfaces.Add(ipv62, index2);
540  neighborCache.PopulateNeighborCache(interfaces);
541  }
542  else if (!m_noGenerate)
543  {
544  // Populate NDISC caches for all devices
545  neighborCache.PopulateNeighborCache();
546  }
547 
548  if (m_sendTraffic)
549  {
550  // send Packet from n0 to n1
551  uint16_t port = 9; // Discard port (RFC 863)
553  "ns3::UdpSocketFactory",
554  Address(Inet6SocketAddress(csmaInterfacesLeft.GetAddress(1, 1), port)));
555  onoff.SetConstantRate(DataRate("10Gbps"));
556  onoff.SetAttribute("EnableSeqTsSizeHeader", BooleanValue(true));
557  ApplicationContainer apps = onoff.Install(csmaNodesLeft.Get(0));
558  apps.Start(Seconds(1.0));
559 
560  // Create a packet sink to receive these packets
561  PacketSinkHelper sink("ns3::UdpSocketFactory",
563  sink.SetAttribute("EnableSeqTsSizeHeader", BooleanValue(true));
564  apps = sink.Install(csmaNodesLeft);
566  "RxWithSeqTsSize",
568  AsciiTraceHelper ascii;
569  Ptr<OutputStreamWrapper> stream = ascii.CreateFileStream("neighbor-cache-example.tr");
570  csmaLeft.EnableAsciiAll(stream);
571  csmaLeft.EnablePcapAll("neighbor-cache-example");
572  }
573  else
574  {
575  Ptr<OutputStreamWrapper> outputStream = Create<OutputStreamWrapper>(&std::cout);
577  }
578  }
579  Simulator::Stop(Seconds(1.00002));
580  Simulator::Run();
582 }
neighbor Cache example class.
void CommandSetup(int argc, char **argv)
description the command-line parameters.
void ReceivePacket(Ptr< const Packet > pkt, const Address &from, const Address &dst, const SeqTsSizeHeader &header)
Print the information of receive data.
void Run()
Run the example.
a polymophic address class
Definition: address.h:101
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
Ptr< Application > Get(uint32_t i) const
Get the Ptr<Application> stored in this container at a given index.
void EnableAsciiAll(std::string prefix)
Enable ascii trace output on each device (which is of the appropriate type) in the set of all nodes c...
Manage ASCII trace files for device models.
Definition: trace-helper.h:174
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we'll use to write the traced bits.
Parse command-line arguments.
Definition: command-line.h:232
build a set of CsmaNetDevice objects
Definition: csma-helper.h:48
void SetChannelAttribute(std::string n1, const AttributeValue &v1)
Definition: csma-helper.cc:56
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::CsmaChannel with the attributes configured by CsmaHelper::SetChannelAttri...
Definition: csma-helper.cc:226
An Inet6 address class.
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
static Ipv4Address GetAny()
holds a vector of std::pair of Ptr<Ipv4> and interface index.
std::pair< Ptr< Ipv4 >, uint32_t > Get(uint32_t i) const
Get the std::pair of an Ptr<Ipv4> and interface stored at the location specified by the index.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
static void PrintNeighborCacheAllAt(Time printTime, Ptr< OutputStreamWrapper > stream, Time::Unit unit=Time::S)
prints the neighbor cache of all nodes at a particular time.
Helper class to auto-assign global IPv6 unicast addresses.
Describes an IPv6 address.
Definition: ipv6-address.h:49
static Ipv6Address GetAny()
Get the "any" (::) Ipv6Address.
Keep track of a set of IPv6 interfaces.
void SetForwarding(uint32_t i, bool state)
Set the state of the stack (act as a router or as an host) for the specified index.
void SetDefaultRouteInAllNodes(uint32_t router)
Set the default route for all the devices (except the router itself).
Ipv6Address GetAddress(uint32_t i, uint32_t j) const
Get the address for the specified index.
std::pair< Ptr< Ipv6 >, uint32_t > Get(uint32_t i) const
Get the std::pair of an Ptr<Ipv6> and interface stored at the location specified by the index.
Describes an IPv6 prefix.
Definition: ipv6-address.h:455
static void PrintNeighborCacheAllAt(Time printTime, Ptr< OutputStreamWrapper > stream, Time::Unit unit=Time::S)
prints the neighbor cache of all nodes at a particular time.
A helper class to populate neighbor cache.
void PopulateNeighborCache()
Populate neighbor ARP and NDISC caches for all devices.
holds a vector of ns3::NetDevice pointers
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
void Add(const NodeContainer &nc)
Append the contents of another NodeContainer to the end of this container.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
bool TraceConnectWithoutContext(std::string name, const CallbackBase &cb)
Connect a TraceSource to a Callback without a context.
Definition: object-base.cc:315
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:44
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
void EnablePcapAll(std::string prefix, bool promiscuous=false)
Enable pcap output on each device (which is of the appropriate type) in the set of all nodes created ...
Header with a sequence, a timestamp, and a "size" attribute.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
Hold variables of type string.
Definition: string.h:56
uint16_t port
Definition: dsdv-manet.cc:44
void ReceivePacket(Ptr< Socket > socket)
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
void(* DataRate)(DataRate oldValue, DataRate newValue)
TracedValue callback signature for DataRate.
Definition: data-rate.h:327
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1350
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
address
Definition: first.py:47
stack
Definition: first.py:44
interfaces
Definition: first.py:50
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:302
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:704
@ LOG_LEVEL_LOGIC
LOG_LOGIC and above.
Definition: log.h:110
cmd
Definition: second.py:40
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition: wifi-tcp.cc:55