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;
69  m_icmpv6 = 0;
71 }
72 
74 {
75  NS_LOG_FUNCTION (this << device << interface);
76  m_device = device;
77  m_interface = interface;
78  m_icmpv6 = icmpv6;
79 }
80 
82 {
84  return m_interface;
85 }
86 
88 {
90  return m_device;
91 }
92 
94 {
95  NS_LOG_FUNCTION (this << dst);
96 
97  if (m_ndCache.find (dst) != m_ndCache.end ())
98  {
99  NdiscCache::Entry* entry = m_ndCache[dst];
100  return entry;
101  }
102  return 0;
103 }
104 
105 std::list<NdiscCache::Entry*> NdiscCache::LookupInverse (Address dst)
106 {
107  NS_LOG_FUNCTION (this << dst);
108 
109  std::list<NdiscCache::Entry *> entryList;
110  for (CacheI i = m_ndCache.begin (); i != m_ndCache.end (); i++)
111  {
112  NdiscCache::Entry *entry = (*i).second;
113  if (entry->GetMacAddress () == dst)
114  {
115  entryList.push_back (entry);
116  }
117  }
118  return entryList;
119 }
120 
121 
123 {
124  NS_LOG_FUNCTION (this << to);
125  NS_ASSERT (m_ndCache.find (to) == m_ndCache.end ());
126 
127  NdiscCache::Entry* entry = new NdiscCache::Entry (this);
128  entry->SetIpv6Address (to);
129  m_ndCache[to] = entry;
130  return entry;
131 }
132 
134 {
136 
137  for (CacheI i = m_ndCache.begin (); i != m_ndCache.end (); i++)
138  {
139  if ((*i).second == entry)
140  {
141  m_ndCache.erase (i);
142  entry->ClearWaitingPacket ();
143  delete entry;
144  return;
145  }
146  }
147 }
148 
150 {
152 
153  for (CacheI i = m_ndCache.begin (); i != m_ndCache.end (); i++)
154  {
155  delete (*i).second; /* delete the pointer NdiscCache::Entry */
156  }
157 
158  m_ndCache.erase (m_ndCache.begin (), m_ndCache.end ());
159 }
160 
161 void NdiscCache::SetUnresQlen (uint32_t unresQlen)
162 {
163  NS_LOG_FUNCTION (this << unresQlen);
164  m_unresQlen = unresQlen;
165 }
166 
168 {
170  return m_unresQlen;
171 }
172 
174 {
175  NS_LOG_FUNCTION (this << stream);
176  std::ostream* os = stream->GetStream ();
177 
178  for (CacheI i = m_ndCache.begin (); i != m_ndCache.end (); i++)
179  {
180  *os << i->first << " dev ";
181  std::string found = Names::FindName (m_device);
182  if (Names::FindName (m_device) != "")
183  {
184  *os << found;
185  }
186  else
187  {
188  *os << static_cast<int> (m_device->GetIfIndex ());
189  }
190 
191  *os << " lladdr " << i->second->GetMacAddress ();
192 
193  if (i->second->IsReachable ())
194  {
195  *os << " REACHABLE\n";
196  }
197  else if (i->second->IsDelay ())
198  {
199  *os << " DELAY\n";
200  }
201  else if (i->second->IsIncomplete ())
202  {
203  *os << " INCOMPLETE\n";
204  }
205  else if (i->second->IsProbe ())
206  {
207  *os << " PROBE\n";
208  }
209  else if (i->second->IsStale ())
210  {
211  *os << " STALE\n";
212  }
213  else if (i->second->IsPermanent ())
214  {
215  *os << " PERMANENT\n";
216  }
217  else
218  {
219  NS_FATAL_ERROR ("Test for possibly unreachable code-- please file a bug report, with a test case, if this is ever hit");
220  }
221  }
222 }
223 
225  : m_ndCache (nd),
226  m_waiting (),
227  m_router (false),
228  m_nudTimer (Timer::CANCEL_ON_DESTROY),
229  m_lastReachabilityConfirmation (Seconds (0.0)),
230  m_nsRetransmit (0)
231 {
233 }
234 
236 {
237  NS_LOG_FUNCTION (this << router);
238  m_router = router;
239 }
240 
242 {
244  return m_router;
245 }
246 
248 {
249  NS_LOG_FUNCTION (this << p.second << p.first);
250 
251  if (m_waiting.size () >= m_ndCache->GetUnresQlen ())
252  {
253  /* we store only m_unresQlen packet => first packet in first packet remove */
255  m_waiting.pop_front ();
256  }
257  m_waiting.push_back (p);
258 }
259 
261 {
264  m_waiting.clear ();
265 }
266 
268 {
270  this->MarkStale ();
271 }
272 
274 {
276  Ipv6Address addr;
277 
278  /* determine source address */
279  if (m_ipv6Address.IsLinkLocal ())
280  {
281  addr = m_ndCache->GetInterface ()->GetLinkLocalAddress ().GetAddress ();;
282  }
283  else if (!m_ipv6Address.IsAny ())
284  {
285  addr = m_ndCache->GetInterface ()->GetAddressMatchingDestination (m_ipv6Address).GetAddress ();
286 
287  if (addr.IsAny ()) /* maybe address has expired */
288  {
289  /* delete the entry */
290  m_ndCache->Remove (this);
291  return;
292  }
293  }
294 
295  if (m_nsRetransmit < m_ndCache->m_icmpv6->GetMaxMulticastSolicit ())
296  {
297  m_nsRetransmit++;
298 
299  m_ndCache->m_icmpv6->SendNS (addr, Ipv6Address::MakeSolicitedAddress (m_ipv6Address), m_ipv6Address, m_ndCache->GetDevice ()->GetAddress ());
300  /* arm the timer again */
301  StartRetransmitTimer ();
302  }
303  else
304  {
305  Ipv6PayloadHeaderPair malformedPacket = m_waiting.front ();
306  if (malformedPacket.first == 0)
307  {
308  malformedPacket.first = Create<Packet> ();
309  }
310  else
311  {
312  malformedPacket.first->AddHeader (malformedPacket.second);
313  }
314 
315  m_ndCache->m_icmpv6->SendErrorDestinationUnreachable (malformedPacket.first, addr, Icmpv6Header::ICMPV6_ADDR_UNREACHABLE);
316 
317  /* delete the entry */
318  m_ndCache->Remove (this);
319  }
320 }
321 
323 {
325  Ipv6Address addr;
326 
327  this->MarkProbe ();
328 
329  if (m_ipv6Address.IsLinkLocal ())
330  {
331  addr = m_ndCache->GetInterface ()->GetLinkLocalAddress ().GetAddress ();
332  }
333  else if (!m_ipv6Address.IsAny ())
334  {
335  addr = m_ndCache->GetInterface ()->GetAddressMatchingDestination (m_ipv6Address).GetAddress ();
336  if (addr.IsAny ()) /* maybe address has expired */
337  {
338  /* delete the entry */
339  m_ndCache->Remove (this);
340  return;
341  }
342  }
343  else
344  {
345  /* should not happen */
346  return;
347  }
348 
349  Ipv6PayloadHeaderPair p = m_ndCache->m_icmpv6->ForgeNS (addr, m_ipv6Address, m_ipv6Address, m_ndCache->GetDevice ()->GetAddress ());
350  p.first->AddHeader (p.second);
351  m_ndCache->GetDevice ()->Send (p.first, this->GetMacAddress (), Ipv6L3Protocol::PROT_NUMBER);
352 
353  m_nsRetransmit = 1;
354  StartProbeTimer ();
355 }
356 
358 {
360 
361  if (m_nsRetransmit < m_ndCache->m_icmpv6->GetMaxUnicastSolicit ())
362  {
363  m_nsRetransmit++;
364 
365  Ipv6Address addr;
366 
367  if (m_ipv6Address.IsLinkLocal ())
368  {
369  addr = m_ndCache->GetInterface ()->GetLinkLocalAddress ().GetAddress ();
370  }
371  else if (!m_ipv6Address.IsAny ())
372  {
373  addr = m_ndCache->GetInterface ()->GetAddressMatchingDestination (m_ipv6Address).GetAddress ();
374  if (addr.IsAny ()) /* maybe address has expired */
375  {
376  /* delete the entry */
377  m_ndCache->Remove (this);
378  return;
379  }
380  }
381  else
382  {
383  /* should not happen */
384  return;
385  }
386 
387  /* icmpv6->SendNS (m_ndCache->GetInterface ()->GetLinkLocalAddress (), m_ipv6Address, m_ipv6Address, m_ndCache->GetDevice ()->GetAddress ()); */
388  Ipv6PayloadHeaderPair p = m_ndCache->m_icmpv6->ForgeNS (addr, m_ipv6Address, m_ipv6Address, m_ndCache->GetDevice ()->GetAddress ());
389  p.first->AddHeader (p.second);
390  m_ndCache->GetDevice ()->Send (p.first, this->GetMacAddress (), Ipv6L3Protocol::PROT_NUMBER);
391 
392  /* arm the timer again */
393  StartProbeTimer ();
394  }
395  else
396  {
397  /* delete the entry */
398  m_ndCache->Remove (this);
399  }
400 }
401 
403 {
404  NS_LOG_FUNCTION (this << ipv6Address);
405  m_ipv6Address = ipv6Address;
406 }
407 
409 {
411  return m_lastReachabilityConfirmation;
412 }
413 
415 {
417  if (m_nudTimer.IsRunning ())
418  {
419  m_nudTimer.Cancel ();
420  }
421 
422  m_lastReachabilityConfirmation = Simulator::Now ();
423  m_nudTimer.SetFunction (&NdiscCache::Entry::FunctionReachableTimeout, this);
424  m_nudTimer.SetDelay (m_ndCache->m_icmpv6->GetReachableTime ());
425  m_nudTimer.Schedule ();
426 }
427 
429 {
431 
432  if (m_state == REACHABLE)
433  {
434  m_lastReachabilityConfirmation = Simulator::Now ();
435  if (m_nudTimer.IsRunning ())
436  {
437  m_nudTimer.Cancel ();
438  }
439  m_nudTimer.Schedule ();
440  }
441 }
442 
444 {
446  if (m_nudTimer.IsRunning ())
447  {
448  m_nudTimer.Cancel ();
449  }
450 
451  m_nudTimer.SetFunction (&NdiscCache::Entry::FunctionProbeTimeout, this);
452  m_nudTimer.SetDelay (m_ndCache->m_icmpv6->GetRetransmissionTime ());
453  m_nudTimer.Schedule ();
454 }
455 
457 {
459  if (m_nudTimer.IsRunning ())
460  {
461  m_nudTimer.Cancel ();
462  }
463 
464  m_nudTimer.SetFunction (&NdiscCache::Entry::FunctionDelayTimeout, this);
465  m_nudTimer.SetDelay (m_ndCache->m_icmpv6->GetDelayFirstProbe ());
466  m_nudTimer.Schedule ();
467 }
468 
470 {
472  if (m_nudTimer.IsRunning ())
473  {
474  m_nudTimer.Cancel ();
475  }
476 
477  m_nudTimer.SetFunction (&NdiscCache::Entry::FunctionRetransmitTimeout, this);
478  m_nudTimer.SetDelay (m_ndCache->m_icmpv6->GetRetransmissionTime ());
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:322
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:408
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:45
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:122
void SetUnresQlen(uint32_t unresQlen)
Set the max number of waiting packet.
Definition: ndisc-cache.cc:161
void StopNudTimer()
Stop NUD timer and reset the NUD retransmission counter.
Definition: ndisc-cache.cc:482
#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
Ptr< Icmpv6L4Protocol > m_icmpv6
the icmpv6 L4 protocol for this cache.
Definition: ndisc-cache.h:448
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
#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
void SetDevice(Ptr< NetDevice > device, Ptr< Ipv6Interface > interface, Ptr< Icmpv6L4Protocol > icmpv6)
Set the device and interface.
Definition: ndisc-cache.cc:73
#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:167
Ptr< Ipv6Interface > GetInterface() const
Get the Ipv6Interface associated with this cache.
Definition: ndisc-cache.cc:81
bool IsDelay() const
Is the entry DELAY.
Definition: ndisc-cache.cc:559
Ptr< Ipv6Interface > m_interface
the interface.
Definition: ndisc-cache.h:443
void FunctionRetransmitTimeout()
Function called when retransmit timer timeout.
Definition: ndisc-cache.cc:273
void MarkReachable()
Changes the state to this entry to REACHABLE.
Definition: ndisc-cache.cc:520
void Flush()
Flush the cache.
Definition: ndisc-cache.cc:149
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:49
tuple mac
Definition: third.py:92
void PrintNdiscCache(Ptr< OutputStreamWrapper > stream)
Print the NDISC cache entries.
Definition: ndisc-cache.cc:173
NdiscCache()
Constructor.
Definition: ndisc-cache.cc:52
void StartRetransmitTimer()
Start retransmit timer.
Definition: ndisc-cache.cc:469
void ClearWaitingPacket()
Clear the waiting packet list.
Definition: ndisc-cache.cc:260
Entry(NdiscCache *nd)
Constructor.
Definition: ndisc-cache.cc:224
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:456
void StartReachableTimer()
Start the reachable timer.
Definition: ndisc-cache.cc:414
bool IsIncomplete() const
Is the entry INCOMPLETE.
Definition: ndisc-cache.cc:565
void FunctionReachableTimeout()
Function called when reachable timer timeout.
Definition: ndisc-cache.cc:267
void StartProbeTimer()
Start probe timer.
Definition: ndisc-cache.cc:443
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:249
static const uint32_t DEFAULT_UNRES_QLEN
Default value for unres qlen.
Definition: ndisc-cache.h:63
void Remove(NdiscCache::Entry *entry)
Delete an entry.
Definition: ndisc-cache.cc:133
void FunctionProbeTimeout()
Function called when probe timer timeout.
Definition: ndisc-cache.cc:357
uint32_t m_unresQlen
Max number of packet stored in m_waiting.
Definition: ndisc-cache.h:458
void UpdateReachableTimer()
Update the reachable timer.
Definition: ndisc-cache.cc:428
std::list< NdiscCache::Entry * > LookupInverse(Address dst)
Lookup in the cache for a MAC address.
Definition: ndisc-cache.cc:105
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:93
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:402
void MarkDelay()
Change the state to this entry to DELAY.
Definition: ndisc-cache.cc:534
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:993
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:819
bool IsRouter() const
If the entry is a host or a router.
Definition: ndisc-cache.cc:241
void MarkIncomplete(Ipv6PayloadHeaderPair p)
Changes the state to this entry to INCOMPLETE.
Definition: ndisc-cache.cc:489
Cache m_ndCache
A list of Entry.
Definition: ndisc-cache.h:453
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:156
void SetRouter(bool router)
Set the node type.
Definition: ndisc-cache.cc:235
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:413
std::pair< Ptr< Packet >, Ipv6Header > Ipv6PayloadHeaderPair
Pair of a packet and an Ipv4 header.
Definition: ndisc-cache.h:149
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:247
Ptr< NetDevice > GetDevice() const
Get the NetDevice associated with this cache.
Definition: ndisc-cache.cc:87
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:914
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:438