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 * 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
20/**
21 * This example shows how neighbor caches generate dynamically, when
22 * user is generating neighbor caches globally, neighbor caches will
23 * update dynamically when IPv4/IPv6 addresses are removed or added;
24 * when user is generating neighbor caches partially, NeighborCacheHelper
25 * will take care of address removal, for adding address user may manually
26 * add entry to keep the neighbor cache up-to-date.
27 *
28 * IPv4 Network Topology
29 * \verbatim
30 n1 n2 n3
31 | | |
32 ===========
33 LAN 10.1.1.0
34 \endverbatim
35 *
36 * IPv6 Network Topology
37 * \verbatim
38 n1 n2 n3
39 | | |
40 ===========
41 LAN 2001:1::/64
42 \endverbatim
43 *
44 * Expected Outputs:
45 * IPv4 (default):
46 * \verbatim
47 ARP Cache of node 0 at time 0
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 0
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 ARP Cache of node 2 at time 0
54 10.1.1.1 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
55 10.1.1.2 dev 0 lladdr 02-06-00:00:00:00:00:02 STATIC_AUTOGENERATED
56
57 Arp caches after add address 10.1.1.4 to n1
58 ARP Cache of node 0 at time 1
59 10.1.1.2 dev 0 lladdr 02-06-00:00:00:00:00:02 STATIC_AUTOGENERATED
60 10.1.1.3 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
61 ARP Cache of node 1 at time 1
62 10.1.1.1 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
63 10.1.1.3 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
64 10.1.1.4 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
65 ARP Cache of node 2 at time 1
66 10.1.1.1 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
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
70 Arp caches after remove the first address (10.1.1.1) from n1
71 ARP Cache of node 0 at time 2
72 10.1.1.2 dev 0 lladdr 02-06-00:00:00:00:00:02 STATIC_AUTOGENERATED
73 10.1.1.3 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
74 ARP Cache of node 1 at time 2
75 10.1.1.3 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
76 10.1.1.4 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
77 ARP Cache of node 2 at time 2
78 10.1.1.2 dev 0 lladdr 02-06-00:00:00:00:00:02 STATIC_AUTOGENERATED
79 10.1.1.4 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
80 \endverbatim
81 *
82 * IPv6 (--useIPv6):
83 * \verbatim
84 NDISC Cache of node 0 at time +0s
85 2001:1::200:ff:fe00:2 dev 0 lladdr 02-06-00:00:00:00:00:02 STATIC_AUTOGENERATED
86 2001:1::200:ff:fe00:3 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
87 fe80::200:ff:fe00:2 dev 0 lladdr 02-06-00:00:00:00:00:02 STATIC_AUTOGENERATED
88 fe80::200:ff:fe00:3 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
89 NDISC Cache of node 1 at time +0s
90 2001:1::200:ff:fe00:1 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
91 2001:1::200:ff:fe00:3 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
92 fe80::200:ff:fe00:1 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
93 fe80::200:ff:fe00:3 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
94 NDISC Cache of node 2 at time +0s
95 2001:1::200:ff:fe00:1 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
96 2001:1::200:ff:fe00:2 dev 0 lladdr 02-06-00:00:00:00:00:02 STATIC_AUTOGENERATED
97 fe80::200:ff:fe00:1 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
98 fe80::200:ff:fe00:2 dev 0 lladdr 02-06-00:00:00:00:00:02 STATIC_AUTOGENERATED
99
100 Ndisc caches after add address 2001:1::200:ff:fe00:4 n1
101 NDISC Cache of node 0 at time +1s
102 2001:1::200:ff:fe00:2 dev 0 lladdr 02-06-00:00:00:00:00:02 STATIC_AUTOGENERATED
103 2001:1::200:ff:fe00:3 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
104 fe80::200:ff:fe00:2 dev 0 lladdr 02-06-00:00:00:00:00:02 STATIC_AUTOGENERATED
105 fe80::200:ff:fe00:3 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
106 NDISC Cache of node 1 at time +1s
107 2001:1::200:ff:fe00:1 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
108 2001:1::200:ff:fe00:3 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
109 2001:1::200:ff:fe00:4 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
110 fe80::200:ff:fe00:1 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
111 fe80::200:ff:fe00:3 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
112 NDISC Cache of node 2 at time +1s
113 2001:1::200:ff:fe00:1 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
114 2001:1::200:ff:fe00:2 dev 0 lladdr 02-06-00:00:00:00:00:02 STATIC_AUTOGENERATED
115 2001:1::200:ff:fe00:4 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
116 fe80::200:ff:fe00:1 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
117 fe80::200:ff:fe00:2 dev 0 lladdr 02-06-00:00:00:00:00:02 STATIC_AUTOGENERATED
118
119 Ndisc caches after remove the second address (2001:1::200:ff:fe00:1) from n1
120 NDISC Cache of node 0 at time +2s
121 2001:1::200:ff:fe00:2 dev 0 lladdr 02-06-00:00:00:00:00:02 STATIC_AUTOGENERATED
122 2001:1::200:ff:fe00:3 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
123 fe80::200:ff:fe00:2 dev 0 lladdr 02-06-00:00:00:00:00:02 STATIC_AUTOGENERATED
124 fe80::200:ff:fe00:3 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
125 NDISC Cache of node 1 at time +2s
126 2001:1::200:ff:fe00:3 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
127 2001:1::200:ff:fe00:4 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
128 fe80::200:ff:fe00:1 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
129 fe80::200:ff:fe00:3 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
130 NDISC Cache of node 2 at time +2s
131 2001:1::200:ff:fe00:2 dev 0 lladdr 02-06-00:00:00:00:00:02 STATIC_AUTOGENERATED
132 2001:1::200:ff:fe00:4 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
133 fe80::200:ff:fe00:1 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
134 fe80::200:ff:fe00:2 dev 0 lladdr 02-06-00:00:00:00:00:02 STATIC_AUTOGENERATED
135 \endverbatim
136 */
137
138#include "ns3/core-module.h"
139#include "ns3/csma-module.h"
140#include "ns3/internet-module.h"
141#include "ns3/network-module.h"
142
143using namespace ns3;
144
145NS_LOG_COMPONENT_DEFINE("NeighborCacheDynamic");
146
147void
149{
150 ipv4Interface->AddAddress(ifaceAddr);
151 std::cout << "\nArp caches after add address 10.1.1.4 to n1" << std::endl;
152}
153
154void
156{
157 ipv6Interface->AddAddress(ifaceAddr);
158 std::cout << "\nNdisc caches after add address 2001:1::200:ff:fe00:4 n1" << std::endl;
159}
160
161void
163{
164 ipv4Interface->RemoveAddress(index);
165 std::cout << "\nArp caches after remove the first address (10.1.1.1) from n1" << std::endl;
166}
167
168void
170{
171 ipv6Interface->RemoveAddress(index);
172 std::cout << "\nNdisc caches after remove the second address (2001:1::200:ff:fe00:1) from n1"
173 << std::endl;
174}
175
176int
177main(int argc, char* argv[])
178{
179 bool useIpv6 = false;
180 bool enableLog = false;
181
182 CommandLine cmd(__FILE__);
183 cmd.AddValue("useIPv6", "Use IPv6 instead of IPv4", useIpv6);
184 cmd.AddValue("enableLog", "Enable ArpL3Protocol and Icmpv6L4Protocol logging", enableLog);
185 cmd.Parse(argc, argv);
186
187 if (enableLog)
188 {
189 LogComponentEnable("ArpL3Protocol", LOG_LEVEL_LOGIC);
190 LogComponentEnable("Icmpv6L4Protocol", LOG_LEVEL_LOGIC);
191 }
192
193 uint32_t nCsma = 3;
195 csmaNodes.Create(nCsma);
196
198 csma.SetChannelAttribute("DataRate", StringValue("100Mbps"));
199 csma.SetChannelAttribute("Delay", TimeValue(NanoSeconds(6560)));
200
202 csmaDevices = csma.Install(csmaNodes);
203
205 if (!useIpv6)
206 {
207 stack.SetIpv6StackInstall(false);
208 }
209 else
210 {
211 stack.SetIpv4StackInstall(false);
212 }
213 stack.Install(csmaNodes);
214
215 if (!useIpv6)
216 {
218 address.SetBase("10.1.1.0", "255.255.255.0");
220 csmaInterfaces = address.Assign(csmaDevices);
221 }
222 else
223 {
225 address.SetBase(Ipv6Address("2001:1::"), Ipv6Prefix(64));
227 csmaInterfaces = address.Assign(csmaDevices);
228 }
229
230 // Populate neighbor caches for all devices
231 NeighborCacheHelper neighborCache;
232 neighborCache.SetDynamicNeighborCache(true);
233 neighborCache.PopulateNeighborCache();
234
235 if (!useIpv6)
236 {
237 // Add address 10.1.1.4 to interface 1 in 0.5 seconds
238 Ptr<Node> n1 = csmaNodes.Get(0);
239 uint32_t ipv4ifIndex = 1;
240 Ptr<Ipv4Interface> ipv4Interface =
241 n1->GetObject<Ipv4L3Protocol>()->GetInterface(ipv4ifIndex);
242 Ipv4InterfaceAddress ifaceAddr = Ipv4InterfaceAddress("10.1.1.4", "255.255.255.0");
243 Simulator::Schedule(Seconds(0.5), &AddIpv4Address, ipv4Interface, ifaceAddr);
244
245 // Remove the first address (10.1.1.1) from interface 1 in 1.5 seconds
246 uint32_t addressIndex = 0;
247 Simulator::Schedule(Seconds(1.5), &RemoveIpv4Address, ipv4Interface, addressIndex);
248
249 Ptr<OutputStreamWrapper> outputStream = Create<OutputStreamWrapper>(&std::cout);
253 }
254 else
255 {
256 // Add address 2001:1::200:ff:fe00:4 to interface 1 in 0.5 seconds
257 Ptr<Node> n1 = csmaNodes.Get(0);
258 uint32_t ipv6ifIndex = 1;
259 Ptr<Ipv6Interface> ipv6Interface =
260 n1->GetObject<Ipv6L3Protocol>()->GetInterface(ipv6ifIndex);
261 Ipv6InterfaceAddress ifaceAddr =
262 Ipv6InterfaceAddress("2001:1::200:ff:fe00:4", Ipv6Prefix(64));
263 Simulator::Schedule(Seconds(0.5), &AddIpv6Address, ipv6Interface, ifaceAddr);
264
265 // Remove the second address (2001:1::200:ff:fe00:1) from interface 1 in 1.5 seconds
266 uint32_t addressIndex = 1;
267 Simulator::Schedule(Seconds(1.5), &RemoveIpv6Address, ipv6Interface, addressIndex);
268
269 Ptr<OutputStreamWrapper> outputStream = Create<OutputStreamWrapper>(&std::cout);
273 }
274
278 return 0;
279}
Parse command-line arguments.
Definition: command-line.h:232
build a set of CsmaNetDevice objects
Definition: csma-helper.h:48
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.
Definition: ipv6-address.h:49
IPv6 address associated with an interface.
Keep track of a set of IPv6 interfaces.
IPv6 layer implementation.
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.
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.
Definition: ptr.h:77
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
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
AttributeValue implementation for Time.
Definition: nstime.h:1406
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1355
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
ns address
Definition: first.py:47
ns stack
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:302
@ LOG_LEVEL_LOGIC
LOG_LOGIC and above.
Definition: log.h:110
ns cmd
Definition: second.py:40
ns csmaInterfaces
Definition: second.py:78
ns csmaNodes
Definition: second.py:53
ns csma
Definition: second.py:63
ns csmaDevices
Definition: second.py:67
c_int nCsma
Definition: second.py:38
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)