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#include "ns3/point-to-point-module.h"
287
288using namespace ns3;
289
290NS_LOG_COMPONENT_DEFINE("NeighborCacheExample");
291
298{
299 public:
301
305 void Run();
306
312 void CommandSetup(int argc, char** argv);
313
314 private:
323 const Address& from,
324 const Address& dst,
325 const SeqTsSizeHeader& header);
326
327 bool m_useIpv6{false};
328 bool m_enableLog{false};
329 bool m_useChannel{false};
331 false};
333 false};
334 bool m_noGenerate{false};
335 bool m_sendTraffic{false};
336};
337
339{
340 NS_LOG_FUNCTION(this);
341}
342
343void
345 const Address& from,
346 const Address& dst,
347 const SeqTsSizeHeader& header)
348{
349 std::cout << "Rx pkt from " << from << " to " << dst << " -> " << header << std::endl;
350}
351
352void
354{
355 CommandLine cmd(__FILE__);
356 cmd.AddValue("useIPv6", "Use IPv6 instead of IPv4", m_useIpv6);
357 cmd.AddValue("enableLog", "Enable ArpL3Protocol and Icmpv6L4Protocol logging", m_enableLog);
358 cmd.AddValue("useChannel", "Generate neighbor cache for specific Channel", m_useChannel);
359 cmd.AddValue("useNetDeviceContainer",
360 "Generate neighbor cache for specific netDeviceContainer",
362 cmd.AddValue("useInterfaceContainer",
363 "Generate neighbor cache for specific interfaceContainer",
365 cmd.AddValue("noGenerate", "do not generate neighbor cache automatically", m_noGenerate);
366 cmd.AddValue("sendTraffic", "send data stream from n0 to n1", m_sendTraffic);
367
368 cmd.Parse(argc, argv);
369}
370
371int
372main(int argc, char* argv[])
373{
374 NeighborCacheExample example;
375 example.CommandSetup(argc, argv);
376 example.Run();
377 return 0;
378}
379
380void
382{
383 if (m_enableLog)
384 {
385 LogComponentEnable("ArpL3Protocol", LOG_LEVEL_LOGIC);
386 LogComponentEnable("Icmpv6L4Protocol", LOG_LEVEL_LOGIC);
387 }
388 uint32_t nCsmaLeft = 2;
389 uint32_t nCsmaRight = 2;
390
391 NodeContainer csmaNodesLeft;
392 csmaNodesLeft.Create(nCsmaLeft);
393 NodeContainer csmaNodesRight;
394 csmaNodesRight.Add(csmaNodesLeft.Get(1));
395 csmaNodesRight.Create(nCsmaRight);
396
397 CsmaHelper csmaLeft;
398 csmaLeft.SetChannelAttribute("DataRate", StringValue("20Gbps"));
399 // The 1 microSeconds delay is only for showing packets dropped effect without generating
400 // neighbor caches.
401 csmaLeft.SetChannelAttribute("Delay", TimeValue(MicroSeconds(1)));
402 CsmaHelper csmaRight;
403 csmaRight.SetChannelAttribute("DataRate", StringValue("20Gbps"));
404 csmaRight.SetChannelAttribute("Delay", TimeValue(MicroSeconds(1)));
405
406 NetDeviceContainer csmaDevicesLeft;
407 csmaDevicesLeft = csmaLeft.Install(csmaNodesLeft);
408 NetDeviceContainer csmaDevicesRight;
409 csmaDevicesRight = csmaRight.Install(csmaNodesRight);
410
412 if (!m_useIpv6)
413 {
414 stack.SetIpv6StackInstall(false);
415 }
416 else
417 {
418 stack.SetIpv4StackInstall(false);
419 }
420 // disabled Ipv4ArpJitter and Ipv6NsRsJitter to avoid the influence on packet dropped
421 stack.SetIpv4ArpJitter(false);
422 stack.SetIpv6NsRsJitter(false);
423 stack.Install(csmaNodesLeft.Get(0));
424 stack.Install(csmaNodesRight);
425
426 if (!m_useIpv6)
427 {
429 address.SetBase("10.1.1.0", "255.255.255.0");
430 Ipv4InterfaceContainer csmaInterfacesLeft;
431 csmaInterfacesLeft = address.Assign(csmaDevicesLeft);
432 address.SetBase("10.1.2.0", "255.255.255.0");
433 Ipv4InterfaceContainer csmaInterfacesRight;
434 csmaInterfacesRight = address.Assign(csmaDevicesRight);
435
436 // Populate ARP caches
437 NeighborCacheHelper neighborCache;
438 if (m_useChannel)
439 {
440 // Populate ARP caches for given channel
441 Ptr<Channel> csmaChannel = csmaDevicesLeft.Get(0)->GetChannel();
442 neighborCache.PopulateNeighborCache(csmaChannel);
443 }
445 {
446 // Populate ARP caches for given netDeviceContainer
447 neighborCache.PopulateNeighborCache(csmaDevicesRight);
448 }
450 {
451 std::pair<Ptr<Ipv4>, uint32_t> txInterface = csmaInterfacesLeft.Get(0);
452 Ptr<Ipv4> ipv41 = txInterface.first;
453 uint32_t index1 = txInterface.second;
454 std::pair<Ptr<Ipv4>, uint32_t> rxInterface = csmaInterfacesRight.Get(nCsmaRight);
455 Ptr<Ipv4> ipv42 = rxInterface.first;
456 uint32_t index2 = rxInterface.second;
457
458 // Populate ARP caches for given interfaceContainer
460 interfaces.Add(ipv41, index1);
461 interfaces.Add(ipv42, index2);
462 neighborCache.PopulateNeighborCache(interfaces);
463 }
464 else if (!m_noGenerate)
465 {
466 // Populate ARP caches for all devices
467 neighborCache.PopulateNeighborCache();
468 }
469
470 if (m_sendTraffic)
471 {
472 // send Packet from n0 to n1
473 uint16_t port = 9; // Discard port (RFC 863)
474 OnOffHelper onoff("ns3::UdpSocketFactory",
475 Address(InetSocketAddress(csmaInterfacesLeft.GetAddress(1), port)));
476 onoff.SetConstantRate(DataRate("10Gbps"));
477 onoff.SetAttribute("EnableSeqTsSizeHeader", BooleanValue(true));
478 ApplicationContainer apps = onoff.Install(csmaNodesLeft.Get(0));
479 apps.Start(Seconds(1.0));
480
481 // Create a packet sink to receive these packets
482 PacketSinkHelper sink("ns3::UdpSocketFactory",
483 Address(InetSocketAddress(Ipv4Address::GetAny(), port)));
484 sink.SetAttribute("EnableSeqTsSizeHeader", BooleanValue(true));
485 apps = sink.Install(csmaNodesLeft);
487 "RxWithSeqTsSize",
489 AsciiTraceHelper ascii;
490 Ptr<OutputStreamWrapper> stream = ascii.CreateFileStream("neighbor-cache-example.tr");
491 csmaLeft.EnableAsciiAll(stream);
492 csmaLeft.EnablePcapAll("neighbor-cache-example");
493 }
494 else
495 {
496 Ptr<OutputStreamWrapper> outputStream = Create<OutputStreamWrapper>(&std::cout);
497 Ipv4RoutingHelper::PrintNeighborCacheAllAt(Seconds(0), outputStream);
498 }
499 }
500 else
501 {
503 address.SetBase(Ipv6Address("2001:1::"), Ipv6Prefix(64));
504 Ipv6InterfaceContainer csmaInterfacesLeft;
505 csmaInterfacesLeft = address.Assign(csmaDevicesLeft);
506 csmaInterfacesLeft.SetForwarding(1, true);
507 csmaInterfacesLeft.SetDefaultRouteInAllNodes(1);
508
509 address.SetBase(Ipv6Address("2001:2::"), Ipv6Prefix(64));
510 Ipv6InterfaceContainer csmaInterfacesRight;
511 csmaInterfacesRight = address.Assign(csmaDevicesRight);
512 csmaInterfacesRight.SetForwarding(0, true);
513 csmaInterfacesRight.SetDefaultRouteInAllNodes(0);
514
515 // Populate neighbor NDISC caches
516 NeighborCacheHelper neighborCache;
517 if (m_useChannel)
518 {
519 // Populate NDISC caches for given channel
520 Ptr<Channel> csmaChannel = csmaDevicesLeft.Get(0)->GetChannel();
521 neighborCache.PopulateNeighborCache(csmaChannel);
522 }
524 {
525 // Populate NDISC caches for given netDeviceContainer
526 neighborCache.PopulateNeighborCache(csmaDevicesRight);
527 }
529 {
530 std::pair<Ptr<Ipv6>, uint32_t> txInterface = csmaInterfacesLeft.Get(0);
531 Ptr<Ipv6> ipv61 = txInterface.first;
532 uint32_t index1 = txInterface.second;
533 std::pair<Ptr<Ipv6>, uint32_t> rxInterface = csmaInterfacesRight.Get(nCsmaRight);
534 Ptr<Ipv6> ipv62 = rxInterface.first;
535 uint32_t index2 = rxInterface.second;
536
537 // Populate NDISC caches for given interfaceContainer
539 interfaces.Add(ipv61, index1);
540 interfaces.Add(ipv62, index2);
541 neighborCache.PopulateNeighborCache(interfaces);
542 }
543 else if (!m_noGenerate)
544 {
545 // Populate NDISC caches for all devices
546 neighborCache.PopulateNeighborCache();
547 }
548
549 if (m_sendTraffic)
550 {
551 // send Packet from n0 to n1
552 uint16_t port = 9; // Discard port (RFC 863)
553 OnOffHelper onoff(
554 "ns3::UdpSocketFactory",
555 Address(Inet6SocketAddress(csmaInterfacesLeft.GetAddress(1, 1), port)));
556 onoff.SetConstantRate(DataRate("10Gbps"));
557 onoff.SetAttribute("EnableSeqTsSizeHeader", BooleanValue(true));
558 ApplicationContainer apps = onoff.Install(csmaNodesLeft.Get(0));
559 apps.Start(Seconds(1.0));
560
561 // Create a packet sink to receive these packets
562 PacketSinkHelper sink("ns3::UdpSocketFactory",
563 Address(Inet6SocketAddress(Ipv6Address::GetAny(), port)));
564 sink.SetAttribute("EnableSeqTsSizeHeader", BooleanValue(true));
565 apps = sink.Install(csmaNodesLeft);
567 "RxWithSeqTsSize",
569 AsciiTraceHelper ascii;
570 Ptr<OutputStreamWrapper> stream = ascii.CreateFileStream("neighbor-cache-example.tr");
571 csmaLeft.EnableAsciiAll(stream);
572 csmaLeft.EnablePcapAll("neighbor-cache-example");
573 }
574 else
575 {
576 Ptr<OutputStreamWrapper> outputStream = Create<OutputStreamWrapper>(&std::cout);
577 Ipv6RoutingHelper::PrintNeighborCacheAllAt(Seconds(0), outputStream);
578 }
579 }
580 Simulator::Stop(Seconds(1.00002));
581 Simulator::Run();
582 Simulator::Destroy();
583}
neighbor Cache example class.
void CommandSetup(int argc, char **argv)
description the command-line parameters.
bool m_sendTraffic
send data stream from n0 to n1
bool m_noGenerate
do not generate neighbor cache automatically
void ReceivePacket(Ptr< const Packet > pkt, const Address &from, const Address &dst, const SeqTsSizeHeader &header)
Print the information of receive data.
bool m_useNetDeviceContainer
Generate neighbor cache for specific netDeviceContainer.
bool m_useChannel
Generate neighbor cache for specific Channel.
void Run()
Run the example.
bool m_useIpv6
Use IPv6 instead of IPv4.
bool m_enableLog
Enable ArpL3Protocol and Icmpv6L4Protocol logging.
bool m_useInterfaceContainer
Generate neighbor cache for specific interfaceContainer.
a polymophic address class
Definition: address.h:100
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:173
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.
AttributeValue implementation for Boolean.
Definition: boolean.h:37
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.
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
Helper class to auto-assign global IPv6 unicast addresses.
Describes an IPv6 address.
Definition: ipv6-address.h:49
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
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:311
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:44
void SetConstantRate(DataRate dataRate, uint32_t packetSize=512)
Helper function to set a constant rate source.
ApplicationContainer Install(NodeContainer c) const
Install an ns3::OnOffApplication on each node of the input container configured with all the attribut...
void SetAttribute(std::string name, const AttributeValue &value)
Helper function used to set the underlying application attributes.
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.
Hold variables of type string.
Definition: string.h:56
AttributeValue implementation for Time.
Definition: nstime.h:1423
uint16_t port
Definition: dsdv-manet.cc:45
#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:1360
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1336
address
Definition: first.py:40
stack
Definition: first.py:37
interfaces
Definition: first.py:44
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:305
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:707
@ LOG_LEVEL_LOGIC
LOG_LOGIC and above.
Definition: log.h:113
cmd
Definition: second.py:33
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition: wifi-tcp.cc:55