A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
neighbor-cache-dynamic.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2022 ZHIHENG DONG
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Zhiheng Dong <dzh2077@gmail.com>
7 */
8
9/**
10 * This example shows how neighbor caches generate dynamically, when
11 * user is generating neighbor caches globally, neighbor caches will
12 * update dynamically when IPv4/IPv6 addresses are removed or added;
13 * when user is generating neighbor caches partially, NeighborCacheHelper
14 * will take care of address removal, for adding address user may manually
15 * add entry to keep the neighbor cache up-to-date.
16 *
17 * IPv4 Network Topology
18 * \verbatim
19 n1 n2 n3
20 | | |
21 ===========
22 LAN 10.1.1.0
23 \endverbatim
24 *
25 * IPv6 Network Topology
26 * \verbatim
27 n1 n2 n3
28 | | |
29 ===========
30 LAN 2001:1::/64
31 \endverbatim
32 *
33 * Expected Outputs:
34 * IPv4 (default):
35 * \verbatim
36 ARP Cache of node 0 at time 0
37 10.1.1.2 dev 0 lladdr 02-06-00:00:00:00:00:02 STATIC_AUTOGENERATED
38 10.1.1.3 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
39 ARP Cache of node 1 at time 0
40 10.1.1.1 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
41 10.1.1.3 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
42 ARP Cache of node 2 at time 0
43 10.1.1.1 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
44 10.1.1.2 dev 0 lladdr 02-06-00:00:00:00:00:02 STATIC_AUTOGENERATED
45
46 Arp caches after add address 10.1.1.4 to n1
47 ARP Cache of node 0 at time 1
48 10.1.1.2 dev 0 lladdr 02-06-00:00:00:00:00:02 STATIC_AUTOGENERATED
49 10.1.1.3 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
50 ARP Cache of node 1 at time 1
51 10.1.1.1 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
52 10.1.1.3 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
53 10.1.1.4 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
54 ARP Cache of node 2 at time 1
55 10.1.1.1 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
56 10.1.1.2 dev 0 lladdr 02-06-00:00:00:00:00:02 STATIC_AUTOGENERATED
57 10.1.1.4 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
58
59 Arp caches after remove the first address (10.1.1.1) from n1
60 ARP Cache of node 0 at time 2
61 10.1.1.2 dev 0 lladdr 02-06-00:00:00:00:00:02 STATIC_AUTOGENERATED
62 10.1.1.3 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
63 ARP Cache of node 1 at time 2
64 10.1.1.3 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
65 10.1.1.4 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
66 ARP Cache of node 2 at time 2
67 10.1.1.2 dev 0 lladdr 02-06-00:00:00:00:00:02 STATIC_AUTOGENERATED
68 10.1.1.4 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
69 \endverbatim
70 *
71 * IPv6 (--useIPv6):
72 * \verbatim
73 NDISC Cache of node 0 at time +0s
74 2001:1::200:ff:fe00:2 dev 0 lladdr 02-06-00:00:00:00:00:02 STATIC_AUTOGENERATED
75 2001:1::200:ff:fe00:3 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
76 fe80::200:ff:fe00:2 dev 0 lladdr 02-06-00:00:00:00:00:02 STATIC_AUTOGENERATED
77 fe80::200:ff:fe00:3 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
78 NDISC Cache of node 1 at time +0s
79 2001:1::200:ff:fe00:1 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
80 2001:1::200:ff:fe00:3 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
81 fe80::200:ff:fe00:1 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
82 fe80::200:ff:fe00:3 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
83 NDISC Cache of node 2 at time +0s
84 2001:1::200:ff:fe00:1 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
85 2001:1::200:ff:fe00:2 dev 0 lladdr 02-06-00:00:00:00:00:02 STATIC_AUTOGENERATED
86 fe80::200:ff:fe00:1 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
87 fe80::200:ff:fe00:2 dev 0 lladdr 02-06-00:00:00:00:00:02 STATIC_AUTOGENERATED
88
89 Ndisc caches after add address 2001:1::200:ff:fe00:4 n1
90 NDISC Cache of node 0 at time +1s
91 2001:1::200:ff:fe00:2 dev 0 lladdr 02-06-00:00:00:00:00:02 STATIC_AUTOGENERATED
92 2001:1::200:ff:fe00:3 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
93 fe80::200:ff:fe00:2 dev 0 lladdr 02-06-00:00:00:00:00:02 STATIC_AUTOGENERATED
94 fe80::200:ff:fe00:3 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
95 NDISC Cache of node 1 at time +1s
96 2001:1::200:ff:fe00:1 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
97 2001:1::200:ff:fe00:3 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
98 2001:1::200:ff:fe00:4 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
99 fe80::200:ff:fe00:1 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
100 fe80::200:ff:fe00:3 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
101 NDISC Cache of node 2 at time +1s
102 2001:1::200:ff:fe00:1 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
103 2001:1::200:ff:fe00:2 dev 0 lladdr 02-06-00:00:00:00:00:02 STATIC_AUTOGENERATED
104 2001:1::200:ff:fe00:4 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
105 fe80::200:ff:fe00:1 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
106 fe80::200:ff:fe00:2 dev 0 lladdr 02-06-00:00:00:00:00:02 STATIC_AUTOGENERATED
107
108 Ndisc caches after remove the second address (2001:1::200:ff:fe00:1) from n1
109 NDISC Cache of node 0 at time +2s
110 2001:1::200:ff:fe00:2 dev 0 lladdr 02-06-00:00:00:00:00:02 STATIC_AUTOGENERATED
111 2001:1::200:ff:fe00:3 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
112 fe80::200:ff:fe00:2 dev 0 lladdr 02-06-00:00:00:00:00:02 STATIC_AUTOGENERATED
113 fe80::200:ff:fe00:3 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
114 NDISC Cache of node 1 at time +2s
115 2001:1::200:ff:fe00:3 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
116 2001:1::200:ff:fe00:4 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
117 fe80::200:ff:fe00:1 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
118 fe80::200:ff:fe00:3 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
119 NDISC Cache of node 2 at time +2s
120 2001:1::200:ff:fe00:2 dev 0 lladdr 02-06-00:00:00:00:00:02 STATIC_AUTOGENERATED
121 2001:1::200:ff:fe00:4 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
122 fe80::200:ff:fe00:1 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
123 fe80::200:ff:fe00:2 dev 0 lladdr 02-06-00:00:00:00:00:02 STATIC_AUTOGENERATED
124 \endverbatim
125 */
126
127#include "ns3/core-module.h"
128#include "ns3/csma-module.h"
129#include "ns3/internet-module.h"
130#include "ns3/network-module.h"
131
132using namespace ns3;
133
134NS_LOG_COMPONENT_DEFINE("NeighborCacheDynamic");
135
136void
138{
139 ipv4Interface->AddAddress(ifaceAddr);
140 std::cout << "\nArp caches after add address 10.1.1.4 to n1" << std::endl;
141}
142
143void
145{
146 ipv6Interface->AddAddress(ifaceAddr);
147 std::cout << "\nNdisc caches after add address 2001:1::200:ff:fe00:4 n1" << std::endl;
148}
149
150void
152{
153 ipv4Interface->RemoveAddress(index);
154 std::cout << "\nArp caches after remove the first address (10.1.1.1) from n1" << std::endl;
155}
156
157void
159{
160 ipv6Interface->RemoveAddress(index);
161 std::cout << "\nNdisc caches after remove the second address (2001:1::200:ff:fe00:1) from n1"
162 << std::endl;
163}
164
165int
166main(int argc, char* argv[])
167{
168 bool useIpv6 = false;
169 bool enableLog = false;
170
171 CommandLine cmd(__FILE__);
172 cmd.AddValue("useIPv6", "Use IPv6 instead of IPv4", useIpv6);
173 cmd.AddValue("enableLog", "Enable ArpL3Protocol and Icmpv6L4Protocol logging", enableLog);
174 cmd.Parse(argc, argv);
175
176 if (enableLog)
177 {
178 LogComponentEnable("ArpL3Protocol", LOG_LEVEL_LOGIC);
179 LogComponentEnable("Icmpv6L4Protocol", LOG_LEVEL_LOGIC);
180 }
181
182 uint32_t nCsma = 3;
184 csmaNodes.Create(nCsma);
185
187 csma.SetChannelAttribute("DataRate", StringValue("100Mbps"));
188 csma.SetChannelAttribute("Delay", TimeValue(NanoSeconds(6560)));
189
191 csmaDevices = csma.Install(csmaNodes);
192
194 if (!useIpv6)
195 {
196 stack.SetIpv6StackInstall(false);
197 }
198 else
199 {
200 stack.SetIpv4StackInstall(false);
201 }
202 stack.Install(csmaNodes);
203
204 if (!useIpv6)
205 {
207 address.SetBase("10.1.1.0", "255.255.255.0");
209 csmaInterfaces = address.Assign(csmaDevices);
210 }
211 else
212 {
214 address.SetBase(Ipv6Address("2001:1::"), Ipv6Prefix(64));
216 csmaInterfaces = address.Assign(csmaDevices);
217 }
218
219 // Populate neighbor caches for all devices
220 NeighborCacheHelper neighborCache;
221 neighborCache.SetDynamicNeighborCache(true);
222 neighborCache.PopulateNeighborCache();
223
224 if (!useIpv6)
225 {
226 // Add address 10.1.1.4 to interface 1 in 0.5 seconds
227 Ptr<Node> n1 = csmaNodes.Get(0);
228 uint32_t ipv4ifIndex = 1;
229 Ptr<Ipv4Interface> ipv4Interface =
230 n1->GetObject<Ipv4L3Protocol>()->GetInterface(ipv4ifIndex);
231 Ipv4InterfaceAddress ifaceAddr = Ipv4InterfaceAddress("10.1.1.4", "255.255.255.0");
232 Simulator::Schedule(Seconds(0.5), &AddIpv4Address, ipv4Interface, ifaceAddr);
233
234 // Remove the first address (10.1.1.1) from interface 1 in 1.5 seconds
235 uint32_t addressIndex = 0;
236 Simulator::Schedule(Seconds(1.5), &RemoveIpv4Address, ipv4Interface, addressIndex);
237
238 Ptr<OutputStreamWrapper> outputStream = Create<OutputStreamWrapper>(&std::cout);
242 }
243 else
244 {
245 // Add address 2001:1::200:ff:fe00:4 to interface 1 in 0.5 seconds
246 Ptr<Node> n1 = csmaNodes.Get(0);
247 uint32_t ipv6ifIndex = 1;
248 Ptr<Ipv6Interface> ipv6Interface =
249 n1->GetObject<Ipv6L3Protocol>()->GetInterface(ipv6ifIndex);
250 Ipv6InterfaceAddress ifaceAddr =
251 Ipv6InterfaceAddress("2001:1::200:ff:fe00:4", Ipv6Prefix(64));
252 Simulator::Schedule(Seconds(0.5), &AddIpv6Address, ipv6Interface, ifaceAddr);
253
254 // Remove the second address (2001:1::200:ff:fe00:1) from interface 1 in 1.5 seconds
255 uint32_t addressIndex = 1;
256 Simulator::Schedule(Seconds(1.5), &RemoveIpv6Address, ipv6Interface, addressIndex);
257
258 Ptr<OutputStreamWrapper> outputStream = Create<OutputStreamWrapper>(&std::cout);
262 }
263
267 return 0;
268}
Parse command-line arguments.
build a set of CsmaNetDevice objects
Definition csma-helper.h:37
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
a class to store IPv4 address information on an interface
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Implement the IPv4 layer.
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.
IPv6 address associated with an interface.
Keep track of a set of IPv6 interfaces.
IPv6 layer implementation.
Describes an IPv6 prefix.
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.
void SetDynamicNeighborCache(bool enable)
Enable/disable dynamic neighbor cache, auto-generated neighbor cache will update by IP addresses chan...
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
Smart pointer class similar to boost::intrusive_ptr.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
Hold variables of type string.
Definition string.h:45
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1344
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
address
Definition first.py:36
stack
Definition first.py:33
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:291
@ LOG_LEVEL_LOGIC
LOG_LOGIC and above.
Definition log.h:99
csmaInterfaces
Definition second.py:67
csmaNodes
Definition second.py:42
nCsma
Definition second.py:27
csmaDevices
Definition second.py:56
void RemoveIpv6Address(Ptr< Ipv6Interface > ipv6Interface, uint32_t index)
void AddIpv6Address(Ptr< Ipv6Interface > ipv6Interface, Ipv6InterfaceAddress ifaceAddr)
void RemoveIpv4Address(Ptr< Ipv4Interface > ipv4Interface, uint32_t index)
void AddIpv4Address(Ptr< Ipv4Interface > ipv4Interface, Ipv4InterfaceAddress ifaceAddr)