A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
neighbor-cache-test.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#include "ns3/icmpv4-l4-protocol.h"
10#include "ns3/icmpv6-l4-protocol.h"
11#include "ns3/internet-stack-helper.h"
12#include "ns3/ipv4-address-helper.h"
13#include "ns3/ipv4-l3-protocol.h"
14#include "ns3/ipv4-routing-helper.h"
15#include "ns3/ipv6-address-helper.h"
16#include "ns3/ipv6-l3-protocol.h"
17#include "ns3/ipv6-routing-helper.h"
18#include "ns3/neighbor-cache-helper.h"
19#include "ns3/simple-channel.h"
20#include "ns3/simple-net-device-helper.h"
21#include "ns3/simple-net-device.h"
22#include "ns3/simulator.h"
23#include "ns3/socket-factory.h"
24#include "ns3/socket.h"
25#include "ns3/test.h"
26#include "ns3/udp-l4-protocol.h"
27#include "ns3/udp-socket-factory.h"
28
29using namespace ns3;
30
31/**
32 * @ingroup internet-test
33 *
34 * @brief Dynamic Neighbor Cache Test
35 */
37{
38 Ptr<Packet> m_receivedPacket; //!< Received packet
39
40 /**
41 * @brief Send data immediately after being called.
42 * @param socket The sending socket.
43 * @param to IPv4 Destination address.
44 */
45 void DoSendDatav4(Ptr<Socket> socket, Ipv4Address to);
46
47 /**
48 * @brief Send data immediately after being called.
49 * @param socket The sending socket.
50 * @param to IPv6 Destination address.
51 */
52 void DoSendDatav6(Ptr<Socket> socket, Ipv6Address to);
53
54 /**
55 * @brief Schedules the DoSendData () function to send the data.
56 * @param socket The sending socket.
57 * @param to IPv4 Destination address.
58 */
59 void SendData(Ptr<Socket> socket, Ipv4Address to);
60
61 /**
62 * @brief Schedules the DoSendData () function to send the data.
63 * @param socket The sending socket.
64 * @param to IPv6 Destination address.
65 */
66 void SendData(Ptr<Socket> socket, Ipv6Address to);
67
68 /**
69 * @brief Add an IPv4 address to an IPv4 interface
70 * @param ipv4Interface The interface that address will be added.
71 * @param ifaceAddr The added IPv4 address.
72 */
73 void AddIpv4Address(Ptr<Ipv4Interface> ipv4Interface, Ipv4InterfaceAddress ifaceAddr);
74
75 /**
76 * @brief Add an IPv6 address to an IPv6 interface
77 * @param ipv6Interface The interface that address will be added.
78 * @param ifaceAddr The added IPv6 address.
79 */
80 void AddIpv6Address(Ptr<Ipv6Interface> ipv6Interface, Ipv6InterfaceAddress ifaceAddr);
81
82 /**
83 * @brief Remove an IPv4 address from an IPv4 interface
84 * @param ipv4Interface The interface that address will be removed from.
85 * @param index The index of IPv4 address that will be removed.
86 */
87 void RemoveIpv4Address(Ptr<Ipv4Interface> ipv4Interface, uint32_t index);
88
89 /**
90 * @brief Remove an IPv6 address from an IPv6 interface
91 * @param ipv6Interface The interface that address will be removed from.
92 * @param index The index of IPv6 address that will be removed.
93 */
94 void RemoveIpv6Address(Ptr<Ipv6Interface> ipv6Interface, uint32_t index);
95
96 public:
97 void DoRun() override;
98
100
101 /**
102 * @brief Receive data.
103 * @param socket The receiving socket.
104 */
105 void ReceivePkt(Ptr<Socket> socket);
106
107 std::vector<uint32_t> m_receivedPacketSizes; //!< Received packet sizes
108};
109
111 : TestCase(
112 "The DynamicNeighborCacheTestPopulate checks if neighbor caches are correctly populated "
113 "in global scope and updated when there is an IP address added or removed.")
114{
115}
116
117void
119{
120 uint32_t availableData [[maybe_unused]] = socket->GetRxAvailable();
121 m_receivedPacket = socket->Recv(std::numeric_limits<uint32_t>::max(), 0);
122 NS_TEST_ASSERT_MSG_EQ(availableData,
124 "Received Packet size is not equal to the Rx buffer size");
126}
127
128void
130{
131 Address realTo = InetSocketAddress(to, 1234);
132 NS_TEST_EXPECT_MSG_EQ(socket->SendTo(Create<Packet>(123), 0, realTo), 123, "100");
133}
134
135void
137{
138 Address realTo = Inet6SocketAddress(to, 1234);
139 NS_TEST_EXPECT_MSG_EQ(socket->SendTo(Create<Packet>(123), 0, realTo), 123, "100");
140}
141
142void
144{
146 Simulator::ScheduleWithContext(socket->GetNode()->GetId(),
147 Seconds(60),
149 this,
150 socket,
151 to);
152}
153
154void
156{
158 Simulator::ScheduleWithContext(socket->GetNode()->GetId(),
159 Seconds(60),
161 this,
162 socket,
163 to);
164}
165
166void
168 Ipv4InterfaceAddress ifaceAddr)
169{
170 ipv4Interface->AddAddress(ifaceAddr);
171}
172
173void
175 Ipv6InterfaceAddress ifaceAddr)
176{
177 ipv6Interface->AddAddress(ifaceAddr);
178}
179
180void
182{
183 ipv4Interface->RemoveAddress(index);
184}
185
186void
188{
189 ipv6Interface->RemoveAddress(index);
190}
191
192void
194{
195 Ptr<Node> tx1Node = CreateObject<Node>();
196 Ptr<Node> tx2Node = CreateObject<Node>();
197 Ptr<Node> rxNode = CreateObject<Node>();
198 Ptr<Node> snifferNode = CreateObject<Node>();
199
200 NodeContainer all(tx1Node, tx2Node, rxNode, snifferNode);
201
202 std::ostringstream stringStream1v4;
203 Ptr<OutputStreamWrapper> arpStream = Create<OutputStreamWrapper>(&stringStream1v4);
204 std::ostringstream stringStream1v6;
205 Ptr<OutputStreamWrapper> ndiscStream = Create<OutputStreamWrapper>(&stringStream1v6);
206
207 InternetStackHelper internetNodes;
208 internetNodes.Install(all);
209
211 // Sender Node
213 {
215 tx1Dev->SetAddress(Mac48Address("00:00:00:00:00:01"));
216 tx1Node->AddDevice(tx1Dev);
217 }
218 net.Add(tx1Dev);
219
221 {
223 tx2Dev->SetAddress(Mac48Address("00:00:00:00:00:02"));
224 tx2Node->AddDevice(tx2Dev);
225 }
226 net.Add(tx2Dev);
227
228 // Receive node
230 {
232 rxDev->SetAddress(Mac48Address("00:00:00:00:00:03"));
233 rxNode->AddDevice(rxDev);
234 }
235 net.Add(rxDev);
236
237 // Sniffer node
238 Ptr<SimpleNetDevice> snifferDev;
239 {
240 snifferDev = CreateObject<SimpleNetDevice>();
241 snifferDev->SetAddress(Mac48Address("00:00:00:00:00:04"));
242 snifferNode->AddDevice(snifferDev);
243 }
244 net.Add(snifferDev);
245
246 // link the channels
248 tx1Dev->SetChannel(channel);
249 tx2Dev->SetChannel(channel);
250 rxDev->SetChannel(channel);
251 snifferDev->SetChannel(channel);
252
253 // Setup IPv4 addresses
255 ipv4.SetBase(Ipv4Address("10.0.1.0"), Ipv4Mask("255.255.255.0"));
256 Ipv4InterfaceContainer icv4 = ipv4.Assign(net);
257
258 // Add address 10.1.1.5 to rxNode in 0.5 seconds
259 Ptr<Node> n1 = rxNode;
260 uint32_t ipv4ifIndex = 1;
261 Ptr<Ipv4Interface> ipv4Interface = n1->GetObject<Ipv4L3Protocol>()->GetInterface(ipv4ifIndex);
262 Ipv4InterfaceAddress ifaceAddr = Ipv4InterfaceAddress("10.0.1.5", "255.255.255.0");
265 this,
266 ipv4Interface,
267 ifaceAddr);
268
269 // Remove the first address (10.1.1.3) from rxNode in 1.5 seconds
270 uint32_t addressIndex = 0;
273 this,
274 ipv4Interface,
275 addressIndex);
276
277 // Setup IPv6 addresses
279 ipv6.SetBase(Ipv6Address("2001:0::"), Ipv6Prefix(64));
280 Ipv6InterfaceContainer icv6 = ipv6.Assign(net);
281
282 // Add address 2001:1::200:ff:fe00:5 to rxNode in 0.5 seconds
283 uint32_t ipv6ifIndex = 1;
284 Ptr<Ipv6Interface> ipv6Interface = n1->GetObject<Ipv6L3Protocol>()->GetInterface(ipv6ifIndex);
285 Ipv6InterfaceAddress ifaceAddrv6 =
286 Ipv6InterfaceAddress("2001:0::200:ff:fe00:5", Ipv6Prefix(64));
289 this,
290 ipv6Interface,
291 ifaceAddrv6);
292
293 // Remove the second address (2001:1::200:ff:fe00:3) from rxNode in 1.5 seconds
294 addressIndex = 1;
297 this,
298 ipv6Interface,
299 addressIndex);
300
301 // Populate neighbor caches.
302 NeighborCacheHelper neighborCache;
303 neighborCache.SetDynamicNeighborCache(true);
304 neighborCache.PopulateNeighborCache();
305
306 // Print cache.
313
314 // Create the UDP sockets
315 Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<UdpSocketFactory>();
316 Ptr<Socket> rxSocketv4 = rxSocketFactory->CreateSocket();
317 Ptr<Socket> rxSocketv6 = rxSocketFactory->CreateSocket();
318 NS_TEST_EXPECT_MSG_EQ(rxSocketv4->Bind(InetSocketAddress(Ipv4Address("10.0.1.5"), 1234)),
319 0,
320 "trivial");
322 rxSocketv6->Bind(Inet6SocketAddress(Ipv6Address("2001:0::200:ff:fe00:5"), 1234)),
323 0,
324 "trivial");
325 rxSocketv4->SetRecvCallback(MakeCallback(&DynamicNeighborCacheTest::ReceivePkt, this));
326 rxSocketv6->SetRecvCallback(MakeCallback(&DynamicNeighborCacheTest::ReceivePkt, this));
327
328 Ptr<SocketFactory> snifferSocketFactory = snifferNode->GetObject<UdpSocketFactory>();
329 Ptr<Socket> snifferSocketv4 = snifferSocketFactory->CreateSocket();
330 Ptr<Socket> snifferSocketv6 = snifferSocketFactory->CreateSocket();
331 NS_TEST_EXPECT_MSG_EQ(snifferSocketv4->Bind(InetSocketAddress(Ipv4Address("10.0.1.4"), 1234)),
332 0,
333 "trivial");
335 snifferSocketv6->Bind(Inet6SocketAddress(Ipv6Address("2001:0::200:ff:fe00:4"), 1234)),
336 0,
337 "trivial");
338 snifferSocketv4->SetRecvCallback(MakeCallback(&DynamicNeighborCacheTest::ReceivePkt, this));
339 snifferSocketv6->SetRecvCallback(MakeCallback(&DynamicNeighborCacheTest::ReceivePkt, this));
340
341 Ptr<SocketFactory> tx1SocketFactory = tx1Node->GetObject<UdpSocketFactory>();
342 Ptr<Socket> tx1Socket = tx1SocketFactory->CreateSocket();
343 tx1Socket->SetAllowBroadcast(true);
344
345 Ptr<SocketFactory> tx2SocketFactory = tx2Node->GetObject<UdpSocketFactory>();
346 Ptr<Socket> tx2Socket = tx2SocketFactory->CreateSocket();
347 tx2Socket->SetAllowBroadcast(true);
348
349 // ------ Now the tests ------------
350
351 // Unicast test
352
353 // send data to the added address
354 SendData(tx1Socket, Ipv4Address("10.0.1.5"));
355 SendData(tx1Socket, Ipv6Address("2001:0::200:ff:fe00:5"));
356
357 // send data to the removed address
358 SendData(tx1Socket, Ipv4Address("10.0.1.3"));
359 SendData(tx1Socket, Ipv6Address("2001:0::200:ff:fe00:3"));
360
363
364 // Check if all packet are correctly received.
366 123,
367 "Should receive packet sending to the added IPv4 address.");
369 123,
370 "Should receive packet sending to the added IPv6 address.");
373 2,
374 "Should receive only 1 packet from IPv4 interface and only 1 packet from IPv6 interface.");
375
376 // Check if the arp caches are populated correctly at time 0,
377 // Check if the arp caches are updated correctly at time 1 after new IP address is added,
378 // Check if the arp caches are updated correctly at time 2 after an IP address is removed.
379 constexpr auto arpCache =
380 "ARP Cache of node 0 at time 0\n"
381 "10.0.1.2 dev 1 lladdr 04-06-00:00:00:00:00:02 STATIC_AUTOGENERATED\n"
382 "10.0.1.3 dev 1 lladdr 04-06-00:00:00:00:00:03 STATIC_AUTOGENERATED\n"
383 "10.0.1.4 dev 1 lladdr 04-06-00:00:00:00:00:04 STATIC_AUTOGENERATED\n"
384 "ARP Cache of node 1 at time 0\n"
385 "10.0.1.1 dev 1 lladdr 04-06-00:00:00:00:00:01 STATIC_AUTOGENERATED\n"
386 "10.0.1.3 dev 1 lladdr 04-06-00:00:00:00:00:03 STATIC_AUTOGENERATED\n"
387 "10.0.1.4 dev 1 lladdr 04-06-00:00:00:00:00:04 STATIC_AUTOGENERATED\n"
388 "ARP Cache of node 2 at time 0\n"
389 "10.0.1.1 dev 1 lladdr 04-06-00:00:00:00:00:01 STATIC_AUTOGENERATED\n"
390 "10.0.1.2 dev 1 lladdr 04-06-00:00:00:00:00:02 STATIC_AUTOGENERATED\n"
391 "10.0.1.4 dev 1 lladdr 04-06-00:00:00:00:00:04 STATIC_AUTOGENERATED\n"
392 "ARP Cache of node 3 at time 0\n"
393 "10.0.1.1 dev 1 lladdr 04-06-00:00:00:00:00:01 STATIC_AUTOGENERATED\n"
394 "10.0.1.2 dev 1 lladdr 04-06-00:00:00:00:00:02 STATIC_AUTOGENERATED\n"
395 "10.0.1.3 dev 1 lladdr 04-06-00:00:00:00:00:03 STATIC_AUTOGENERATED\n"
396 "ARP Cache of node 0 at time 1\n"
397 "10.0.1.2 dev 1 lladdr 04-06-00:00:00:00:00:02 STATIC_AUTOGENERATED\n"
398 "10.0.1.3 dev 1 lladdr 04-06-00:00:00:00:00:03 STATIC_AUTOGENERATED\n"
399 "10.0.1.4 dev 1 lladdr 04-06-00:00:00:00:00:04 STATIC_AUTOGENERATED\n"
400 "10.0.1.5 dev 1 lladdr 04-06-00:00:00:00:00:03 STATIC_AUTOGENERATED\n"
401 "ARP Cache of node 1 at time 1\n"
402 "10.0.1.1 dev 1 lladdr 04-06-00:00:00:00:00:01 STATIC_AUTOGENERATED\n"
403 "10.0.1.3 dev 1 lladdr 04-06-00:00:00:00:00:03 STATIC_AUTOGENERATED\n"
404 "10.0.1.4 dev 1 lladdr 04-06-00:00:00:00:00:04 STATIC_AUTOGENERATED\n"
405 "10.0.1.5 dev 1 lladdr 04-06-00:00:00:00:00:03 STATIC_AUTOGENERATED\n"
406 "ARP Cache of node 2 at time 1\n"
407 "10.0.1.1 dev 1 lladdr 04-06-00:00:00:00:00:01 STATIC_AUTOGENERATED\n"
408 "10.0.1.2 dev 1 lladdr 04-06-00:00:00:00:00:02 STATIC_AUTOGENERATED\n"
409 "10.0.1.4 dev 1 lladdr 04-06-00:00:00:00:00:04 STATIC_AUTOGENERATED\n"
410 "ARP Cache of node 3 at time 1\n"
411 "10.0.1.1 dev 1 lladdr 04-06-00:00:00:00:00:01 STATIC_AUTOGENERATED\n"
412 "10.0.1.2 dev 1 lladdr 04-06-00:00:00:00:00:02 STATIC_AUTOGENERATED\n"
413 "10.0.1.3 dev 1 lladdr 04-06-00:00:00:00:00:03 STATIC_AUTOGENERATED\n"
414 "10.0.1.5 dev 1 lladdr 04-06-00:00:00:00:00:03 STATIC_AUTOGENERATED\n"
415 "ARP Cache of node 0 at time 2\n"
416 "10.0.1.2 dev 1 lladdr 04-06-00:00:00:00:00:02 STATIC_AUTOGENERATED\n"
417 "10.0.1.4 dev 1 lladdr 04-06-00:00:00:00:00:04 STATIC_AUTOGENERATED\n"
418 "10.0.1.5 dev 1 lladdr 04-06-00:00:00:00:00:03 STATIC_AUTOGENERATED\n"
419 "ARP Cache of node 1 at time 2\n"
420 "10.0.1.1 dev 1 lladdr 04-06-00:00:00:00:00:01 STATIC_AUTOGENERATED\n"
421 "10.0.1.4 dev 1 lladdr 04-06-00:00:00:00:00:04 STATIC_AUTOGENERATED\n"
422 "10.0.1.5 dev 1 lladdr 04-06-00:00:00:00:00:03 STATIC_AUTOGENERATED\n"
423 "ARP Cache of node 2 at time 2\n"
424 "10.0.1.1 dev 1 lladdr 04-06-00:00:00:00:00:01 STATIC_AUTOGENERATED\n"
425 "10.0.1.2 dev 1 lladdr 04-06-00:00:00:00:00:02 STATIC_AUTOGENERATED\n"
426 "10.0.1.4 dev 1 lladdr 04-06-00:00:00:00:00:04 STATIC_AUTOGENERATED\n"
427 "ARP Cache of node 3 at time 2\n"
428 "10.0.1.1 dev 1 lladdr 04-06-00:00:00:00:00:01 STATIC_AUTOGENERATED\n"
429 "10.0.1.2 dev 1 lladdr 04-06-00:00:00:00:00:02 STATIC_AUTOGENERATED\n"
430 "10.0.1.5 dev 1 lladdr 04-06-00:00:00:00:00:03 STATIC_AUTOGENERATED\n";
431 NS_TEST_EXPECT_MSG_EQ(stringStream1v4.str(), arpCache, "Arp cache is incorrect.");
432
433 // Check if the ndisc caches are populated correctly at time 0,
434 // Check if the ndisc caches are updated correctly at time 1 after new IP address is added,
435 // Check if the ndisc caches are updated correctly at time 2 after an IP address is removed.
436 constexpr auto NdiscCache =
437 "NDISC Cache of node 0 at time +0s\n"
438 "2001::200:ff:fe00:2 dev 1 lladdr 04-06-00:00:00:00:00:02 STATIC_AUTOGENERATED\n"
439 "2001::200:ff:fe00:3 dev 1 lladdr 04-06-00:00:00:00:00:03 STATIC_AUTOGENERATED\n"
440 "2001::200:ff:fe00:4 dev 1 lladdr 04-06-00:00:00:00:00:04 STATIC_AUTOGENERATED\n"
441 "fe80::200:ff:fe00:2 dev 1 lladdr 04-06-00:00:00:00:00:02 STATIC_AUTOGENERATED\n"
442 "fe80::200:ff:fe00:3 dev 1 lladdr 04-06-00:00:00:00:00:03 STATIC_AUTOGENERATED\n"
443 "fe80::200:ff:fe00:4 dev 1 lladdr 04-06-00:00:00:00:00:04 STATIC_AUTOGENERATED\n"
444 "NDISC Cache of node 1 at time +0s\n"
445 "2001::200:ff:fe00:1 dev 1 lladdr 04-06-00:00:00:00:00:01 STATIC_AUTOGENERATED\n"
446 "2001::200:ff:fe00:3 dev 1 lladdr 04-06-00:00:00:00:00:03 STATIC_AUTOGENERATED\n"
447 "2001::200:ff:fe00:4 dev 1 lladdr 04-06-00:00:00:00:00:04 STATIC_AUTOGENERATED\n"
448 "fe80::200:ff:fe00:1 dev 1 lladdr 04-06-00:00:00:00:00:01 STATIC_AUTOGENERATED\n"
449 "fe80::200:ff:fe00:3 dev 1 lladdr 04-06-00:00:00:00:00:03 STATIC_AUTOGENERATED\n"
450 "fe80::200:ff:fe00:4 dev 1 lladdr 04-06-00:00:00:00:00:04 STATIC_AUTOGENERATED\n"
451 "NDISC Cache of node 2 at time +0s\n"
452 "2001::200:ff:fe00:1 dev 1 lladdr 04-06-00:00:00:00:00:01 STATIC_AUTOGENERATED\n"
453 "2001::200:ff:fe00:2 dev 1 lladdr 04-06-00:00:00:00:00:02 STATIC_AUTOGENERATED\n"
454 "2001::200:ff:fe00:4 dev 1 lladdr 04-06-00:00:00:00:00:04 STATIC_AUTOGENERATED\n"
455 "fe80::200:ff:fe00:1 dev 1 lladdr 04-06-00:00:00:00:00:01 STATIC_AUTOGENERATED\n"
456 "fe80::200:ff:fe00:2 dev 1 lladdr 04-06-00:00:00:00:00:02 STATIC_AUTOGENERATED\n"
457 "fe80::200:ff:fe00:4 dev 1 lladdr 04-06-00:00:00:00:00:04 STATIC_AUTOGENERATED\n"
458 "NDISC Cache of node 3 at time +0s\n"
459 "2001::200:ff:fe00:1 dev 1 lladdr 04-06-00:00:00:00:00:01 STATIC_AUTOGENERATED\n"
460 "2001::200:ff:fe00:2 dev 1 lladdr 04-06-00:00:00:00:00:02 STATIC_AUTOGENERATED\n"
461 "2001::200:ff:fe00:3 dev 1 lladdr 04-06-00:00:00:00:00:03 STATIC_AUTOGENERATED\n"
462 "fe80::200:ff:fe00:1 dev 1 lladdr 04-06-00:00:00:00:00:01 STATIC_AUTOGENERATED\n"
463 "fe80::200:ff:fe00:2 dev 1 lladdr 04-06-00:00:00:00:00:02 STATIC_AUTOGENERATED\n"
464 "fe80::200:ff:fe00:3 dev 1 lladdr 04-06-00:00:00:00:00:03 STATIC_AUTOGENERATED\n"
465 "NDISC Cache of node 0 at time +1s\n"
466 "2001::200:ff:fe00:2 dev 1 lladdr 04-06-00:00:00:00:00:02 STATIC_AUTOGENERATED\n"
467 "2001::200:ff:fe00:3 dev 1 lladdr 04-06-00:00:00:00:00:03 STATIC_AUTOGENERATED\n"
468 "2001::200:ff:fe00:4 dev 1 lladdr 04-06-00:00:00:00:00:04 STATIC_AUTOGENERATED\n"
469 "2001::200:ff:fe00:5 dev 1 lladdr 04-06-00:00:00:00:00:03 STATIC_AUTOGENERATED\n"
470 "fe80::200:ff:fe00:2 dev 1 lladdr 04-06-00:00:00:00:00:02 STATIC_AUTOGENERATED\n"
471 "fe80::200:ff:fe00:3 dev 1 lladdr 04-06-00:00:00:00:00:03 STATIC_AUTOGENERATED\n"
472 "fe80::200:ff:fe00:4 dev 1 lladdr 04-06-00:00:00:00:00:04 STATIC_AUTOGENERATED\n"
473 "NDISC Cache of node 1 at time +1s\n"
474 "2001::200:ff:fe00:1 dev 1 lladdr 04-06-00:00:00:00:00:01 STATIC_AUTOGENERATED\n"
475 "2001::200:ff:fe00:3 dev 1 lladdr 04-06-00:00:00:00:00:03 STATIC_AUTOGENERATED\n"
476 "2001::200:ff:fe00:4 dev 1 lladdr 04-06-00:00:00:00:00:04 STATIC_AUTOGENERATED\n"
477 "2001::200:ff:fe00:5 dev 1 lladdr 04-06-00:00:00:00:00:03 STATIC_AUTOGENERATED\n"
478 "fe80::200:ff:fe00:1 dev 1 lladdr 04-06-00:00:00:00:00:01 STATIC_AUTOGENERATED\n"
479 "fe80::200:ff:fe00:3 dev 1 lladdr 04-06-00:00:00:00:00:03 STATIC_AUTOGENERATED\n"
480 "fe80::200:ff:fe00:4 dev 1 lladdr 04-06-00:00:00:00:00:04 STATIC_AUTOGENERATED\n"
481 "NDISC Cache of node 2 at time +1s\n"
482 "2001::200:ff:fe00:1 dev 1 lladdr 04-06-00:00:00:00:00:01 STATIC_AUTOGENERATED\n"
483 "2001::200:ff:fe00:2 dev 1 lladdr 04-06-00:00:00:00:00:02 STATIC_AUTOGENERATED\n"
484 "2001::200:ff:fe00:4 dev 1 lladdr 04-06-00:00:00:00:00:04 STATIC_AUTOGENERATED\n"
485 "fe80::200:ff:fe00:1 dev 1 lladdr 04-06-00:00:00:00:00:01 STATIC_AUTOGENERATED\n"
486 "fe80::200:ff:fe00:2 dev 1 lladdr 04-06-00:00:00:00:00:02 STATIC_AUTOGENERATED\n"
487 "fe80::200:ff:fe00:4 dev 1 lladdr 04-06-00:00:00:00:00:04 STATIC_AUTOGENERATED\n"
488 "NDISC Cache of node 3 at time +1s\n"
489 "2001::200:ff:fe00:1 dev 1 lladdr 04-06-00:00:00:00:00:01 STATIC_AUTOGENERATED\n"
490 "2001::200:ff:fe00:2 dev 1 lladdr 04-06-00:00:00:00:00:02 STATIC_AUTOGENERATED\n"
491 "2001::200:ff:fe00:3 dev 1 lladdr 04-06-00:00:00:00:00:03 STATIC_AUTOGENERATED\n"
492 "2001::200:ff:fe00:5 dev 1 lladdr 04-06-00:00:00:00:00:03 STATIC_AUTOGENERATED\n"
493 "fe80::200:ff:fe00:1 dev 1 lladdr 04-06-00:00:00:00:00:01 STATIC_AUTOGENERATED\n"
494 "fe80::200:ff:fe00:2 dev 1 lladdr 04-06-00:00:00:00:00:02 STATIC_AUTOGENERATED\n"
495 "fe80::200:ff:fe00:3 dev 1 lladdr 04-06-00:00:00:00:00:03 STATIC_AUTOGENERATED\n"
496 "NDISC Cache of node 0 at time +2s\n"
497 "2001::200:ff:fe00:2 dev 1 lladdr 04-06-00:00:00:00:00:02 STATIC_AUTOGENERATED\n"
498 "2001::200:ff:fe00:4 dev 1 lladdr 04-06-00:00:00:00:00:04 STATIC_AUTOGENERATED\n"
499 "2001::200:ff:fe00:5 dev 1 lladdr 04-06-00:00:00:00:00:03 STATIC_AUTOGENERATED\n"
500 "fe80::200:ff:fe00:2 dev 1 lladdr 04-06-00:00:00:00:00:02 STATIC_AUTOGENERATED\n"
501 "fe80::200:ff:fe00:3 dev 1 lladdr 04-06-00:00:00:00:00:03 STATIC_AUTOGENERATED\n"
502 "fe80::200:ff:fe00:4 dev 1 lladdr 04-06-00:00:00:00:00:04 STATIC_AUTOGENERATED\n"
503 "NDISC Cache of node 1 at time +2s\n"
504 "2001::200:ff:fe00:1 dev 1 lladdr 04-06-00:00:00:00:00:01 STATIC_AUTOGENERATED\n"
505 "2001::200:ff:fe00:4 dev 1 lladdr 04-06-00:00:00:00:00:04 STATIC_AUTOGENERATED\n"
506 "2001::200:ff:fe00:5 dev 1 lladdr 04-06-00:00:00:00:00:03 STATIC_AUTOGENERATED\n"
507 "fe80::200:ff:fe00:1 dev 1 lladdr 04-06-00:00:00:00:00:01 STATIC_AUTOGENERATED\n"
508 "fe80::200:ff:fe00:3 dev 1 lladdr 04-06-00:00:00:00:00:03 STATIC_AUTOGENERATED\n"
509 "fe80::200:ff:fe00:4 dev 1 lladdr 04-06-00:00:00:00:00:04 STATIC_AUTOGENERATED\n"
510 "NDISC Cache of node 2 at time +2s\n"
511 "2001::200:ff:fe00:1 dev 1 lladdr 04-06-00:00:00:00:00:01 STATIC_AUTOGENERATED\n"
512 "2001::200:ff:fe00:2 dev 1 lladdr 04-06-00:00:00:00:00:02 STATIC_AUTOGENERATED\n"
513 "2001::200:ff:fe00:4 dev 1 lladdr 04-06-00:00:00:00:00:04 STATIC_AUTOGENERATED\n"
514 "fe80::200:ff:fe00:1 dev 1 lladdr 04-06-00:00:00:00:00:01 STATIC_AUTOGENERATED\n"
515 "fe80::200:ff:fe00:2 dev 1 lladdr 04-06-00:00:00:00:00:02 STATIC_AUTOGENERATED\n"
516 "fe80::200:ff:fe00:4 dev 1 lladdr 04-06-00:00:00:00:00:04 STATIC_AUTOGENERATED\n"
517 "NDISC Cache of node 3 at time +2s\n"
518 "2001::200:ff:fe00:1 dev 1 lladdr 04-06-00:00:00:00:00:01 STATIC_AUTOGENERATED\n"
519 "2001::200:ff:fe00:2 dev 1 lladdr 04-06-00:00:00:00:00:02 STATIC_AUTOGENERATED\n"
520 "2001::200:ff:fe00:5 dev 1 lladdr 04-06-00:00:00:00:00:03 STATIC_AUTOGENERATED\n"
521 "fe80::200:ff:fe00:1 dev 1 lladdr 04-06-00:00:00:00:00:01 STATIC_AUTOGENERATED\n"
522 "fe80::200:ff:fe00:2 dev 1 lladdr 04-06-00:00:00:00:00:02 STATIC_AUTOGENERATED\n"
523 "fe80::200:ff:fe00:3 dev 1 lladdr 04-06-00:00:00:00:00:03 STATIC_AUTOGENERATED\n";
524 NS_TEST_EXPECT_MSG_EQ(stringStream1v6.str(), NdiscCache, "Ndisc cache is incorrect.");
525
527
529}
530
531/**
532 * @ingroup internet-test
533 *
534 * @brief Neighbor cache on Channel Test
535 */
536class ChannelTest : public TestCase
537{
538 public:
539 void DoRun() override;
540 ChannelTest();
541
542 private:
543 NodeContainer m_nodes; //!< Nodes used in the test.
544};
545
547 : TestCase(
548 "The ChannelTest Check if neighbor caches are correctly populated on specific channel.")
549{
550}
551
552void
554{
555 m_nodes.Create(3);
556
558 SimpleNetDeviceHelper simpleHelper;
559 NetDeviceContainer net = simpleHelper.Install(m_nodes.Get(0), channel);
560 net.Add(simpleHelper.Install(m_nodes.Get(1), channel));
561
563 SimpleNetDeviceHelper simpleHelper2;
564 NetDeviceContainer net2 = simpleHelper.Install(m_nodes.Get(1), channel2);
565 net2.Add(simpleHelper2.Install(m_nodes.Get(2), channel2));
566
567 InternetStackHelper internet;
568 internet.Install(m_nodes);
569
570 // Setup IPv4 addresses
572 ipv4.SetBase("10.1.1.0", "255.255.255.252");
573 Ipv4InterfaceContainer i = ipv4.Assign(net);
574 ipv4.SetBase("10.1.2.0", "255.255.255.252");
575 Ipv4InterfaceContainer i2 = ipv4.Assign(net2);
576
577 // Setup IPv6 addresses
579 ipv6.SetBase(Ipv6Address("2001:0::"), Ipv6Prefix(64));
580 Ipv6InterfaceContainer icv61 = ipv6.Assign(net);
581 ipv6.SetBase(Ipv6Address("2001:1::"), Ipv6Prefix(64));
582 Ipv6InterfaceContainer icv62 = ipv6.Assign(net2);
583
584 // Populate neighbor caches on the first channel
585 NeighborCacheHelper neighborCache;
586 neighborCache.PopulateNeighborCache(channel);
587
588 std::ostringstream stringStream1v4;
589 Ptr<OutputStreamWrapper> arpStream = Create<OutputStreamWrapper>(&stringStream1v4);
590 std::ostringstream stringStream1v6;
591 Ptr<OutputStreamWrapper> ndiscStream = Create<OutputStreamWrapper>(&stringStream1v6);
592
593 // Print cache.
596
598
599 // Check if arp caches are populated correctly in the first channel
600 constexpr auto arpCache = "ARP Cache of node 0 at time 0\n"
601 "10.1.1.2 dev 0 lladdr 04-06-00:00:00:00:00:02 STATIC_AUTOGENERATED\n"
602 "ARP Cache of node 1 at time 0\n"
603 "10.1.1.1 dev 0 lladdr 04-06-00:00:00:00:00:01 STATIC_AUTOGENERATED\n"
604 "ARP Cache of node 2 at time 0\n";
605 NS_TEST_EXPECT_MSG_EQ(stringStream1v4.str(), arpCache, "Arp cache is incorrect.");
606
607 // Check if ndisc caches are populated correctly in the first channel
608 constexpr auto NdiscCache =
609 "NDISC Cache of node 0 at time +0s\n"
610 "2001::200:ff:fe00:2 dev 0 lladdr 04-06-00:00:00:00:00:02 STATIC_AUTOGENERATED\n"
611 "fe80::200:ff:fe00:2 dev 0 lladdr 04-06-00:00:00:00:00:02 STATIC_AUTOGENERATED\n"
612 "NDISC Cache of node 1 at time +0s\n"
613 "2001::200:ff:fe00:1 dev 0 lladdr 04-06-00:00:00:00:00:01 STATIC_AUTOGENERATED\n"
614 "fe80::200:ff:fe00:1 dev 0 lladdr 04-06-00:00:00:00:00:01 STATIC_AUTOGENERATED\n"
615 "NDISC Cache of node 2 at time +0s\n";
616 NS_TEST_EXPECT_MSG_EQ(stringStream1v6.str(), NdiscCache, "Ndisc cache is incorrect.");
618}
619
620/**
621 * @ingroup internet-test
622 *
623 * @brief Neighbor Cache on NetDeviceContainer Test
624 */
626{
627 public:
628 void DoRun() override;
630
631 private:
632 NodeContainer m_nodes; //!< Nodes used in the test.
633};
634
636 : TestCase("The NetDeviceContainerTest check if neighbor caches are populated correctly on "
637 "specific netDeviceContainer.")
638{
639}
640
641void
643{
644 m_nodes.Create(3);
645
647 SimpleNetDeviceHelper simpleHelper;
648 NetDeviceContainer net = simpleHelper.Install(m_nodes.Get(0), channel);
649 net.Add(simpleHelper.Install(m_nodes.Get(1), channel));
650
652 SimpleNetDeviceHelper simpleHelper2;
653 NetDeviceContainer net2 = simpleHelper.Install(m_nodes.Get(1), channel2);
654 net2.Add(simpleHelper2.Install(m_nodes.Get(2), channel2));
655
656 InternetStackHelper internet;
657 internet.Install(m_nodes);
658
659 // Setup IPv4 addresses
661 ipv4.SetBase("10.1.1.0", "255.255.255.252");
662 Ipv4InterfaceContainer i = ipv4.Assign(net);
663 ipv4.SetBase("10.1.2.0", "255.255.255.252");
664 Ipv4InterfaceContainer i2 = ipv4.Assign(net2);
665
666 // Setup IPv6 addresses
668 ipv6.SetBase(Ipv6Address("2001:0::"), Ipv6Prefix(64));
669 Ipv6InterfaceContainer icv61 = ipv6.Assign(net);
670 ipv6.SetBase(Ipv6Address("2001:1::"), Ipv6Prefix(64));
671 Ipv6InterfaceContainer icv62 = ipv6.Assign(net2);
672
673 // Populate neighbor caches on NetDeviceContainer net2.
674 NeighborCacheHelper neighborCache;
675 neighborCache.PopulateNeighborCache(net2);
676
677 std::ostringstream stringStream1v4;
678 Ptr<OutputStreamWrapper> arpStream = Create<OutputStreamWrapper>(&stringStream1v4);
679 std::ostringstream stringStream1v6;
680 Ptr<OutputStreamWrapper> ndiscStream = Create<OutputStreamWrapper>(&stringStream1v6);
681
682 // Print cache.
685
687
688 // Check if arp caches are populated correctly on NetDeviceContainer net2.
689 constexpr auto arpCache =
690 "ARP Cache of node 0 at time 0\n"
691 "ARP Cache of node 1 at time 0\n"
692 "10.1.2.2 dev 1 lladdr 04-06-00:00:00:00:00:04 STATIC_AUTOGENERATED\n"
693 "ARP Cache of node 2 at time 0\n"
694 "10.1.2.1 dev 0 lladdr 04-06-00:00:00:00:00:03 STATIC_AUTOGENERATED\n";
695 NS_TEST_EXPECT_MSG_EQ(stringStream1v4.str(), arpCache, "Arp cache is incorrect.");
696
697 // Check if ndisc caches are populated correctly on NetDeviceContainer net2.
698 constexpr auto NdiscCache =
699 "NDISC Cache of node 0 at time +0s\n"
700 "NDISC Cache of node 1 at time +0s\n"
701 "2001:1::200:ff:fe00:4 dev 1 lladdr 04-06-00:00:00:00:00:04 STATIC_AUTOGENERATED\n"
702 "fe80::200:ff:fe00:4 dev 1 lladdr 04-06-00:00:00:00:00:04 STATIC_AUTOGENERATED\n"
703 "NDISC Cache of node 2 at time +0s\n"
704 "2001:1::200:ff:fe00:3 dev 0 lladdr 04-06-00:00:00:00:00:03 STATIC_AUTOGENERATED\n"
705 "fe80::200:ff:fe00:3 dev 0 lladdr 04-06-00:00:00:00:00:03 STATIC_AUTOGENERATED\n";
706 NS_TEST_EXPECT_MSG_EQ(stringStream1v6.str(), NdiscCache, "Ndisc cache is incorrect.");
708}
709
710/**
711 * @ingroup internet-test
712 *
713 * @brief Neighbor Cache on InterfaceContainer Test
714 */
716{
717 public:
718 void DoRun() override;
720
721 private:
722 NodeContainer m_nodes; //!< Nodes used in the test.
723};
724
726 : TestCase("The InterfaceContainerTest check if neighbor caches are populated correctly on "
727 "specific interfaceContainer.")
728{
729}
730
731void
733{
734 m_nodes.Create(3);
735
737 SimpleNetDeviceHelper simpleHelper;
738 NetDeviceContainer net = simpleHelper.Install(m_nodes.Get(0), channel);
739 net.Add(simpleHelper.Install(m_nodes.Get(1), channel));
740
742 SimpleNetDeviceHelper simpleHelper2;
743 NetDeviceContainer net2 = simpleHelper.Install(m_nodes.Get(1), channel2);
744 net2.Add(simpleHelper2.Install(m_nodes.Get(2), channel2));
745
746 InternetStackHelper internet;
747 internet.Install(m_nodes);
748
749 // Setup IPv4 addresses
751 ipv4.SetBase("10.1.1.0", "255.255.255.252");
752 Ipv4InterfaceContainer i = ipv4.Assign(net);
753 ipv4.SetBase("10.1.2.0", "255.255.255.252");
754 Ipv4InterfaceContainer i2 = ipv4.Assign(net2);
755
756 // Setup IPv6 addresses
758 ipv6.SetBase(Ipv6Address("2001:0::"), Ipv6Prefix(64));
759 Ipv6InterfaceContainer icv61 = ipv6.Assign(net);
760 ipv6.SetBase(Ipv6Address("2001:1::"), Ipv6Prefix(64));
761 Ipv6InterfaceContainer icv62 = ipv6.Assign(net2);
762
763 // Populate neighbor caches on Ipv4InterfaceContainer i and Ipv6InterfaceContainer icv62.
764 NeighborCacheHelper neighborCache;
765 neighborCache.PopulateNeighborCache(i);
766 neighborCache.PopulateNeighborCache(icv62);
767
768 std::ostringstream stringStream1v4;
769 Ptr<OutputStreamWrapper> arpStream = Create<OutputStreamWrapper>(&stringStream1v4);
770 std::ostringstream stringStream1v6;
771 Ptr<OutputStreamWrapper> ndiscStream = Create<OutputStreamWrapper>(&stringStream1v6);
772
773 // Print cache.
776
778
779 // Check if arp caches are populated correctly on Ipv4InterfaceContainer i.
780 constexpr auto arpCache = "ARP Cache of node 0 at time 0\n"
781 "10.1.1.2 dev 0 lladdr 04-06-00:00:00:00:00:02 STATIC_AUTOGENERATED\n"
782 "ARP Cache of node 1 at time 0\n"
783 "10.1.1.1 dev 0 lladdr 04-06-00:00:00:00:00:01 STATIC_AUTOGENERATED\n"
784 "ARP Cache of node 2 at time 0\n";
785 NS_TEST_EXPECT_MSG_EQ(stringStream1v4.str(), arpCache, "Arp cache is incorrect.");
786
787 // Check if ndisc caches are populated correctly on Ipv6InterfaceContainer icv62.
788 constexpr auto NdiscCache =
789 "NDISC Cache of node 0 at time +0s\n"
790 "NDISC Cache of node 1 at time +0s\n"
791 "2001:1::200:ff:fe00:4 dev 1 lladdr 04-06-00:00:00:00:00:04 STATIC_AUTOGENERATED\n"
792 "fe80::200:ff:fe00:4 dev 1 lladdr 04-06-00:00:00:00:00:04 STATIC_AUTOGENERATED\n"
793 "NDISC Cache of node 2 at time +0s\n"
794 "2001:1::200:ff:fe00:3 dev 0 lladdr 04-06-00:00:00:00:00:03 STATIC_AUTOGENERATED\n"
795 "fe80::200:ff:fe00:3 dev 0 lladdr 04-06-00:00:00:00:00:03 STATIC_AUTOGENERATED\n";
796 NS_TEST_EXPECT_MSG_EQ(stringStream1v6.str(), NdiscCache, "Ndisc cache is incorrect.");
798}
799
800/**
801 * @ingroup internet-test
802 *
803 * @brief Neighbor Cache Flush Test
804 */
805class FlushTest : public TestCase
806{
807 public:
808 void DoRun() override;
809 FlushTest();
810
811 private:
812 NodeContainer m_nodes; //!< Nodes used in the test.
813};
814
816 : TestCase("The FlushTest checks that FlushAutoGenerated() will only remove "
817 "STATIC_AUTOGENERATED entries.")
818{
819}
820
821void
823{
824 m_nodes.Create(3);
825
827 SimpleNetDeviceHelper simpleHelper;
828 NetDeviceContainer net = simpleHelper.Install(m_nodes.Get(0), channel);
829 net.Add(simpleHelper.Install(m_nodes.Get(1), channel));
830
832 SimpleNetDeviceHelper simpleHelper2;
833 NetDeviceContainer net2 = simpleHelper.Install(m_nodes.Get(1), channel2);
834 net2.Add(simpleHelper2.Install(m_nodes.Get(2), channel2));
835
836 InternetStackHelper internet;
837 internet.Install(m_nodes);
838
839 // Setup IPv4 addresses
841 ipv4.SetBase("10.1.1.0", "255.255.255.252");
842 Ipv4InterfaceContainer i = ipv4.Assign(net);
843 ipv4.SetBase("10.1.2.0", "255.255.255.252");
844 Ipv4InterfaceContainer i2 = ipv4.Assign(net2);
845
846 // Setup IPv6 addresses
848 ipv6.SetBase(Ipv6Address("2001:0::"), Ipv6Prefix(64));
849 Ipv6InterfaceContainer icv61 = ipv6.Assign(net);
850 ipv6.SetBase(Ipv6Address("2001:1::"), Ipv6Prefix(64));
851 Ipv6InterfaceContainer icv62 = ipv6.Assign(net2);
852
853 // Populate STATIC_AUTOGENERATED neighbor cache
854 NeighborCacheHelper neighborCache;
855 neighborCache.PopulateNeighborCache();
856
857 // Manually add an PERMANENT arp cache entry
858 std::pair<Ptr<Ipv4>, uint32_t> returnValue = i.Get(0);
859 Ptr<Ipv4> v4 = returnValue.first;
860 uint32_t index = returnValue.second;
861 Ptr<Ipv4Interface> iface = DynamicCast<Ipv4L3Protocol>(v4)->GetInterface(index);
862 Ptr<ArpCache> arpCache = iface->GetArpCache();
863 ArpCache::Entry* arpCacheEntry = arpCache->Add(Ipv4Address("10.1.1.4"));
864 arpCacheEntry->SetMacAddress(Mac48Address("00:00:00:00:00:01"));
865 arpCacheEntry->MarkPermanent();
866
867 // Manually add an PERMANENT ndisc entry
868 std::pair<Ptr<Ipv6>, uint32_t> returnValue2 = icv61.Get(0);
869 Ptr<Ipv6> v6 = returnValue2.first;
870 index = returnValue2.second;
871 Ptr<Ipv6Interface> ifacev6 = DynamicCast<Ipv6L3Protocol>(v6)->GetInterface(index);
872 Ptr<NdiscCache> ndiscCache = ifacev6->GetNdiscCache();
873 NdiscCache::Entry* ndiscCacheEntry = ndiscCache->Add(Ipv6Address("2001::200:ff:fe00:4"));
874 ndiscCacheEntry->SetMacAddress(Mac48Address("00:00:00:00:00:01"));
875 ndiscCacheEntry->MarkPermanent();
876
877 // flush auto-generated cache
878 neighborCache.FlushAutoGenerated();
879
880 std::ostringstream stringStream1v4;
881 Ptr<OutputStreamWrapper> arpStream = Create<OutputStreamWrapper>(&stringStream1v4);
882 std::ostringstream stringStream1v6;
883 Ptr<OutputStreamWrapper> ndiscStream = Create<OutputStreamWrapper>(&stringStream1v6);
884
885 // Print cache.
888
890 // Check if the STATIC_AUTOGENERATED entries are flushed and the PERMANENT entry is left.
891 constexpr auto ArpCache = "ARP Cache of node 0 at time 0\n"
892 "10.1.1.4 dev 0 lladdr 04-06-00:00:00:00:00:01 PERMANENT\n"
893 "ARP Cache of node 1 at time 0\n"
894 "ARP Cache of node 2 at time 0\n";
895 NS_TEST_EXPECT_MSG_EQ(stringStream1v4.str(), ArpCache, "Arp cache is incorrect.");
896
897 // Check if the STATIC_AUTOGENERATED entries are flushed and the PERMANENT entry is left.
898 constexpr auto NdiscCache =
899 "NDISC Cache of node 0 at time +0s\n"
900 "2001::200:ff:fe00:4 dev 0 lladdr 04-06-00:00:00:00:00:01 PERMANENT\n"
901 "NDISC Cache of node 1 at time +0s\n"
902 "NDISC Cache of node 2 at time +0s\n";
903 NS_TEST_EXPECT_MSG_EQ(stringStream1v6.str(), NdiscCache, "Ndisc cache is incorrect.");
905}
906
907/**
908 * @ingroup internet-test
909 *
910 * @brief Neighbor Cache on Overlapped Scope Test
911 */
913{
914 public:
915 void DoRun() override;
917
918 private:
919 NodeContainer m_nodes; //!< Nodes used in the test.
920};
921
923 : TestCase("The DuplicateTest checks that populate neighbor caches in overlapped scope does "
924 "not raise an error or generate duplicate entries.")
925{
926}
927
928void
930{
931 m_nodes.Create(3);
932
934 SimpleNetDeviceHelper simpleHelper;
935 NetDeviceContainer net = simpleHelper.Install(m_nodes.Get(0), channel);
936 net.Add(simpleHelper.Install(m_nodes.Get(1), channel));
937
939 SimpleNetDeviceHelper simpleHelper2;
940 NetDeviceContainer net2 = simpleHelper.Install(m_nodes.Get(1), channel2);
941 net2.Add(simpleHelper2.Install(m_nodes.Get(2), channel2));
942
943 InternetStackHelper internet;
944 internet.Install(m_nodes);
945
946 // Setup IPv4 addresses
948 ipv4.SetBase("10.1.1.0", "255.255.255.252");
949 Ipv4InterfaceContainer i = ipv4.Assign(net);
950 ipv4.SetBase("10.1.2.0", "255.255.255.252");
951 Ipv4InterfaceContainer i2 = ipv4.Assign(net2);
952
953 // Setup IPv6 addresses
955 ipv6.SetBase(Ipv6Address("2001:0::"), Ipv6Prefix(64));
956 Ipv6InterfaceContainer icv61 = ipv6.Assign(net);
957 ipv6.SetBase(Ipv6Address("2001:1::"), Ipv6Prefix(64));
958 Ipv6InterfaceContainer icv62 = ipv6.Assign(net2);
959
960 // Populate neighbor cache in overlapped scope.
961 NeighborCacheHelper neighborCache;
962 neighborCache.PopulateNeighborCache();
963 neighborCache.PopulateNeighborCache(channel);
964 neighborCache.PopulateNeighborCache(net2);
965 neighborCache.PopulateNeighborCache(icv61);
966 neighborCache.PopulateNeighborCache();
967
968 std::ostringstream stringStream1v4;
969 Ptr<OutputStreamWrapper> arpStream = Create<OutputStreamWrapper>(&stringStream1v4);
970 std::ostringstream stringStream1v6;
971 Ptr<OutputStreamWrapper> ndiscStream = Create<OutputStreamWrapper>(&stringStream1v6);
972
973 // Print cache.
976
978 // Check if the STATIC_AUTOGENERATED entries are flushed and the PERMANENT entry is left.
979 constexpr auto ArpCache =
980 "ARP Cache of node 0 at time 0\n"
981 "10.1.1.2 dev 0 lladdr 04-06-00:00:00:00:00:02 STATIC_AUTOGENERATED\n"
982 "ARP Cache of node 1 at time 0\n"
983 "10.1.1.1 dev 0 lladdr 04-06-00:00:00:00:00:01 STATIC_AUTOGENERATED\n"
984 "10.1.2.2 dev 1 lladdr 04-06-00:00:00:00:00:04 STATIC_AUTOGENERATED\n"
985 "ARP Cache of node 2 at time 0\n"
986 "10.1.2.1 dev 0 lladdr 04-06-00:00:00:00:00:03 STATIC_AUTOGENERATED\n";
987 NS_TEST_EXPECT_MSG_EQ(stringStream1v4.str(), ArpCache, "Arp cache is incorrect.");
988
989 // Check if the STATIC_AUTOGENERATED entries are flushed and the PERMANENT entry is left.
990 constexpr auto NdiscCache =
991 "NDISC Cache of node 0 at time +0s\n"
992 "2001::200:ff:fe00:2 dev 0 lladdr 04-06-00:00:00:00:00:02 STATIC_AUTOGENERATED\n"
993 "fe80::200:ff:fe00:2 dev 0 lladdr 04-06-00:00:00:00:00:02 STATIC_AUTOGENERATED\n"
994 "NDISC Cache of node 1 at time +0s\n"
995 "2001::200:ff:fe00:1 dev 0 lladdr 04-06-00:00:00:00:00:01 STATIC_AUTOGENERATED\n"
996 "fe80::200:ff:fe00:1 dev 0 lladdr 04-06-00:00:00:00:00:01 STATIC_AUTOGENERATED\n"
997 "2001:1::200:ff:fe00:4 dev 1 lladdr 04-06-00:00:00:00:00:04 STATIC_AUTOGENERATED\n"
998 "fe80::200:ff:fe00:4 dev 1 lladdr 04-06-00:00:00:00:00:04 STATIC_AUTOGENERATED\n"
999 "NDISC Cache of node 2 at time +0s\n"
1000 "2001:1::200:ff:fe00:3 dev 0 lladdr 04-06-00:00:00:00:00:03 STATIC_AUTOGENERATED\n"
1001 "fe80::200:ff:fe00:3 dev 0 lladdr 04-06-00:00:00:00:00:03 STATIC_AUTOGENERATED\n";
1002 NS_TEST_EXPECT_MSG_EQ(stringStream1v6.str(), NdiscCache, "Ndisc cache is incorrect.");
1004}
1005
1006/**
1007 * @ingroup internet-test
1008 *
1009 * @brief Dynamic Neighbor Cache on Reduced Scope Test
1010 */
1012{
1013 public:
1014 void DoRun() override;
1016
1017 /**
1018 * @brief Add an IPv4 address to an IPv4 interface
1019 * @param ipv4Interface The interface that address will be added.
1020 * @param ifaceAddr The added IPv4 address.
1021 */
1022 void AddIpv4Address(Ptr<Ipv4Interface> ipv4Interface, Ipv4InterfaceAddress ifaceAddr);
1023
1024 /**
1025 * @brief Add an IPv6 address to an IPv6 interface
1026 * @param ipv6Interface The interface that address will be added.
1027 * @param ifaceAddr The added IPv6 address.
1028 */
1029 void AddIpv6Address(Ptr<Ipv6Interface> ipv6Interface, Ipv6InterfaceAddress ifaceAddr);
1030
1031 /**
1032 * @brief Remove an IPv4 address from an IPv4 interface
1033 * @param ipv4Interface The interface that address will be removed from.
1034 * @param index The index of IPv4 address that will be removed.
1035 */
1036 void RemoveIpv4Address(Ptr<Ipv4Interface> ipv4Interface, uint32_t index);
1037
1038 /**
1039 * @brief Remove an IPv6 address from an IPv6 interface
1040 * @param ipv6Interface The interface that address will be removed from.
1041 * @param index The index of IPv6 address that will be removed.
1042 */
1043 void RemoveIpv6Address(Ptr<Ipv6Interface> ipv6Interface, uint32_t index);
1044
1045 private:
1046 NodeContainer m_nodes; //!< Nodes used in the test.
1047};
1048
1050 : TestCase("The DynamicPartialTest checks if dynamic neighbor cache update correctly when "
1051 "generating on a non-global scope.")
1052{
1053}
1054
1055void
1057{
1058 ipv4Interface->AddAddress(ifaceAddr);
1059}
1060
1061void
1063{
1064 ipv6Interface->AddAddress(ifaceAddr);
1065}
1066
1067void
1069{
1070 ipv4Interface->RemoveAddress(index);
1071}
1072
1073void
1075{
1076 ipv6Interface->RemoveAddress(index);
1077}
1078
1079void
1081{
1082 m_nodes.Create(3);
1083
1085 SimpleNetDeviceHelper simpleHelper;
1086 NetDeviceContainer net = simpleHelper.Install(m_nodes.Get(0), channel);
1087 net.Add(simpleHelper.Install(m_nodes.Get(1), channel));
1088
1090 SimpleNetDeviceHelper simpleHelper2;
1091 NetDeviceContainer net2 = simpleHelper.Install(m_nodes.Get(1), channel2);
1092 net2.Add(simpleHelper2.Install(m_nodes.Get(2), channel2));
1093
1094 InternetStackHelper internet;
1095 internet.Install(m_nodes);
1096
1097 // Setup IPv4 addresses
1098 Ipv4AddressHelper ipv4;
1099 ipv4.SetBase("10.1.1.0", "255.255.255.252");
1100 Ipv4InterfaceContainer i = ipv4.Assign(net);
1101 ipv4.SetBase("10.1.2.0", "255.255.255.252");
1102 Ipv4InterfaceContainer i2 = ipv4.Assign(net2);
1103
1104 // Add address 10.1.1.5 to n1 in 0.5 seconds
1105 Ptr<Node> n1 = m_nodes.Get(0);
1106 uint32_t ipv4ifIndex = 1;
1107 Ptr<Ipv4Interface> ipv4Interface = n1->GetObject<Ipv4L3Protocol>()->GetInterface(ipv4ifIndex);
1108 Ipv4InterfaceAddress ifaceAddr = Ipv4InterfaceAddress("10.1.1.5", "255.255.255.0");
1111 this,
1112 ipv4Interface,
1113 ifaceAddr);
1114
1115 // Remove the first address (10.1.1.1) from n1 in 1.5 seconds
1116 uint32_t addressIndex = 0;
1119 this,
1120 ipv4Interface,
1121 addressIndex);
1122
1123 // Setup IPv6 addresses
1124 Ipv6AddressHelper ipv6;
1125 ipv6.SetBase(Ipv6Address("2001:0::"), Ipv6Prefix(64));
1126 Ipv6InterfaceContainer icv61 = ipv6.Assign(net);
1127 ipv6.SetBase(Ipv6Address("2001:1::"), Ipv6Prefix(64));
1128 Ipv6InterfaceContainer icv62 = ipv6.Assign(net2);
1129
1130 // Add address 2001:1::200:ff:fe00:5 to n1 in 0.5 seconds
1131 uint32_t ipv6ifIndex = 1;
1132 Ptr<Ipv6Interface> ipv6Interface = n1->GetObject<Ipv6L3Protocol>()->GetInterface(ipv6ifIndex);
1133 Ipv6InterfaceAddress ifaceAddrv6 =
1134 Ipv6InterfaceAddress("2001:0::200:ff:fe00:5", Ipv6Prefix(64));
1137 this,
1138 ipv6Interface,
1139 ifaceAddrv6);
1140
1141 // Remove the second address (2001:1::200:ff:fe00:1) from n1 in 1.5 seconds
1142 addressIndex = 1;
1145 this,
1146 ipv6Interface,
1147 addressIndex);
1148
1149 // Populate dynamic neighbor cache on the first channel
1150 NeighborCacheHelper neighborCache;
1151 neighborCache.SetDynamicNeighborCache(true);
1152 neighborCache.PopulateNeighborCache(channel);
1153
1154 std::ostringstream stringStream1v4;
1155 Ptr<OutputStreamWrapper> arpStream = Create<OutputStreamWrapper>(&stringStream1v4);
1156 std::ostringstream stringStream1v6;
1157 Ptr<OutputStreamWrapper> ndiscStream = Create<OutputStreamWrapper>(&stringStream1v6);
1158
1159 // Print cache.
1166
1168 // Check if the dynamic neighbor cache doesn't change after an Ip address is added,
1169 // Check if the dynamic neighbor cache update correctly after an Ip address is removed.
1170 constexpr auto ArpCache = "ARP Cache of node 0 at time 0\n"
1171 "10.1.1.2 dev 0 lladdr 04-06-00:00:00:00:00:02 STATIC_AUTOGENERATED\n"
1172 "ARP Cache of node 1 at time 0\n"
1173 "10.1.1.1 dev 0 lladdr 04-06-00:00:00:00:00:01 STATIC_AUTOGENERATED\n"
1174 "ARP Cache of node 2 at time 0\n"
1175 "ARP Cache of node 0 at time 1\n"
1176 "10.1.1.2 dev 0 lladdr 04-06-00:00:00:00:00:02 STATIC_AUTOGENERATED\n"
1177 "ARP Cache of node 1 at time 1\n"
1178 "10.1.1.1 dev 0 lladdr 04-06-00:00:00:00:00:01 STATIC_AUTOGENERATED\n"
1179 "ARP Cache of node 2 at time 1\n"
1180 "ARP Cache of node 0 at time 2\n"
1181 "10.1.1.2 dev 0 lladdr 04-06-00:00:00:00:00:02 STATIC_AUTOGENERATED\n"
1182 "ARP Cache of node 1 at time 2\n"
1183 "ARP Cache of node 2 at time 2\n";
1184 NS_TEST_EXPECT_MSG_EQ(stringStream1v4.str(), ArpCache, "Arp cache is incorrect.");
1185
1186 // Check if the dynamic neighbor cache doesn't change after an Ip address is added,
1187 // Check if the dynamic neighbor cache update correctly after an Ip address is removed.
1188 constexpr auto NdiscCache =
1189 "NDISC Cache of node 0 at time +0s\n"
1190 "2001::200:ff:fe00:2 dev 0 lladdr 04-06-00:00:00:00:00:02 STATIC_AUTOGENERATED\n"
1191 "fe80::200:ff:fe00:2 dev 0 lladdr 04-06-00:00:00:00:00:02 STATIC_AUTOGENERATED\n"
1192 "NDISC Cache of node 1 at time +0s\n"
1193 "2001::200:ff:fe00:1 dev 0 lladdr 04-06-00:00:00:00:00:01 STATIC_AUTOGENERATED\n"
1194 "fe80::200:ff:fe00:1 dev 0 lladdr 04-06-00:00:00:00:00:01 STATIC_AUTOGENERATED\n"
1195 "NDISC Cache of node 2 at time +0s\n"
1196 "NDISC Cache of node 0 at time +1s\n"
1197 "2001::200:ff:fe00:2 dev 0 lladdr 04-06-00:00:00:00:00:02 STATIC_AUTOGENERATED\n"
1198 "fe80::200:ff:fe00:2 dev 0 lladdr 04-06-00:00:00:00:00:02 STATIC_AUTOGENERATED\n"
1199 "NDISC Cache of node 1 at time +1s\n"
1200 "2001::200:ff:fe00:1 dev 0 lladdr 04-06-00:00:00:00:00:01 STATIC_AUTOGENERATED\n"
1201 "fe80::200:ff:fe00:1 dev 0 lladdr 04-06-00:00:00:00:00:01 STATIC_AUTOGENERATED\n"
1202 "NDISC Cache of node 2 at time +1s\n"
1203 "NDISC Cache of node 0 at time +2s\n"
1204 "2001::200:ff:fe00:2 dev 0 lladdr 04-06-00:00:00:00:00:02 STATIC_AUTOGENERATED\n"
1205 "fe80::200:ff:fe00:2 dev 0 lladdr 04-06-00:00:00:00:00:02 STATIC_AUTOGENERATED\n"
1206 "NDISC Cache of node 1 at time +2s\n"
1207 "fe80::200:ff:fe00:1 dev 0 lladdr 04-06-00:00:00:00:00:01 STATIC_AUTOGENERATED\n"
1208 "NDISC Cache of node 2 at time +2s\n";
1209 NS_TEST_EXPECT_MSG_EQ(stringStream1v6.str(), NdiscCache, "Ndisc cache is incorrect.");
1211}
1212
1213/**
1214 * @ingroup internet-test
1215 *
1216 * @brief NeighborCache TestSuite
1217 */
1219{
1220 public:
1222 : TestSuite("neighbor-cache", Type::UNIT)
1223 {
1224 AddTestCase(new DynamicNeighborCacheTest, TestCase::Duration::QUICK);
1225 AddTestCase(new ChannelTest, TestCase::Duration::QUICK);
1226 AddTestCase(new NetDeviceContainerTest, TestCase::Duration::QUICK);
1227 AddTestCase(new InterfaceContainerTest, TestCase::Duration::QUICK);
1228 AddTestCase(new FlushTest, TestCase::Duration::QUICK);
1229 AddTestCase(new DuplicateTest, TestCase::Duration::QUICK);
1230 AddTestCase(new DynamicPartialTest, TestCase::Duration::QUICK);
1231 }
1232};
1233
1234static NeighborCacheTestSuite g_neighborcacheTestSuite; //!< Static variable for test initialization
Neighbor cache on Channel Test.
void DoRun() override
Implementation to actually run this TestCase.
NodeContainer m_nodes
Nodes used in the test.
Neighbor Cache on Overlapped Scope Test.
NodeContainer m_nodes
Nodes used in the test.
void DoRun() override
Implementation to actually run this TestCase.
Dynamic Neighbor Cache Test.
std::vector< uint32_t > m_receivedPacketSizes
Received packet sizes.
void ReceivePkt(Ptr< Socket > socket)
Receive data.
void RemoveIpv4Address(Ptr< Ipv4Interface > ipv4Interface, uint32_t index)
Remove an IPv4 address from an IPv4 interface.
void AddIpv4Address(Ptr< Ipv4Interface > ipv4Interface, Ipv4InterfaceAddress ifaceAddr)
Add an IPv4 address to an IPv4 interface.
void AddIpv6Address(Ptr< Ipv6Interface > ipv6Interface, Ipv6InterfaceAddress ifaceAddr)
Add an IPv6 address to an IPv6 interface.
void DoRun() override
Implementation to actually run this TestCase.
void DoSendDatav4(Ptr< Socket > socket, Ipv4Address to)
Send data immediately after being called.
void RemoveIpv6Address(Ptr< Ipv6Interface > ipv6Interface, uint32_t index)
Remove an IPv6 address from an IPv6 interface.
void DoSendDatav6(Ptr< Socket > socket, Ipv6Address to)
Send data immediately after being called.
void SendData(Ptr< Socket > socket, Ipv4Address to)
Schedules the DoSendData () function to send the data.
Ptr< Packet > m_receivedPacket
Received packet.
Dynamic Neighbor Cache on Reduced Scope Test.
void DoRun() override
Implementation to actually run this TestCase.
void AddIpv6Address(Ptr< Ipv6Interface > ipv6Interface, Ipv6InterfaceAddress ifaceAddr)
Add an IPv6 address to an IPv6 interface.
void RemoveIpv4Address(Ptr< Ipv4Interface > ipv4Interface, uint32_t index)
Remove an IPv4 address from an IPv4 interface.
void RemoveIpv6Address(Ptr< Ipv6Interface > ipv6Interface, uint32_t index)
Remove an IPv6 address from an IPv6 interface.
void AddIpv4Address(Ptr< Ipv4Interface > ipv4Interface, Ipv4InterfaceAddress ifaceAddr)
Add an IPv4 address to an IPv4 interface.
NodeContainer m_nodes
Nodes used in the test.
Neighbor Cache Flush Test.
void DoRun() override
Implementation to actually run this TestCase.
NodeContainer m_nodes
Nodes used in the test.
Neighbor Cache on InterfaceContainer Test.
NodeContainer m_nodes
Nodes used in the test.
void DoRun() override
Implementation to actually run this TestCase.
NeighborCache TestSuite.
Neighbor Cache on NetDeviceContainer Test.
NodeContainer m_nodes
Nodes used in the test.
void DoRun() override
Implementation to actually run this TestCase.
a polymophic address class
Definition address.h:90
A record that that holds information about an ArpCache entry.
Definition arp-cache.h:173
void MarkPermanent()
Changes the state of this entry to Permanent.
Definition arp-cache.cc:435
void SetMacAddress(Address macAddress)
Definition arp-cache.cc:495
An ARP cache.
Definition arp-cache.h:42
An Inet6 address class.
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Ipv4 addresses are stored in host order in this class.
a class to store IPv4 address information on an interface
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.
Implement the IPv4 layer.
a class to represent an Ipv4 address mask
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.
void SetBase(Ipv6Address network, Ipv6Prefix prefix, Ipv6Address base=Ipv6Address("::1"))
Set the base network number, network prefix, and base interface ID.
Ipv6InterfaceContainer Assign(const NetDeviceContainer &c)
Allocate an Ipv6InterfaceContainer with auto-assigned addresses.
Describes an IPv6 address.
IPv6 address associated with an interface.
Keep track of a set of IPv6 interfaces.
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.
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.
an EUI-48 address
A record that holds information about a NdiscCache entry.
void MarkPermanent()
Change the state to this entry to PERMANENT.
void SetMacAddress(Address mac)
Set the MAC address of this entry.
IPv6 Neighbor Discovery cache.
Definition ndisc-cache.h:38
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...
void FlushAutoGenerated() const
Remove entries generated from NeighborCacheHelper from ARP cache and NDISC cache.
holds a vector of ns3::NetDevice pointers
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
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.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition packet.h:850
void RemoveAllByteTags()
Remove all byte tags stored in this packet.
Definition packet.cc:382
Smart pointer class similar to boost::intrusive_ptr.
build a set of SimpleNetDevice objects
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::SimpleChannel with the attributes configured by SimpleNetDeviceHelper::Se...
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 ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition simulator.h:577
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
encapsulates test code
Definition test.h:1050
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
A suite of tests to run.
Definition test.h:1267
Type
Type of test.
Definition test.h:1274
static constexpr auto UNIT
Definition test.h:1291
API to create UDP socket instances.
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition test.h:134
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
Definition test.h:241
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1344
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:684
Ptr< T1 > DynamicCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition ptr.h:580
static NeighborCacheTestSuite g_neighborcacheTestSuite
Static variable for test initialization.