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  .AddAttribute ("UnresolvedQueueSize",
43  "Size of the queue for packets pending an NA reply.",
46  MakeUintegerChecker<uint32_t> ())
47  ;
48  return tid;
49 }
50 
52 {
54 }
55 
57 {
59  Flush ();
60 }
61 
63 {
65  Flush ();
66  m_device = 0;
67  m_interface = 0;
69 }
70 
72 {
73  NS_LOG_FUNCTION (this << device << interface);
74  m_device = device;
75  m_interface = interface;
76 }
77 
79 {
81  return m_interface;
82 }
83 
85 {
87  return m_device;
88 }
89 
91 {
92  NS_LOG_FUNCTION (this << dst);
93 
94  if (m_ndCache.find (dst) != m_ndCache.end ())
95  {
96  NdiscCache::Entry* entry = m_ndCache[dst];
97  return entry;
98  }
99  return 0;
100 }
101 
103 {
104  NS_LOG_FUNCTION (this << to);
105  NS_ASSERT (m_ndCache.find (to) == m_ndCache.end ());
106 
107  NdiscCache::Entry* entry = new NdiscCache::Entry (this);
108  entry->SetIpv6Address (to);
109  m_ndCache[to] = entry;
110  return entry;
111 }
112 
114 {
116 
117  for (CacheI i = m_ndCache.begin (); i != m_ndCache.end (); i++)
118  {
119  if ((*i).second == entry)
120  {
121  m_ndCache.erase (i);
122  entry->ClearWaitingPacket ();
123  delete entry;
124  return;
125  }
126  }
127 }
128 
130 {
132 
133  for (CacheI i = m_ndCache.begin (); i != m_ndCache.end (); i++)
134  {
135  delete (*i).second; /* delete the pointer NdiscCache::Entry */
136  }
137 
138  m_ndCache.erase (m_ndCache.begin (), m_ndCache.end ());
139 }
140 
141 void NdiscCache::SetUnresQlen (uint32_t unresQlen)
142 {
143  NS_LOG_FUNCTION (this << unresQlen);
144  m_unresQlen = unresQlen;
145 }
146 
148 {
150  return m_unresQlen;
151 }
152 
154 {
155  NS_LOG_FUNCTION (this << stream);
156  std::ostream* os = stream->GetStream ();
157 
158  for (CacheI i = m_ndCache.begin (); i != m_ndCache.end (); i++)
159  {
160  *os << i->first << " dev ";
161  std::string found = Names::FindName (m_device);
162  if (Names::FindName (m_device) != "")
163  {
164  *os << found;
165  }
166  else
167  {
168  *os << static_cast<int> (m_device->GetIfIndex ());
169  }
170 
171  *os << " lladdr " << i->second->GetMacAddress ();
172 
173  if (i->second->IsReachable ())
174  {
175  *os << " REACHABLE\n";
176  }
177  else if (i->second->IsDelay ())
178  {
179  *os << " DELAY\n";
180  }
181  else if (i->second->IsIncomplete ())
182  {
183  *os << " INCOMPLETE\n";
184  }
185  else if (i->second->IsProbe ())
186  {
187  *os << " PROBE\n";
188  }
189  else
190  {
191  *os << " STALE\n";
192  }
193  }
194 }
195 
197  : m_ndCache (nd),
198  m_waiting (),
199  m_router (false),
200  m_reachableTimer (Timer::CANCEL_ON_DESTROY),
201  m_retransTimer (Timer::CANCEL_ON_DESTROY),
202  m_probeTimer (Timer::CANCEL_ON_DESTROY),
203  m_delayTimer (Timer::CANCEL_ON_DESTROY),
204  m_lastReachabilityConfirmation (Seconds (0.0)),
205  m_nsRetransmit (0)
206 {
208 }
209 
211 {
212  NS_LOG_FUNCTION (this << router);
213  m_router = router;
214 }
215 
217 {
219  return m_router;
220 }
221 
223 {
224  NS_LOG_FUNCTION (this << p);
225 
226  if (m_waiting.size () >= m_ndCache->GetUnresQlen ())
227  {
228  /* we store only m_unresQlen packet => first packet in first packet remove */
230  m_waiting.remove (0);
231  }
232  m_waiting.push_back (p);
233 }
234 
236 {
239  m_waiting.clear ();
240 }
241 
243 {
245  this->MarkStale ();
246 }
247 
249 {
251  Ptr<Icmpv6L4Protocol> icmpv6 = m_ndCache->GetDevice ()->GetNode ()->GetObject<Ipv6L3Protocol> ()->GetIcmpv6 ();
252  Ipv6Address addr;
253 
254  /* determine source address */
255  if (m_ipv6Address.IsLinkLocal ())
256  {
257  addr = m_ndCache->GetInterface ()->GetLinkLocalAddress ().GetAddress ();;
258  }
259  else if (!m_ipv6Address.IsAny ())
260  {
261  addr = m_ndCache->GetInterface ()->GetAddressMatchingDestination (m_ipv6Address).GetAddress ();
262 
263  if (addr.IsAny ()) /* maybe address has expired */
264  {
265  /* delete the entry */
266  m_ndCache->Remove (this);
267  return;
268  }
269  }
270 
271  if (GetNSRetransmit () < icmpv6->MAX_MULTICAST_SOLICIT)
272  {
273  IncNSRetransmit ();
274 
275  icmpv6->SendNS (addr, Ipv6Address::MakeSolicitedAddress (m_ipv6Address), m_ipv6Address, m_ndCache->GetDevice ()->GetAddress ());
276  /* arm the timer again */
277  StartRetransmitTimer ();
278  }
279  else
280  {
281  Ptr<Packet> malformedPacket = m_waiting.front ();
282  if (malformedPacket == 0)
283  {
284  malformedPacket = Create<Packet> ();
285  }
286 
287  icmpv6->SendErrorDestinationUnreachable (malformedPacket, addr, Icmpv6Header::ICMPV6_ADDR_UNREACHABLE);
288 
289  /* delete the entry */
290  m_ndCache->Remove (this);
291  }
292 }
293 
295 {
297  Ptr<Ipv6L3Protocol> ipv6 = m_ndCache->GetDevice ()->GetNode ()->GetObject<Ipv6L3Protocol> ();
298  Ptr<Icmpv6L4Protocol> icmpv6 = ipv6->GetIcmpv6 ();
299  Ipv6Address addr;
300 
301  this->MarkProbe ();
302 
303  if (m_ipv6Address.IsLinkLocal ())
304  {
305  addr = m_ndCache->GetInterface ()->GetLinkLocalAddress ().GetAddress ();
306  }
307  else if (!m_ipv6Address.IsAny ())
308  {
309  addr = m_ndCache->GetInterface ()->GetAddressMatchingDestination (m_ipv6Address).GetAddress ();
310  if (addr.IsAny ()) /* maybe address has expired */
311  {
312  /* delete the entry */
313  m_ndCache->Remove (this);
314  return;
315  }
316  }
317  else
318  {
319  /* should not happen */
320  return;
321  }
322 
323  Ptr<Packet> p = icmpv6->ForgeNS (addr, m_ipv6Address, m_ipv6Address, m_ndCache->GetDevice ()->GetAddress ());
324  m_ndCache->GetDevice ()->Send (p, this->GetMacAddress (), Ipv6L3Protocol::PROT_NUMBER);
325 
326  ResetNSRetransmit ();
327  IncNSRetransmit ();
328  StartProbeTimer ();
329 }
330 
332 {
334  Ptr<Ipv6L3Protocol> ipv6 = m_ndCache->GetDevice ()->GetNode ()->GetObject<Ipv6L3Protocol> ();
335  Ptr<Icmpv6L4Protocol> icmpv6 = ipv6->GetIcmpv6 ();
336 
337  if (GetNSRetransmit () < icmpv6->MAX_UNICAST_SOLICIT)
338  {
339  Ipv6Address addr;
340 
341  if (m_ipv6Address.IsLinkLocal ())
342  {
343  addr = m_ndCache->GetInterface ()->GetLinkLocalAddress ().GetAddress ();
344  }
345  else if (!m_ipv6Address.IsAny ())
346  {
347  addr = m_ndCache->GetInterface ()->GetAddressMatchingDestination (m_ipv6Address).GetAddress ();
348  if (addr.IsAny ()) /* maybe address has expired */
349  {
350  /* delete the entry */
351  m_ndCache->Remove (this);
352  return;
353  }
354  }
355  else
356  {
357  /* should not happen */
358  return;
359  }
360 
361  IncNSRetransmit ();
362  /* icmpv6->SendNS (m_ndCache->GetInterface ()->GetLinkLocalAddress (), m_ipv6Address, m_ipv6Address, m_ndCache->GetDevice ()->GetAddress ()); */
363  Ptr<Packet> p = icmpv6->ForgeNS (addr, m_ipv6Address, m_ipv6Address, m_ndCache->GetDevice ()->GetAddress ());
364  m_ndCache->GetDevice ()->Send (p, this->GetMacAddress (), Ipv6L3Protocol::PROT_NUMBER);
365 
366  /* arm the timer again */
367  StartProbeTimer ();
368  }
369  else
370  {
371  /* delete the entry */
372  m_ndCache->Remove (this);
373  }
374 }
375 
377 {
378  NS_LOG_FUNCTION (this << ipv6Address);
379  m_ipv6Address = ipv6Address;
380 }
381 
383 {
385  return m_nsRetransmit;
386 }
387 
389 {
391  m_nsRetransmit++;
392 }
393 
395 {
397  m_nsRetransmit = 0;
398 }
399 
401 {
403  return m_lastReachabilityConfirmation;
404 }
405 
407 {
409 }
410 
412 {
414  if (m_reachableTimer.IsRunning ())
415  {
416  m_reachableTimer.Cancel ();
417  }
418  m_reachableTimer.SetFunction (&NdiscCache::Entry::FunctionReachableTimeout, this);
419  m_reachableTimer.SetDelay (MilliSeconds (Icmpv6L4Protocol::REACHABLE_TIME));
420  m_reachableTimer.Schedule ();
421 }
422 
424 {
426  m_reachableTimer.Cancel ();
427 }
428 
430 {
432  if (m_probeTimer.IsRunning ())
433  {
434  m_probeTimer.Cancel ();
435  }
436  m_probeTimer.SetFunction (&NdiscCache::Entry::FunctionProbeTimeout, this);
437  m_probeTimer.SetDelay (MilliSeconds (Icmpv6L4Protocol::RETRANS_TIMER));
438  m_probeTimer.Schedule ();
439 }
440 
442 {
444  m_probeTimer.Cancel ();
445  ResetNSRetransmit ();
446 }
447 
448 
450 {
452  if (m_delayTimer.IsRunning ())
453  {
454  m_delayTimer.Cancel ();
455  }
456  m_delayTimer.SetFunction (&NdiscCache::Entry::FunctionDelayTimeout, this);
457  m_delayTimer.SetDelay (Seconds (Icmpv6L4Protocol::DELAY_FIRST_PROBE_TIME));
458  m_delayTimer.Schedule ();
459 }
460 
462 {
464  m_delayTimer.Cancel ();
465  ResetNSRetransmit ();
466 }
467 
469 {
471  if (m_retransTimer.IsRunning ())
472  {
473  m_retransTimer.Cancel ();
474  }
475  m_retransTimer.SetFunction (&NdiscCache::Entry::FunctionRetransmitTimeout, this);
476  m_retransTimer.SetDelay (MilliSeconds (Icmpv6L4Protocol::RETRANS_TIMER));
477  m_retransTimer.Schedule ();
478 }
479 
481 {
483  m_retransTimer.Cancel ();
484  ResetNSRetransmit ();
485 }
486 
488 {
489  NS_LOG_FUNCTION (this << p);
490  m_state = INCOMPLETE;
491 
492  if (p)
493  {
494  m_waiting.push_back (p);
495  }
496 }
497 
498 std::list<Ptr<Packet> > NdiscCache::Entry::MarkReachable (Address mac)
499 {
500  NS_LOG_FUNCTION (this << mac);
501  m_state = REACHABLE;
502  m_macAddress = mac;
503  return m_waiting;
504 }
505 
507 {
509  m_state = PROBE;
510 }
511 
513 {
515  m_state = STALE;
516 }
517 
519 {
521  m_state = REACHABLE;
522 }
523 
524 std::list<Ptr<Packet> > NdiscCache::Entry::MarkStale (Address mac)
525 {
526  NS_LOG_FUNCTION (this << mac);
527  m_state = STALE;
528  m_macAddress = mac;
529  return m_waiting;
530 }
531 
533 {
535  m_state = DELAY;
536 }
537 
539 {
541  return (m_state == STALE);
542 }
543 
545 {
547  return (m_state == REACHABLE);
548 }
549 
551 {
553  return (m_state == DELAY);
554 }
555 
557 {
559  return (m_state == INCOMPLETE);
560 }
561 
563 {
565  return (m_state == PROBE);
566 }
567 
569 {
571  return m_macAddress;
572 }
573 
575 {
576  NS_LOG_FUNCTION (this << mac);
577  m_macAddress = mac;
578 }
579 
580 } /* namespace ns3 */
581 
bool IsAny() const
If the IPv6 address is the "Any" address.
void FunctionDelayTimeout()
Function called when delay timer timeout.
Definition: ndisc-cache.cc:294
static TypeId GetTypeId()
Get the type ID.
Definition: ndisc-cache.cc:38
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:95
Time GetLastReachabilityConfirmation() const
Get the time of last reachability confirmation.
Definition: ndisc-cache.cc:400
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 "...
void MarkIncomplete(Ptr< Packet > p)
Changes the state to this entry to INCOMPLETE.
Definition: ndisc-cache.cc:487
~NdiscCache()
Destructor.
Definition: ndisc-cache.cc:56
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:568
NdiscCache::Entry * Add(Ipv6Address to)
Add an entry.
Definition: ndisc-cache.cc:102
void SetUnresQlen(uint32_t unresQlen)
Set the max number of waiting packet.
Definition: ndisc-cache.cc:141
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:61
#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:867
void StopReachableTimer()
Stop the reachable timer.
Definition: ndisc-cache.cc:423
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:338
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
a polymophic address class
Definition: address.h:90
uint32_t GetUnresQlen()
Get the max number of waiting packet.
Definition: ndisc-cache.cc:147
Ptr< Ipv6Interface > GetInterface() const
Get the Ipv6Interface associated with this cache.
Definition: ndisc-cache.cc:78
bool IsDelay() const
Is the entry DELAY.
Definition: ndisc-cache.cc:550
Ptr< Ipv6Interface > m_interface
the interface.
Definition: ndisc-cache.h:459
void FunctionRetransmitTimeout()
Function called when retransmit timer timeout.
Definition: ndisc-cache.cc:248
void MarkReachable()
Changes the state to this entry to REACHABLE.
Definition: ndisc-cache.cc:518
void Flush()
Flush the cache.
Definition: ndisc-cache.cc:129
Hold an unsigned integer type.
Definition: uinteger.h:44
#define DELAY(time)
Gets the delay between a given time and the current time.
Introspection did not find any typical Config paths.
Definition: ndisc-cache.h:46
static const uint32_t REACHABLE_TIME
Neighbor Discovery node constants : reachable time.
void PrintNdiscCache(Ptr< OutputStreamWrapper > stream)
Print the NDISC cache entries.
Definition: ndisc-cache.cc:153
void StopProbeTimer()
Stop probe timer.
Definition: ndisc-cache.cc:441
NdiscCache()
Constructor.
Definition: ndisc-cache.cc:51
void StartRetransmitTimer()
Start retransmit timer.
Definition: ndisc-cache.cc:468
void ClearWaitingPacket()
Clear the waiting packet list.
Definition: ndisc-cache.cc:235
Entry(NdiscCache *nd)
Constructor.
Definition: ndisc-cache.cc:196
bool IsProbe() const
Is the entry PROBE.
Definition: ndisc-cache.cc:562
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void AddWaitingPacket(Ptr< Packet > p)
Add a packet (or replace old value) in the queue.
Definition: ndisc-cache.cc:222
void StartDelayTimer()
Start delay timer.
Definition: ndisc-cache.cc:449
void StartReachableTimer()
Start the reachable timer.
Definition: ndisc-cache.cc:411
bool IsIncomplete() const
Is the entry INCOMPLETE.
Definition: ndisc-cache.cc:556
void FunctionReachableTimeout()
Function called when reachable timer timeout.
Definition: ndisc-cache.cc:242
void StartProbeTimer()
Start probe timer.
Definition: ndisc-cache.cc:429
static const uint32_t DEFAULT_UNRES_QLEN
Default value for unres qlen.
Definition: ndisc-cache.h:60
void Remove(NdiscCache::Entry *entry)
Delete an entry.
Definition: ndisc-cache.cc:113
void FunctionProbeTimeout()
Function called when probe timer timeout.
Definition: ndisc-cache.cc:331
uint32_t m_unresQlen
Max number of packet stored in m_waiting.
Definition: ndisc-cache.h:469
void SetMacAddress(Address mac)
Set the MAC address of this entry.
Definition: ndisc-cache.cc:574
Describes an IPv6 address.
Definition: ipv6-address.h:47
NdiscCache::Entry * Lookup(Ipv6Address dst)
Lookup in the cache.
Definition: ndisc-cache.cc:90
void MarkStale()
Changes the state to this entry to STALE.
Definition: ndisc-cache.cc:512
void IncNSRetransmit()
Increment NS retransmit.
Definition: ndisc-cache.cc:388
void DoDispose()
Dispose this object.
Definition: ndisc-cache.cc:62
void SetIpv6Address(Ipv6Address ipv6Address)
Set the IPv6 address.
Definition: ndisc-cache.cc:376
void MarkDelay()
Change the state to this entry to DELAY.
Definition: ndisc-cache.cc:532
static const uint32_t RETRANS_TIMER
Neighbor Discovery node constants : retransmission timer.
void StopRetransmitTimer()
Stop retransmit timer.
Definition: ndisc-cache.cc:480
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:859
void UpdateLastReachabilityconfirmation()
Update the time of last reachability confirmation.
Definition: ndisc-cache.cc:406
void StopDelayTimer()
Stop delay timer.
Definition: ndisc-cache.cc:461
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:664
bool IsRouter() const
If the entry is a host or a router.
Definition: ndisc-cache.cc:216
void SetDevice(Ptr< NetDevice > device, Ptr< Ipv6Interface > interface)
Set the device and interface.
Definition: ndisc-cache.cc:71
Cache m_ndCache
A list of Entry.
Definition: ndisc-cache.h:464
A record that holds information about an NdiscCache entry.
Definition: ndisc-cache.h:138
void SetRouter(bool router)
Set the node type.
Definition: ndisc-cache.cc:210
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:429
bool IsReachable() const
Is the entry REACHABLE.
Definition: ndisc-cache.cc:544
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:51
void ResetNSRetransmit()
Reset NS retransmit (=0).
Definition: ndisc-cache.cc:394
Ptr< NetDevice > GetDevice() const
Get the NetDevice associated with this cache.
Definition: ndisc-cache.cc:84
TypeId SetParent(TypeId tid)
Definition: type-id.cc:631
void MarkProbe()
Changes the state to this entry to PROBE.
Definition: ndisc-cache.cc:506
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:538
static Ipv6Address MakeSolicitedAddress(Ipv6Address addr)
Make the solicited IPv6 address.
Ptr< NetDevice > m_device
The NetDevice.
Definition: ndisc-cache.h:454
uint8_t GetNSRetransmit() const
Get the number of NS retransmit.
Definition: ndisc-cache.cc:382