A Discrete-Event Network Simulator
API
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 
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 
143 using namespace ns3;
144 
145 NS_LOG_COMPONENT_DEFINE("NeighborCacheDynamic");
146 
147 void
149 {
150  ipv4Interface->AddAddress(ifaceAddr);
151  std::cout << "\nArp caches after add address 10.1.1.4 to n1" << std::endl;
152 }
153 
154 void
156 {
157  ipv6Interface->AddAddress(ifaceAddr);
158  std::cout << "\nNdisc caches after add address 2001:1::200:ff:fe00:4 n1" << std::endl;
159 }
160 
161 void
162 RemoveIpv4Address(Ptr<Ipv4Interface> ipv4Interface, uint32_t index)
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 
168 void
169 RemoveIpv6Address(Ptr<Ipv6Interface> ipv6Interface, uint32_t index)
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 
176 int
177 main(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");
221  }
222  else
223  {
225  address.SetBase(Ipv6Address("2001:1::"), Ipv6Prefix(64));
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 
275  Simulator::Stop(Seconds(10.0));
276  Simulator::Run();
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.
bool AddAddress(Ipv4InterfaceAddress address)
Ipv4InterfaceAddress RemoveAddress(uint32_t 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.
Ipv6InterfaceAddress RemoveAddress(uint32_t index)
Remove an address from interface.
bool AddAddress(Ipv6InterfaceAddress iface)
Add an IPv6 address.
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.
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:471
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
#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:1362
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
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
csmaInterfaces
Definition: second.py:78
csmaNodes
Definition: second.py:53
csma
Definition: second.py:63
nCsma
Definition: second.py:38
cmd
Definition: second.py:40
csmaDevices
Definition: second.py:67
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)