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  NS_LOG_LOGIC ("Found an entry:" << dst << " to " << entry->GetMacAddress ());
101  return entry;
102  }
103  NS_LOG_LOGIC ("Nothing found");
104  return 0;
105 }
106 
107 std::list<NdiscCache::Entry*> NdiscCache::LookupInverse (Address dst)
108 {
109  NS_LOG_FUNCTION (this << dst);
110 
111  std::list<NdiscCache::Entry *> entryList;
112  for (CacheI i = m_ndCache.begin (); i != m_ndCache.end (); i++)
113  {
114  NdiscCache::Entry *entry = (*i).second;
115  if (entry->GetMacAddress () == dst)
116  {
117  NS_LOG_LOGIC ("Found an entry:" << (*i).first << " to " << (*i).second);
118  entryList.push_back (entry);
119  }
120  }
121  return entryList;
122 }
123 
124 
126 {
127  NS_LOG_FUNCTION (this << to);
128  NS_ASSERT (m_ndCache.find (to) == m_ndCache.end ());
129 
130  NdiscCache::Entry* entry = new NdiscCache::Entry (this);
131  entry->SetIpv6Address (to);
132  m_ndCache[to] = entry;
133  return entry;
134 }
135 
137 {
139 
140  for (CacheI i = m_ndCache.begin (); i != m_ndCache.end (); i++)
141  {
142  if ((*i).second == entry)
143  {
144  m_ndCache.erase (i);
145  entry->ClearWaitingPacket ();
146  delete entry;
147  return;
148  }
149  }
150 }
151 
153 {
155 
156  for (CacheI i = m_ndCache.begin (); i != m_ndCache.end (); i++)
157  {
158  delete (*i).second; /* delete the pointer NdiscCache::Entry */
159  }
160 
161  m_ndCache.erase (m_ndCache.begin (), m_ndCache.end ());
162 }
163 
164 void NdiscCache::SetUnresQlen (uint32_t unresQlen)
165 {
166  NS_LOG_FUNCTION (this << unresQlen);
167  m_unresQlen = unresQlen;
168 }
169 
171 {
173  return m_unresQlen;
174 }
175 
177 {
178  NS_LOG_FUNCTION (this << stream);
179  std::ostream* os = stream->GetStream ();
180 
181  for (CacheI i = m_ndCache.begin (); i != m_ndCache.end (); i++)
182  {
183  *os << i->first << " dev ";
184  std::string found = Names::FindName (m_device);
185  if (Names::FindName (m_device) != "")
186  {
187  *os << found;
188  }
189  else
190  {
191  *os << static_cast<int> (m_device->GetIfIndex ());
192  }
193 
194  *os << " lladdr " << i->second->GetMacAddress ();
195 
196  if (i->second->IsReachable ())
197  {
198  *os << " REACHABLE\n";
199  }
200  else if (i->second->IsDelay ())
201  {
202  *os << " DELAY\n";
203  }
204  else if (i->second->IsIncomplete ())
205  {
206  *os << " INCOMPLETE\n";
207  }
208  else if (i->second->IsProbe ())
209  {
210  *os << " PROBE\n";
211  }
212  else if (i->second->IsStale ())
213  {
214  *os << " STALE\n";
215  }
216  else if (i->second->IsPermanent ())
217  {
218  *os << " PERMANENT\n";
219  }
220  else
221  {
222  NS_FATAL_ERROR ("Test for possibly unreachable code-- please file a bug report, with a test case, if this is ever hit");
223  }
224  }
225 }
226 
228  : m_ndCache (nd),
229  m_waiting (),
230  m_router (false),
231  m_nudTimer (Timer::CANCEL_ON_DESTROY),
232  m_lastReachabilityConfirmation (Seconds (0.0)),
233  m_nsRetransmit (0)
234 {
236 }
237 
239 {
240  NS_LOG_FUNCTION (this << router);
241  m_router = router;
242 }
243 
245 {
247  return m_router;
248 }
249 
251 {
252  NS_LOG_FUNCTION (this << p.second << p.first);
253 
254  if (m_waiting.size () >= m_ndCache->GetUnresQlen ())
255  {
256  /* we store only m_unresQlen packet => first packet in first packet remove */
258  m_waiting.pop_front ();
259  }
260  m_waiting.push_back (p);
261 }
262 
264 {
267  m_waiting.clear ();
268 }
269 
271 {
273  this->MarkStale ();
274 }
275 
277 {
279  Ipv6Address addr;
280 
281  /* determine source address */
282  if (m_ipv6Address.IsLinkLocal ())
283  {
284  addr = m_ndCache->GetInterface ()->GetLinkLocalAddress ().GetAddress ();;
285  }
286  else if (!m_ipv6Address.IsAny ())
287  {
288  addr = m_ndCache->GetInterface ()->GetAddressMatchingDestination (m_ipv6Address).GetAddress ();
289 
290  if (addr.IsAny ()) /* maybe address has expired */
291  {
292  /* delete the entry */
293  m_ndCache->Remove (this);
294  return;
295  }
296  }
297 
298  if (m_nsRetransmit < m_ndCache->m_icmpv6->GetMaxMulticastSolicit ())
299  {
300  m_nsRetransmit++;
301 
302  m_ndCache->m_icmpv6->SendNS (addr, Ipv6Address::MakeSolicitedAddress (m_ipv6Address), m_ipv6Address, m_ndCache->GetDevice ()->GetAddress ());
303  /* arm the timer again */
304  StartRetransmitTimer ();
305  }
306  else
307  {
308  Ipv6PayloadHeaderPair malformedPacket = m_waiting.front ();
309  if (malformedPacket.first == 0)
310  {
311  malformedPacket.first = Create<Packet> ();
312  }
313  else
314  {
315  malformedPacket.first->AddHeader (malformedPacket.second);
316  }
317 
318  m_ndCache->m_icmpv6->SendErrorDestinationUnreachable (malformedPacket.first, addr, Icmpv6Header::ICMPV6_ADDR_UNREACHABLE);
319 
320  /* delete the entry */
321  m_ndCache->Remove (this);
322  }
323 }
324 
326 {
328  Ipv6Address addr;
329 
330  this->MarkProbe ();
331 
332  if (m_ipv6Address.IsLinkLocal ())
333  {
334  addr = m_ndCache->GetInterface ()->GetLinkLocalAddress ().GetAddress ();
335  }
336  else if (!m_ipv6Address.IsAny ())
337  {
338  addr = m_ndCache->GetInterface ()->GetAddressMatchingDestination (m_ipv6Address).GetAddress ();
339  if (addr.IsAny ()) /* maybe address has expired */
340  {
341  /* delete the entry */
342  m_ndCache->Remove (this);
343  return;
344  }
345  }
346  else
347  {
348  /* should not happen */
349  return;
350  }
351 
352  Ipv6PayloadHeaderPair p = m_ndCache->m_icmpv6->ForgeNS (addr, m_ipv6Address, m_ipv6Address, m_ndCache->GetDevice ()->GetAddress ());
353  p.first->AddHeader (p.second);
354  m_ndCache->GetDevice ()->Send (p.first, this->GetMacAddress (), Ipv6L3Protocol::PROT_NUMBER);
355 
356  m_nsRetransmit = 1;
357  StartProbeTimer ();
358 }
359 
361 {
363 
364  if (m_nsRetransmit < m_ndCache->m_icmpv6->GetMaxUnicastSolicit ())
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 = m_ndCache->m_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 (m_ndCache->m_icmpv6->GetReachableTime ());
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 
454  m_nudTimer.SetFunction (&NdiscCache::Entry::FunctionProbeTimeout, this);
455  m_nudTimer.SetDelay (m_ndCache->m_icmpv6->GetRetransmissionTime ());
456  m_nudTimer.Schedule ();
457 }
458 
460 {
462  if (m_nudTimer.IsRunning ())
463  {
464  m_nudTimer.Cancel ();
465  }
466 
467  m_nudTimer.SetFunction (&NdiscCache::Entry::FunctionDelayTimeout, this);
468  m_nudTimer.SetDelay (m_ndCache->m_icmpv6->GetDelayFirstProbe ());
469  m_nudTimer.Schedule ();
470 }
471 
473 {
475  if (m_nudTimer.IsRunning ())
476  {
477  m_nudTimer.Cancel ();
478  }
479 
480  m_nudTimer.SetFunction (&NdiscCache::Entry::FunctionRetransmitTimeout, this);
481  m_nudTimer.SetDelay (m_ndCache->m_icmpv6->GetRetransmissionTime ());
482  m_nudTimer.Schedule ();
483 }
484 
486 {
488  m_nudTimer.Cancel ();
489  m_nsRetransmit = 0;
490 }
491 
493 {
494  NS_LOG_FUNCTION (this << p.second << p.first);
495  m_state = INCOMPLETE;
496 
497  if (p.first)
498  {
499  m_waiting.push_back (p);
500  }
501 }
502 
503 std::list<NdiscCache::Ipv6PayloadHeaderPair> NdiscCache::Entry::MarkReachable (Address mac)
504 {
505  NS_LOG_FUNCTION (this << mac);
506  m_state = REACHABLE;
507  m_macAddress = mac;
508  return m_waiting;
509 }
510 
512 {
514  m_state = PROBE;
515 }
516 
518 {
520  m_state = STALE;
521 }
522 
524 {
526  m_state = REACHABLE;
527 }
528 
529 std::list<NdiscCache::Ipv6PayloadHeaderPair> NdiscCache::Entry::MarkStale (Address mac)
530 {
531  NS_LOG_FUNCTION (this << mac);
532  m_state = STALE;
533  m_macAddress = mac;
534  return m_waiting;
535 }
536 
538 {
540  m_state = DELAY;
541 }
542 
544 {
546  StopNudTimer ();
547  m_state = PERMANENT;
548 }
549 
551 {
553  return (m_state == STALE);
554 }
555 
557 {
559  return (m_state == REACHABLE);
560 }
561 
563 {
565  return (m_state == DELAY);
566 }
567 
569 {
571  return (m_state == INCOMPLETE);
572 }
573 
575 {
577  return (m_state == PROBE);
578 }
579 
581 {
583  return (m_state == PERMANENT);
584 }
585 
587 {
589  return m_macAddress;
590 }
591 
593 {
594  NS_LOG_FUNCTION (this << mac << int(m_state));
595  m_macAddress = mac;
596 }
597 
598 } /* namespace ns3 */
599 
Address GetMacAddress() const
Get the MAC address of this entry.
Definition: ndisc-cache.cc:586
void FunctionDelayTimeout()
Function called when delay timer timeout.
Definition: ndisc-cache.cc:325
static TypeId GetTypeId()
Get the type ID.
Definition: ndisc-cache.cc:38
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
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 "...
bool IsReachable() const
Is the entry REACHABLE.
Definition: ndisc-cache.cc:556
~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
NdiscCache::Entry * Add(Ipv6Address to)
Add an entry.
Definition: ndisc-cache.cc:125
void SetUnresQlen(uint32_t unresQlen)
Set the max number of waiting packet.
Definition: ndisc-cache.cc:164
void StopNudTimer()
Stop NUD timer and reset the NUD retransmission counter.
Definition: ndisc-cache.cc:485
#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:202
bool IsIncomplete() const
Is the entry INCOMPLETE.
Definition: ndisc-cache.cc:568
#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.
Time GetLastReachabilityConfirmation() const
Get the time of last reachability confirmation.
Definition: ndisc-cache.cc:411
void MarkPermanent()
Change the state to this entry to PERMANENT.
Definition: ndisc-cache.cc:543
a polymophic address class
Definition: address.h:90
uint32_t GetUnresQlen()
Get the max number of waiting packet.
Definition: ndisc-cache.cc:170
Ptr< NetDevice > GetDevice() const
Get the NetDevice associated with this cache.
Definition: ndisc-cache.cc:87
Ptr< Ipv6Interface > m_interface
the interface.
Definition: ndisc-cache.h:443
void FunctionRetransmitTimeout()
Function called when retransmit timer timeout.
Definition: ndisc-cache.cc:276
void MarkReachable()
Changes the state to this entry to REACHABLE.
Definition: ndisc-cache.cc:523
void Flush()
Flush the cache.
Definition: ndisc-cache.cc:152
Hold an unsigned integer type.
Definition: uinteger.h:44
#define DELAY(time)
Gets the delay between a given time and the current time.
mac
Definition: third.py:92
IPv6 Neighbor Discovery cache.
Definition: ndisc-cache.h:49
Ptr< Ipv6Interface > GetInterface() const
Get the Ipv6Interface associated with this cache.
Definition: ndisc-cache.cc:81
void PrintNdiscCache(Ptr< OutputStreamWrapper > stream)
Print the NDISC cache entries.
Definition: ndisc-cache.cc:176
NdiscCache()
Constructor.
Definition: ndisc-cache.cc:52
void StartRetransmitTimer()
Start retransmit timer.
Definition: ndisc-cache.cc:472
void ClearWaitingPacket()
Clear the waiting packet list.
Definition: ndisc-cache.cc:263
Entry(NdiscCache *nd)
Constructor.
Definition: ndisc-cache.cc:227
bool IsStale() const
Is the entry STALE.
Definition: ndisc-cache.cc:550
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void StartDelayTimer()
Start delay timer.
Definition: ndisc-cache.cc:459
bool IsRouter() const
If the entry is a host or a router.
Definition: ndisc-cache.cc:244
void StartReachableTimer()
Start the reachable timer.
Definition: ndisc-cache.cc:417
void FunctionReachableTimeout()
Function called when reachable timer timeout.
Definition: ndisc-cache.cc:270
void StartProbeTimer()
Start probe timer.
Definition: ndisc-cache.cc:446
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:136
void FunctionProbeTimeout()
Function called when probe timer timeout.
Definition: ndisc-cache.cc:360
NS_LOG_LOGIC("Net device "<< nd<< " is not bridged")
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:431
std::list< NdiscCache::Entry * > LookupInverse(Address dst)
Lookup in the cache for a MAC address.
Definition: ndisc-cache.cc:107
void SetMacAddress(Address mac)
Set the MAC address of this entry.
Definition: ndisc-cache.cc:592
Describes an IPv6 address.
Definition: ipv6-address.h:49
bool IsProbe() const
Is the entry PROBE.
Definition: ndisc-cache.cc:574
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:517
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:537
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1014
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
void MarkIncomplete(Ipv6PayloadHeaderPair p)
Changes the state to this entry to INCOMPLETE.
Definition: ndisc-cache.cc:492
Cache m_ndCache
A list of Entry.
Definition: ndisc-cache.h:453
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:238
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 IsDelay() const
Is the entry DELAY.
Definition: ndisc-cache.cc:562
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
bool IsPermanent() const
Is the entry PERMANENT.
Definition: ndisc-cache.cc:580
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:250
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:915
void MarkProbe()
Changes the state to this entry to PROBE.
Definition: ndisc-cache.cc:511
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 IsAny() const
If the IPv6 address is the "Any" address.
static Ipv6Address MakeSolicitedAddress(Ipv6Address addr)
Make the solicited IPv6 address.
Ptr< NetDevice > m_device
The NetDevice.
Definition: ndisc-cache.h:438