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 // disabled Ipv4ArpJitter and Ipv6NsRsJitter to avoid the influence on packet dropped
413 stack.SetIpv4ArpJitter(false);
414 stack.SetIpv6NsRsJitter(false);
415 stack.Install(csmaNodesLeft.Get(0));
416 stack.Install(csmaNodesRight);
417
418 if (!m_useIpv6)
419 {
421 address.SetBase("10.1.1.0", "255.255.255.0");
422 Ipv4InterfaceContainer csmaInterfacesLeft;
423 csmaInterfacesLeft = address.Assign(csmaDevicesLeft);
424 address.SetBase("10.1.2.0", "255.255.255.0");
425 Ipv4InterfaceContainer csmaInterfacesRight;
426 csmaInterfacesRight = address.Assign(csmaDevicesRight);
427
428 // Populate ARP caches
429 NeighborCacheHelper neighborCache;
430 if (m_useChannel)
431 {
432 // Populate ARP caches for given channel
433 Ptr<Channel> csmaChannel = csmaDevicesLeft.Get(0)->GetChannel();
434 neighborCache.PopulateNeighborCache(csmaChannel);
435 }
437 {
438 // Populate ARP caches for given netDeviceContainer
439 neighborCache.PopulateNeighborCache(csmaDevicesRight);
440 }
442 {
443 std::pair<Ptr<Ipv4>, uint32_t> txInterface = csmaInterfacesLeft.Get(0);
444 Ptr<Ipv4> ipv41 = txInterface.first;
445 uint32_t index1 = txInterface.second;
446 std::pair<Ptr<Ipv4>, uint32_t> rxInterface = csmaInterfacesRight.Get(nCsmaRight);
447 Ptr<Ipv4> ipv42 = rxInterface.first;
448 uint32_t index2 = rxInterface.second;
449
450 // Populate ARP caches for given interfaceContainer
452 interfaces.Add(ipv41, index1);
453 interfaces.Add(ipv42, index2);
454 neighborCache.PopulateNeighborCache(interfaces);
455 }
456 else if (!m_noGenerate)
457 {
458 // Populate ARP caches for all devices
459 neighborCache.PopulateNeighborCache();
460 }
461
462 if (m_sendTraffic)
463 {
464 // send Packet from n0 to n1
465 uint16_t port = 9; // Discard port (RFC 863)
466 OnOffHelper onoff("ns3::UdpSocketFactory",
467 Address(InetSocketAddress(csmaInterfacesLeft.GetAddress(1), port)));
468 onoff.SetConstantRate(DataRate("10Gbps"));
469 onoff.SetAttribute("EnableSeqTsSizeHeader", BooleanValue(true));
470 ApplicationContainer apps = onoff.Install(csmaNodesLeft.Get(0));
471 apps.Start(Seconds(1.0));
472
473 // Create a packet sink to receive these packets
474 PacketSinkHelper sink("ns3::UdpSocketFactory",
475 Address(InetSocketAddress(Ipv4Address::GetAny(), port)));
476 sink.SetAttribute("EnableSeqTsSizeHeader", BooleanValue(true));
477 apps = sink.Install(csmaNodesLeft);
479 "RxWithSeqTsSize",
481 AsciiTraceHelper ascii;
482 Ptr<OutputStreamWrapper> stream = ascii.CreateFileStream("neighbor-cache-example.tr");
483 csmaLeft.EnableAsciiAll(stream);
484 csmaLeft.EnablePcapAll("neighbor-cache-example");
485 }
486 else
487 {
488 Ptr<OutputStreamWrapper> outputStream = Create<OutputStreamWrapper>(&std::cout);
489 Ipv4RoutingHelper::PrintNeighborCacheAllAt(Seconds(0), outputStream);
490 }
491 }
492 else
493 {
495 address.SetBase(Ipv6Address("2001:1::"), Ipv6Prefix(64));
496 Ipv6InterfaceContainer csmaInterfacesLeft;
497 csmaInterfacesLeft = address.Assign(csmaDevicesLeft);
498 csmaInterfacesLeft.SetForwarding(1, true);
499 csmaInterfacesLeft.SetDefaultRouteInAllNodes(1);
500
501 address.SetBase(Ipv6Address("2001:2::"), Ipv6Prefix(64));
502 Ipv6InterfaceContainer csmaInterfacesRight;
503 csmaInterfacesRight = address.Assign(csmaDevicesRight);
504 csmaInterfacesRight.SetForwarding(0, true);
505 csmaInterfacesRight.SetDefaultRouteInAllNodes(0);
506
507 // Populate neighbor NDISC caches
508 NeighborCacheHelper neighborCache;
509 if (m_useChannel)
510 {
511 // Populate NDISC caches for given channel
512 Ptr<Channel> csmaChannel = csmaDevicesLeft.Get(0)->GetChannel();
513 neighborCache.PopulateNeighborCache(csmaChannel);
514 }
516 {
517 // Populate NDISC caches for given netDeviceContainer
518 neighborCache.PopulateNeighborCache(csmaDevicesRight);
519 }
521 {
522 std::pair<Ptr<Ipv6>, uint32_t> txInterface = csmaInterfacesLeft.Get(0);
523 Ptr<Ipv6> ipv61 = txInterface.first;
524 uint32_t index1 = txInterface.second;
525 std::pair<Ptr<Ipv6>, uint32_t> rxInterface = csmaInterfacesRight.Get(nCsmaRight);
526 Ptr<Ipv6> ipv62 = rxInterface.first;
527 uint32_t index2 = rxInterface.second;
528
529 // Populate NDISC caches for given interfaceContainer
531 interfaces.Add(ipv61, index1);
532 interfaces.Add(ipv62, index2);
533 neighborCache.PopulateNeighborCache(interfaces);
534 }
535 else if (!m_noGenerate)
536 {
537 // Populate NDISC caches for all devices
538 neighborCache.PopulateNeighborCache();
539 }
540
541 if (m_sendTraffic)
542 {
543 // send Packet from n0 to n1
544 uint16_t port = 9; // Discard port (RFC 863)
545 OnOffHelper onoff(
546 "ns3::UdpSocketFactory",
547 Address(Inet6SocketAddress(csmaInterfacesLeft.GetAddress(1, 1), port)));
548 onoff.SetConstantRate(DataRate("10Gbps"));
549 onoff.SetAttribute("EnableSeqTsSizeHeader", BooleanValue(true));
550 ApplicationContainer apps = onoff.Install(csmaNodesLeft.Get(0));
551 apps.Start(Seconds(1.0));
552
553 // Create a packet sink to receive these packets
554 PacketSinkHelper sink("ns3::UdpSocketFactory",
555 Address(Inet6SocketAddress(Ipv6Address::GetAny(), port)));
556 sink.SetAttribute("EnableSeqTsSizeHeader", BooleanValue(true));
557 apps = sink.Install(csmaNodesLeft);
559 "RxWithSeqTsSize",
561 AsciiTraceHelper ascii;
562 Ptr<OutputStreamWrapper> stream = ascii.CreateFileStream("neighbor-cache-example.tr");
563 csmaLeft.EnableAsciiAll(stream);
564 csmaLeft.EnablePcapAll("neighbor-cache-example");
565 }
566 else
567 {
568 Ptr<OutputStreamWrapper> outputStream = Create<OutputStreamWrapper>(&std::cout);
569 Ipv6RoutingHelper::PrintNeighborCacheAllAt(Seconds(0), outputStream);
570 }
571 }
572 Simulator::Stop(Seconds(1.00002));
573 Simulator::Run();
574 Simulator::Destroy();
575}
Neigbor 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:92
holds a vector of ns3::Application pointers.
Ptr< Application > Get(uint32_t i) const
Get the Ptr<Application> stored in this container at a given index.
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter.
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:50
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:456
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:369
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:42
AttributeValue implementation for Time.
Definition: nstime.h:1425
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:328
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1362
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1338
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.
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:691
@ LOG_LEVEL_LOGIC
LOG_LOGIC and above.
Definition: log.h:113
void LogComponentEnable(const char *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:358
cmd
Definition: second.py:33
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition: wifi-tcp.cc:55