A Discrete-Event Network Simulator
API
ndisc-cache.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007-2009 Strasbourg University
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Sebastien Vincent <vincent@clarinet.u-strasbg.fr>
19  */
20 
21 #include "ns3/log.h"
22 #include "ns3/uinteger.h"
23 #include "ns3/node.h"
24 #include "ns3/names.h"
25 
26 #include "ipv6-l3-protocol.h"
27 #include "icmpv6-l4-protocol.h"
28 #include "ndisc-cache.h"
29 #include "ipv6-interface.h"
30 
31 namespace ns3
32 {
33 
34 NS_LOG_COMPONENT_DEFINE ("NdiscCache");
35 
36 NS_OBJECT_ENSURE_REGISTERED (NdiscCache);
37 
39 {
40  static TypeId tid = TypeId ("ns3::NdiscCache")
41  .SetParent<Object> ()
42  .SetGroupName ("Internet")
43  .AddAttribute ("UnresolvedQueueSize",
44  "Size of the queue for packets pending an NA reply.",
47  MakeUintegerChecker<uint32_t> ())
48  ;
49  return tid;
50 }
51 
53 {
55 }
56 
58 {
60  Flush ();
61 }
62 
64 {
66  Flush ();
67  m_device = 0;
68  m_interface = 0;
70 }
71 
73 {
74  NS_LOG_FUNCTION (this << device << interface);
75  m_device = device;
76  m_interface = interface;
77 }
78 
80 {
82  return m_interface;
83 }
84 
86 {
88  return m_device;
89 }
90 
92 {
93  NS_LOG_FUNCTION (this << dst);
94 
95  if (m_ndCache.find (dst) != m_ndCache.end ())
96  {
97  NdiscCache::Entry* entry = m_ndCache[dst];
98  return entry;
99  }
100  return 0;
101 }
102 
103 std::list<NdiscCache::Entry*> NdiscCache::LookupInverse (Address dst)
104 {
105  NS_LOG_FUNCTION (this << dst);
106 
107  std::list<NdiscCache::Entry *> entryList;
108  for (CacheI i = m_ndCache.begin (); i != m_ndCache.end (); i++)
109  {
110  NdiscCache::Entry *entry = (*i).second;
111  if (entry->GetMacAddress () == dst)
112  {
113  entryList.push_back (entry);
114  }
115  }
116  return entryList;
117 }
118 
119 
121 {
122  NS_LOG_FUNCTION (this << to);
123  NS_ASSERT (m_ndCache.find (to) == m_ndCache.end ());
124 
125  NdiscCache::Entry* entry = new NdiscCache::Entry (this);
126  entry->SetIpv6Address (to);
127  m_ndCache[to] = entry;
128  return entry;
129 }
130 
132 {
134 
135  for (CacheI i = m_ndCache.begin (); i != m_ndCache.end (); i++)
136  {
137  if ((*i).second == entry)
138  {
139  m_ndCache.erase (i);
140  entry->ClearWaitingPacket ();
141  delete entry;
142  return;
143  }
144  }
145 }
146 
148 {
150 
151  for (CacheI i = m_ndCache.begin (); i != m_ndCache.end (); i++)
152  {
153  delete (*i).second; /* delete the pointer NdiscCache::Entry */
154  }
155 
156  m_ndCache.erase (m_ndCache.begin (), m_ndCache.end ());
157 }
158 
159 void NdiscCache::SetUnresQlen (uint32_t unresQlen)
160 {
161  NS_LOG_FUNCTION (this << unresQlen);
162  m_unresQlen = unresQlen;
163 }
164 
166 {
168  return m_unresQlen;
169 }
170 
172 {
173  NS_LOG_FUNCTION (this << stream);
174  std::ostream* os = stream->GetStream ();
175 
176  for (CacheI i = m_ndCache.begin (); i != m_ndCache.end (); i++)
177  {
178  *os << i->first << " dev ";
179  std::string found = Names::FindName (m_device);
180  if (Names::FindName (m_device) != "")
181  {
182  *os << found;
183  }
184  else
185  {
186  *os << static_cast<int> (m_device->GetIfIndex ());
187  }
188 
189  *os << " lladdr " << i->second->GetMacAddress ();
190 
191  if (i->second->IsReachable ())
192  {
193  *os << " REACHABLE\n";
194  }
195  else if (i->second->IsDelay ())
196  {
197  *os << " DELAY\n";
198  }
199  else if (i->second->IsIncomplete ())
200  {
201  *os << " INCOMPLETE\n";
202  }
203  else if (i->second->IsProbe ())
204  {
205  *os << " PROBE\n";
206  }
207  else if (i->second->IsStale ())
208  {
209  *os << " STALE\n";
210  }
211  else if (i->second->IsPermanent ())
212  {
213  *os << " PERMANENT\n";
214  }
215  else
216  {
217  NS_FATAL_ERROR ("Test for possibly unreachable code-- please file a bug report, with a test case, if this is ever hit");
218  }
219  }
220 }
221 
223  : m_ndCache (nd),
224  m_waiting (),
225  m_router (false),
226  m_nudTimer (Timer::CANCEL_ON_DESTROY),
227  m_lastReachabilityConfirmation (Seconds (0.0)),
228  m_nsRetransmit (0)
229 {
231 }
232 
234 {
235  NS_LOG_FUNCTION (this << router);
236  m_router = router;
237 }
238 
240 {
242  return m_router;
243 }
244 
246 {
247  NS_LOG_FUNCTION (this << p.second << p.first);
248 
249  if (m_waiting.size () >= m_ndCache->GetUnresQlen ())
250  {
251  /* we store only m_unresQlen packet => first packet in first packet remove */
253  m_waiting.pop_front ();
254  }
255  m_waiting.push_back (p);
256 }
257 
259 {
262  m_waiting.clear ();
263 }
264 
266 {
268  this->MarkStale ();
269 }
270 
272 {
274  Ptr<Icmpv6L4Protocol> icmpv6 = m_ndCache->GetDevice ()->GetNode ()->GetObject<Ipv6L3Protocol> ()->GetIcmpv6 ();
275  Ipv6Address addr;
276 
277  /* determine source address */
278  if (m_ipv6Address.IsLinkLocal ())
279  {
280  addr = m_ndCache->GetInterface ()->GetLinkLocalAddress ().GetAddress ();;
281  }
282  else if (!m_ipv6Address.IsAny ())
283  {
284  addr = m_ndCache->GetInterface ()->GetAddressMatchingDestination (m_ipv6Address).GetAddress ();
285 
286  if (addr.IsAny ()) /* maybe address has expired */
287  {
288  /* delete the entry */
289  m_ndCache->Remove (this);
290  return;
291  }
292  }
293 
294  if (m_nsRetransmit < icmpv6->MAX_MULTICAST_SOLICIT)
295  {
296  m_nsRetransmit++;
297 
298  icmpv6->SendNS (addr, Ipv6Address::MakeSolicitedAddress (m_ipv6Address), m_ipv6Address, m_ndCache->GetDevice ()->GetAddress ());
299  /* arm the timer again */
300  StartRetransmitTimer ();
301  }
302  else
303  {
304  Ipv6PayloadHeaderPair malformedPacket = m_waiting.front ();
305  if (malformedPacket.first == 0)
306  {
307  malformedPacket.first = Create<Packet> ();
308  }
309  else
310  {
311  malformedPacket.first->AddHeader (malformedPacket.second);
312  }
313 
314  icmpv6->SendErrorDestinationUnreachable (malformedPacket.first, addr, Icmpv6Header::ICMPV6_ADDR_UNREACHABLE);
315 
316  /* delete the entry */
317  m_ndCache->Remove (this);
318  }
319 }
320 
322 {
324  Ptr<Ipv6L3Protocol> ipv6 = m_ndCache->GetDevice ()->GetNode ()->GetObject<Ipv6L3Protocol> ();
325  Ptr<Icmpv6L4Protocol> icmpv6 = ipv6->GetIcmpv6 ();
326  Ipv6Address addr;
327 
328  this->MarkProbe ();
329 
330  if (m_ipv6Address.IsLinkLocal ())
331  {
332  addr = m_ndCache->GetInterface ()->GetLinkLocalAddress ().GetAddress ();
333  }
334  else if (!m_ipv6Address.IsAny ())
335  {
336  addr = m_ndCache->GetInterface ()->GetAddressMatchingDestination (m_ipv6Address).GetAddress ();
337  if (addr.IsAny ()) /* maybe address has expired */
338  {
339  /* delete the entry */
340  m_ndCache->Remove (this);
341  return;
342  }
343  }
344  else
345  {
346  /* should not happen */
347  return;
348  }
349 
350  Ipv6PayloadHeaderPair p = icmpv6->ForgeNS (addr, m_ipv6Address, m_ipv6Address, m_ndCache->GetDevice ()->GetAddress ());
351  p.first->AddHeader (p.second);
352  m_ndCache->GetDevice ()->Send (p.first, this->GetMacAddress (), Ipv6L3Protocol::PROT_NUMBER);
353 
354  m_nsRetransmit = 1;
355  StartProbeTimer ();
356 }
357 
359 {
361  Ptr<Ipv6L3Protocol> ipv6 = m_ndCache->GetDevice ()->GetNode ()->GetObject<Ipv6L3Protocol> ();
362  Ptr<Icmpv6L4Protocol> icmpv6 = ipv6->GetIcmpv6 ();
363 
364  if (m_nsRetransmit < icmpv6->MAX_UNICAST_SOLICIT)
365  {
366  m_nsRetransmit++;
367 
368  Ipv6Address addr;
369 
370  if (m_ipv6Address.IsLinkLocal ())
371  {
372  addr = m_ndCache->GetInterface ()->GetLinkLocalAddress ().GetAddress ();
373  }
374  else if (!m_ipv6Address.IsAny ())
375  {
376  addr = m_ndCache->GetInterface ()->GetAddressMatchingDestination (m_ipv6Address).GetAddress ();
377  if (addr.IsAny ()) /* maybe address has expired */
378  {
379  /* delete the entry */
380  m_ndCache->Remove (this);
381  return;
382  }
383  }
384  else
385  {
386  /* should not happen */
387  return;
388  }
389 
390  /* icmpv6->SendNS (m_ndCache->GetInterface ()->GetLinkLocalAddress (), m_ipv6Address, m_ipv6Address, m_ndCache->GetDevice ()->GetAddress ()); */
391  Ipv6PayloadHeaderPair p = icmpv6->ForgeNS (addr, m_ipv6Address, m_ipv6Address, m_ndCache->GetDevice ()->GetAddress ());
392  p.first->AddHeader (p.second);
393  m_ndCache->GetDevice ()->Send (p.first, this->GetMacAddress (), Ipv6L3Protocol::PROT_NUMBER);
394 
395  /* arm the timer again */
396  StartProbeTimer ();
397  }
398  else
399  {
400  /* delete the entry */
401  m_ndCache->Remove (this);
402  }
403 }
404 
406 {
407  NS_LOG_FUNCTION (this << ipv6Address);
408  m_ipv6Address = ipv6Address;
409 }
410 
412 {
414  return m_lastReachabilityConfirmation;
415 }
416 
418 {
420  if (m_nudTimer.IsRunning ())
421  {
422  m_nudTimer.Cancel ();
423  }
424 
425  m_lastReachabilityConfirmation = Simulator::Now ();
426  m_nudTimer.SetFunction (&NdiscCache::Entry::FunctionReachableTimeout, this);
427  m_nudTimer.SetDelay (MilliSeconds (Icmpv6L4Protocol::REACHABLE_TIME));
428  m_nudTimer.Schedule ();
429 }
430 
432 {
434 
435  if (m_state == REACHABLE)
436  {
437  m_lastReachabilityConfirmation = Simulator::Now ();
438  if (m_nudTimer.IsRunning ())
439  {
440  m_nudTimer.Cancel ();
441  }
442  m_nudTimer.Schedule ();
443  }
444 }
445 
447 {
449  if (m_nudTimer.IsRunning ())
450  {
451  m_nudTimer.Cancel ();
452  }
453  m_nudTimer.SetFunction (&NdiscCache::Entry::FunctionProbeTimeout, this);
454  m_nudTimer.SetDelay (MilliSeconds (Icmpv6L4Protocol::RETRANS_TIMER));
455  m_nudTimer.Schedule ();
456 }
457 
459 {
461  if (m_nudTimer.IsRunning ())
462  {
463  m_nudTimer.Cancel ();
464  }
465  m_nudTimer.SetFunction (&NdiscCache::Entry::FunctionDelayTimeout, this);
466  m_nudTimer.SetDelay (Seconds (Icmpv6L4Protocol::DELAY_FIRST_PROBE_TIME));
467  m_nudTimer.Schedule ();
468 }
469 
471 {
473  if (m_nudTimer.IsRunning ())
474  {
475  m_nudTimer.Cancel ();
476  }
477  m_nudTimer.SetFunction (&NdiscCache::Entry::FunctionRetransmitTimeout, this);
478  m_nudTimer.SetDelay (MilliSeconds (Icmpv6L4Protocol::RETRANS_TIMER));
479  m_nudTimer.Schedule ();
480 }
481 
483 {
485  m_nudTimer.Cancel ();
486  m_nsRetransmit = 0;
487 }
488 
490 {
491  NS_LOG_FUNCTION (this << p.second << p.first);
492  m_state = INCOMPLETE;
493 
494  if (p.first)
495  {
496  m_waiting.push_back (p);
497  }
498 }
499 
500 std::list<NdiscCache::Ipv6PayloadHeaderPair> NdiscCache::Entry::MarkReachable (Address mac)
501 {
502  NS_LOG_FUNCTION (this << mac);
503  m_state = REACHABLE;
504  m_macAddress = mac;
505  return m_waiting;
506 }
507 
509 {
511  m_state = PROBE;
512 }
513 
515 {
517  m_state = STALE;
518 }
519 
521 {
523  m_state = REACHABLE;
524 }
525 
526 std::list<NdiscCache::Ipv6PayloadHeaderPair> NdiscCache::Entry::MarkStale (Address mac)
527 {
528  NS_LOG_FUNCTION (this << mac);
529  m_state = STALE;
530  m_macAddress = mac;
531  return m_waiting;
532 }
533 
535 {
537  m_state = DELAY;
538 }
539 
541 {
543  StopNudTimer ();
544  m_state = PERMANENT;
545 }
546 
548 {
550  return (m_state == STALE);
551 }
552 
554 {
556  return (m_state == REACHABLE);
557 }
558 
560 {
562  return (m_state == DELAY);
563 }
564 
566 {
568  return (m_state == INCOMPLETE);
569 }
570 
572 {
574  return (m_state == PROBE);
575 }
576 
578 {
580  return (m_state == PERMANENT);
581 }
582 
584 {
586  return m_macAddress;
587 }
588 
590 {
591  NS_LOG_FUNCTION (this << mac << int(m_state));
592  m_macAddress = mac;
593 }
594 
595 } /* namespace ns3 */
596 
bool IsAny() const
If the IPv6 address is the "Any" address.
void FunctionDelayTimeout()
Function called when delay timer timeout.
Definition: ndisc-cache.cc:321
static TypeId GetTypeId()
Get the type ID.
Definition: ndisc-cache.cc:38
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
Time GetLastReachabilityConfirmation() const
Get the time of last reachability confirmation.
Definition: ndisc-cache.cc:411
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
~NdiscCache()
Destructor.
Definition: ndisc-cache.cc:57
A simple Timer class.
Definition: timer.h:73
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
Address GetMacAddress() const
Get the MAC address of this entry.
Definition: ndisc-cache.cc:583
NdiscCache::Entry * Add(Ipv6Address to)
Add an entry.
Definition: ndisc-cache.cc:120
void SetUnresQlen(uint32_t unresQlen)
Set the max number of waiting packet.
Definition: ndisc-cache.cc:159
void StopNudTimer()
Stop NUD timer and reset the NUD retransmission counter.
Definition: ndisc-cache.cc:482
IPv6 layer implementation.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:903
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:162
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:346
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
void MarkPermanent()
Change the state to this entry to PERMANENT.
Definition: ndisc-cache.cc:540
a polymophic address class
Definition: address.h:90
uint32_t GetUnresQlen()
Get the max number of waiting packet.
Definition: ndisc-cache.cc:165
Ptr< Ipv6Interface > GetInterface() const
Get the Ipv6Interface associated with this cache.
Definition: ndisc-cache.cc:79
bool IsDelay() const
Is the entry DELAY.
Definition: ndisc-cache.cc:559
Ptr< Ipv6Interface > m_interface
the interface.
Definition: ndisc-cache.h:440
void FunctionRetransmitTimeout()
Function called when retransmit timer timeout.
Definition: ndisc-cache.cc:271
void MarkReachable()
Changes the state to this entry to REACHABLE.
Definition: ndisc-cache.cc:520
void Flush()
Flush the cache.
Definition: ndisc-cache.cc:147
Hold an unsigned integer type.
Definition: uinteger.h:44
#define DELAY(time)
Gets the delay between a given time and the current time.
IPv6 Neighbor Discovery cache.
Definition: ndisc-cache.h:48
static const uint32_t REACHABLE_TIME
Neighbor Discovery node constants : reachable time.
tuple mac
Definition: third.py:92
void PrintNdiscCache(Ptr< OutputStreamWrapper > stream)
Print the NDISC cache entries.
Definition: ndisc-cache.cc:171
NdiscCache()
Constructor.
Definition: ndisc-cache.cc:52
void StartRetransmitTimer()
Start retransmit timer.
Definition: ndisc-cache.cc:470
void ClearWaitingPacket()
Clear the waiting packet list.
Definition: ndisc-cache.cc:258
Entry(NdiscCache *nd)
Constructor.
Definition: ndisc-cache.cc:222
bool IsProbe() const
Is the entry PROBE.
Definition: ndisc-cache.cc:571
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void StartDelayTimer()
Start delay timer.
Definition: ndisc-cache.cc:458
void StartReachableTimer()
Start the reachable timer.
Definition: ndisc-cache.cc:417
bool IsIncomplete() const
Is the entry INCOMPLETE.
Definition: ndisc-cache.cc:565
void FunctionReachableTimeout()
Function called when reachable timer timeout.
Definition: ndisc-cache.cc:265
void StartProbeTimer()
Start probe timer.
Definition: ndisc-cache.cc:446
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:224
static const uint32_t DEFAULT_UNRES_QLEN
Default value for unres qlen.
Definition: ndisc-cache.h:62
void Remove(NdiscCache::Entry *entry)
Delete an entry.
Definition: ndisc-cache.cc:131
void FunctionProbeTimeout()
Function called when probe timer timeout.
Definition: ndisc-cache.cc:358
uint32_t m_unresQlen
Max number of packet stored in m_waiting.
Definition: ndisc-cache.h:450
void UpdateReachableTimer()
Update the reachable timer.
Definition: ndisc-cache.cc:431
std::list< NdiscCache::Entry * > LookupInverse(Address dst)
Lookup in the cache for a MAC address.
Definition: ndisc-cache.cc:103
void SetMacAddress(Address mac)
Set the MAC address of this entry.
Definition: ndisc-cache.cc:589
Describes an IPv6 address.
Definition: ipv6-address.h:48
NdiscCache::Entry * Lookup(Ipv6Address dst)
Lookup in the cache.
Definition: ndisc-cache.cc:91
void MarkStale()
Changes the state to this entry to STALE.
Definition: ndisc-cache.cc:514
void DoDispose()
Dispose this object.
Definition: ndisc-cache.cc:63
void SetIpv6Address(Ipv6Address ipv6Address)
Set the IPv6 address.
Definition: ndisc-cache.cc:405
void MarkDelay()
Change the state to this entry to DELAY.
Definition: ndisc-cache.cc:534
static const uint32_t RETRANS_TIMER
Neighbor Discovery node constants : retransmission timer.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
static std::string FindName(Ptr< Object > object)
Given a pointer to an object, look to see if that object has a name associated with it and...
Definition: names.cc:743
bool IsRouter() const
If the entry is a host or a router.
Definition: ndisc-cache.cc:239
void MarkIncomplete(Ipv6PayloadHeaderPair p)
Changes the state to this entry to INCOMPLETE.
Definition: ndisc-cache.cc:489
void SetDevice(Ptr< NetDevice > device, Ptr< Ipv6Interface > interface)
Set the device and interface.
Definition: ndisc-cache.cc:72
Cache m_ndCache
A list of Entry.
Definition: ndisc-cache.h:445
bool IsPermanent() const
Is the entry PERMANENT.
Definition: ndisc-cache.cc:577
A record that holds information about a NdiscCache entry.
Definition: ndisc-cache.h:153
void SetRouter(bool router)
Set the node type.
Definition: ndisc-cache.cc:233
static const uint8_t DELAY_FIRST_PROBE_TIME
Neighbor Discovery node constants : delay for the first probe.
A base class which provides memory management and object aggregation.
Definition: object.h:87
sgi::hash_map< Ipv6Address, NdiscCache::Entry *, Ipv6AddressHash >::iterator CacheI
Neighbor Discovery Cache container iterator.
Definition: ndisc-cache.h:410
std::pair< Ptr< Packet >, Ipv6Header > Ipv6PayloadHeaderPair
Pair of a packet and an Ipv4 header.
Definition: ndisc-cache.h:146
bool IsReachable() const
Is the entry REACHABLE.
Definition: ndisc-cache.cc:553
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: uinteger.h:45
a unique identifier for an interface.
Definition: type-id.h:58
void AddWaitingPacket(Ipv6PayloadHeaderPair p)
Add a packet (or replace old value) in the queue.
Definition: ndisc-cache.cc:245
Ptr< NetDevice > GetDevice() const
Get the NetDevice associated with this cache.
Definition: ndisc-cache.cc:85
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:904
void MarkProbe()
Changes the state to this entry to PROBE.
Definition: ndisc-cache.cc:508
static const uint16_t PROT_NUMBER
The protocol number for IPv6 (0x86DD).
std::ostream * GetStream(void)
Return a pointer to an ostream previously set in the wrapper.
bool IsStale() const
Is the entry STALE.
Definition: ndisc-cache.cc:547
static Ipv6Address MakeSolicitedAddress(Ipv6Address addr)
Make the solicited IPv6 address.
Ptr< NetDevice > m_device
The NetDevice.
Definition: ndisc-cache.h:435