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 
104 {
105  NS_LOG_FUNCTION (this << to);
106  NS_ASSERT (m_ndCache.find (to) == m_ndCache.end ());
107 
108  NdiscCache::Entry* entry = new NdiscCache::Entry (this);
109  entry->SetIpv6Address (to);
110  m_ndCache[to] = entry;
111  return entry;
112 }
113 
115 {
117 
118  for (CacheI i = m_ndCache.begin (); i != m_ndCache.end (); i++)
119  {
120  if ((*i).second == entry)
121  {
122  m_ndCache.erase (i);
123  entry->ClearWaitingPacket ();
124  delete entry;
125  return;
126  }
127  }
128 }
129 
131 {
133 
134  for (CacheI i = m_ndCache.begin (); i != m_ndCache.end (); i++)
135  {
136  delete (*i).second; /* delete the pointer NdiscCache::Entry */
137  }
138 
139  m_ndCache.erase (m_ndCache.begin (), m_ndCache.end ());
140 }
141 
142 void NdiscCache::SetUnresQlen (uint32_t unresQlen)
143 {
144  NS_LOG_FUNCTION (this << unresQlen);
145  m_unresQlen = unresQlen;
146 }
147 
149 {
151  return m_unresQlen;
152 }
153 
155 {
156  NS_LOG_FUNCTION (this << stream);
157  std::ostream* os = stream->GetStream ();
158 
159  for (CacheI i = m_ndCache.begin (); i != m_ndCache.end (); i++)
160  {
161  *os << i->first << " dev ";
162  std::string found = Names::FindName (m_device);
163  if (Names::FindName (m_device) != "")
164  {
165  *os << found;
166  }
167  else
168  {
169  *os << static_cast<int> (m_device->GetIfIndex ());
170  }
171 
172  *os << " lladdr " << i->second->GetMacAddress ();
173 
174  if (i->second->IsReachable ())
175  {
176  *os << " REACHABLE\n";
177  }
178  else if (i->second->IsDelay ())
179  {
180  *os << " DELAY\n";
181  }
182  else if (i->second->IsIncomplete ())
183  {
184  *os << " INCOMPLETE\n";
185  }
186  else if (i->second->IsProbe ())
187  {
188  *os << " PROBE\n";
189  }
190  else
191  {
192  *os << " STALE\n";
193  }
194  }
195 }
196 
198  : m_ndCache (nd),
199  m_waiting (),
200  m_router (false),
201  m_nudTimer (Timer::CANCEL_ON_DESTROY),
202  m_lastReachabilityConfirmation (Seconds (0.0)),
203  m_nsRetransmit (0)
204 {
206 }
207 
209 {
210  NS_LOG_FUNCTION (this << router);
211  m_router = router;
212 }
213 
215 {
217  return m_router;
218 }
219 
221 {
222  NS_LOG_FUNCTION (this << p);
223 
224  if (m_waiting.size () >= m_ndCache->GetUnresQlen ())
225  {
226  /* we store only m_unresQlen packet => first packet in first packet remove */
228  m_waiting.remove (0);
229  }
230  m_waiting.push_back (p);
231 }
232 
234 {
237  m_waiting.clear ();
238 }
239 
241 {
243  this->MarkStale ();
244 }
245 
247 {
249  Ptr<Icmpv6L4Protocol> icmpv6 = m_ndCache->GetDevice ()->GetNode ()->GetObject<Ipv6L3Protocol> ()->GetIcmpv6 ();
250  Ipv6Address addr;
251 
252  /* determine source address */
253  if (m_ipv6Address.IsLinkLocal ())
254  {
255  addr = m_ndCache->GetInterface ()->GetLinkLocalAddress ().GetAddress ();;
256  }
257  else if (!m_ipv6Address.IsAny ())
258  {
259  addr = m_ndCache->GetInterface ()->GetAddressMatchingDestination (m_ipv6Address).GetAddress ();
260 
261  if (addr.IsAny ()) /* maybe address has expired */
262  {
263  /* delete the entry */
264  m_ndCache->Remove (this);
265  return;
266  }
267  }
268 
269  if (m_nsRetransmit < icmpv6->MAX_MULTICAST_SOLICIT)
270  {
271  m_nsRetransmit++;
272 
273  icmpv6->SendNS (addr, Ipv6Address::MakeSolicitedAddress (m_ipv6Address), m_ipv6Address, m_ndCache->GetDevice ()->GetAddress ());
274  /* arm the timer again */
275  StartRetransmitTimer ();
276  }
277  else
278  {
279  Ptr<Packet> malformedPacket = m_waiting.front ();
280  if (malformedPacket == 0)
281  {
282  malformedPacket = Create<Packet> ();
283  }
284 
285  icmpv6->SendErrorDestinationUnreachable (malformedPacket, addr, Icmpv6Header::ICMPV6_ADDR_UNREACHABLE);
286 
287  /* delete the entry */
288  m_ndCache->Remove (this);
289  }
290 }
291 
293 {
295  Ptr<Ipv6L3Protocol> ipv6 = m_ndCache->GetDevice ()->GetNode ()->GetObject<Ipv6L3Protocol> ();
296  Ptr<Icmpv6L4Protocol> icmpv6 = ipv6->GetIcmpv6 ();
297  Ipv6Address addr;
298 
299  this->MarkProbe ();
300 
301  if (m_ipv6Address.IsLinkLocal ())
302  {
303  addr = m_ndCache->GetInterface ()->GetLinkLocalAddress ().GetAddress ();
304  }
305  else if (!m_ipv6Address.IsAny ())
306  {
307  addr = m_ndCache->GetInterface ()->GetAddressMatchingDestination (m_ipv6Address).GetAddress ();
308  if (addr.IsAny ()) /* maybe address has expired */
309  {
310  /* delete the entry */
311  m_ndCache->Remove (this);
312  return;
313  }
314  }
315  else
316  {
317  /* should not happen */
318  return;
319  }
320 
321  Ptr<Packet> p = icmpv6->ForgeNS (addr, m_ipv6Address, m_ipv6Address, m_ndCache->GetDevice ()->GetAddress ());
322  m_ndCache->GetDevice ()->Send (p, this->GetMacAddress (), Ipv6L3Protocol::PROT_NUMBER);
323 
324  m_nsRetransmit = 1;
325  StartProbeTimer ();
326 }
327 
329 {
331  Ptr<Ipv6L3Protocol> ipv6 = m_ndCache->GetDevice ()->GetNode ()->GetObject<Ipv6L3Protocol> ();
332  Ptr<Icmpv6L4Protocol> icmpv6 = ipv6->GetIcmpv6 ();
333 
334  if (m_nsRetransmit < icmpv6->MAX_UNICAST_SOLICIT)
335  {
336  m_nsRetransmit++;
337 
338  Ipv6Address addr;
339 
340  if (m_ipv6Address.IsLinkLocal ())
341  {
342  addr = m_ndCache->GetInterface ()->GetLinkLocalAddress ().GetAddress ();
343  }
344  else if (!m_ipv6Address.IsAny ())
345  {
346  addr = m_ndCache->GetInterface ()->GetAddressMatchingDestination (m_ipv6Address).GetAddress ();
347  if (addr.IsAny ()) /* maybe address has expired */
348  {
349  /* delete the entry */
350  m_ndCache->Remove (this);
351  return;
352  }
353  }
354  else
355  {
356  /* should not happen */
357  return;
358  }
359 
360  /* icmpv6->SendNS (m_ndCache->GetInterface ()->GetLinkLocalAddress (), m_ipv6Address, m_ipv6Address, m_ndCache->GetDevice ()->GetAddress ()); */
361  Ptr<Packet> p = icmpv6->ForgeNS (addr, m_ipv6Address, m_ipv6Address, m_ndCache->GetDevice ()->GetAddress ());
362  m_ndCache->GetDevice ()->Send (p, this->GetMacAddress (), Ipv6L3Protocol::PROT_NUMBER);
363 
364  /* arm the timer again */
365  StartProbeTimer ();
366  }
367  else
368  {
369  /* delete the entry */
370  m_ndCache->Remove (this);
371  }
372 }
373 
375 {
376  NS_LOG_FUNCTION (this << ipv6Address);
377  m_ipv6Address = ipv6Address;
378 }
379 
381 {
383  return m_lastReachabilityConfirmation;
384 }
385 
387 {
389 }
390 
392 {
394  if (m_nudTimer.IsRunning ())
395  {
396  m_nudTimer.Cancel ();
397  }
398 
399  m_nudTimer.SetFunction (&NdiscCache::Entry::FunctionReachableTimeout, this);
400  m_nudTimer.SetDelay (MilliSeconds (Icmpv6L4Protocol::REACHABLE_TIME));
401  m_nudTimer.Schedule ();
402 }
403 
405 {
407  if (m_nudTimer.IsRunning ())
408  {
409  m_nudTimer.Cancel ();
410  }
411  m_nudTimer.SetFunction (&NdiscCache::Entry::FunctionProbeTimeout, this);
412  m_nudTimer.SetDelay (MilliSeconds (Icmpv6L4Protocol::RETRANS_TIMER));
413  m_nudTimer.Schedule ();
414 }
415 
417 {
419  if (m_nudTimer.IsRunning ())
420  {
421  m_nudTimer.Cancel ();
422  }
423  m_nudTimer.SetFunction (&NdiscCache::Entry::FunctionDelayTimeout, this);
424  m_nudTimer.SetDelay (Seconds (Icmpv6L4Protocol::DELAY_FIRST_PROBE_TIME));
425  m_nudTimer.Schedule ();
426 }
427 
429 {
431  if (m_nudTimer.IsRunning ())
432  {
433  m_nudTimer.Cancel ();
434  }
435  m_nudTimer.SetFunction (&NdiscCache::Entry::FunctionRetransmitTimeout, this);
436  m_nudTimer.SetDelay (MilliSeconds (Icmpv6L4Protocol::RETRANS_TIMER));
437  m_nudTimer.Schedule ();
438 }
439 
441 {
443  m_nudTimer.Cancel ();
444  m_nsRetransmit = 0;
445 }
446 
448 {
449  NS_LOG_FUNCTION (this << p);
450  m_state = INCOMPLETE;
451 
452  if (p)
453  {
454  m_waiting.push_back (p);
455  }
456 }
457 
458 std::list<Ptr<Packet> > NdiscCache::Entry::MarkReachable (Address mac)
459 {
460  NS_LOG_FUNCTION (this << mac);
461  m_state = REACHABLE;
462  m_macAddress = mac;
463  return m_waiting;
464 }
465 
467 {
469  m_state = PROBE;
470 }
471 
473 {
475  m_state = STALE;
476 }
477 
479 {
481  m_state = REACHABLE;
482 }
483 
484 std::list<Ptr<Packet> > NdiscCache::Entry::MarkStale (Address mac)
485 {
486  NS_LOG_FUNCTION (this << mac);
487  m_state = STALE;
488  m_macAddress = mac;
489  return m_waiting;
490 }
491 
493 {
495  m_state = DELAY;
496 }
497 
499 {
501  return (m_state == STALE);
502 }
503 
505 {
507  return (m_state == REACHABLE);
508 }
509 
511 {
513  return (m_state == DELAY);
514 }
515 
517 {
519  return (m_state == INCOMPLETE);
520 }
521 
523 {
525  return (m_state == PROBE);
526 }
527 
529 {
531  return m_macAddress;
532 }
533 
535 {
536  NS_LOG_FUNCTION (this << mac << int(m_state));
537  m_macAddress = mac;
538 }
539 
540 } /* namespace ns3 */
541 
bool IsAny() const
If the IPv6 address is the "Any" address.
void FunctionDelayTimeout()
Function called when delay timer timeout.
Definition: ndisc-cache.cc:292
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:380
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:447
~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:528
NdiscCache::Entry * Add(Ipv6Address to)
Add an entry.
Definition: ndisc-cache.cc:103
void SetUnresQlen(uint32_t unresQlen)
Set the max number of waiting packet.
Definition: ndisc-cache.cc:142
void StopNudTimer()
Stop NUD timer and reset the NUD retransmission counter.
Definition: ndisc-cache.cc:440
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:874
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:339
#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:148
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:510
Ptr< Ipv6Interface > m_interface
the interface.
Definition: ndisc-cache.h:413
void FunctionRetransmitTimeout()
Function called when retransmit timer timeout.
Definition: ndisc-cache.cc:246
void MarkReachable()
Changes the state to this entry to REACHABLE.
Definition: ndisc-cache.cc:478
void Flush()
Flush the cache.
Definition: ndisc-cache.cc:130
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:154
NdiscCache()
Constructor.
Definition: ndisc-cache.cc:52
void StartRetransmitTimer()
Start retransmit timer.
Definition: ndisc-cache.cc:428
void ClearWaitingPacket()
Clear the waiting packet list.
Definition: ndisc-cache.cc:233
Entry(NdiscCache *nd)
Constructor.
Definition: ndisc-cache.cc:197
bool IsProbe() const
Is the entry PROBE.
Definition: ndisc-cache.cc:522
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:220
void StartDelayTimer()
Start delay timer.
Definition: ndisc-cache.cc:416
void StartReachableTimer()
Start the reachable timer.
Definition: ndisc-cache.cc:391
bool IsIncomplete() const
Is the entry INCOMPLETE.
Definition: ndisc-cache.cc:516
void FunctionReachableTimeout()
Function called when reachable timer timeout.
Definition: ndisc-cache.cc:240
void StartProbeTimer()
Start probe timer.
Definition: ndisc-cache.cc:404
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:114
void FunctionProbeTimeout()
Function called when probe timer timeout.
Definition: ndisc-cache.cc:328
uint32_t m_unresQlen
Max number of packet stored in m_waiting.
Definition: ndisc-cache.h:423
void SetMacAddress(Address mac)
Set the MAC address of this entry.
Definition: ndisc-cache.cc:534
Describes an IPv6 address.
Definition: ipv6-address.h:47
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:472
void DoDispose()
Dispose this object.
Definition: ndisc-cache.cc:63
void SetIpv6Address(Ipv6Address ipv6Address)
Set the IPv6 address.
Definition: ndisc-cache.cc:374
void MarkDelay()
Change the state to this entry to DELAY.
Definition: ndisc-cache.cc:492
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:866
void UpdateLastReachabilityconfirmation()
Update the time of last reachability confirmation.
Definition: ndisc-cache.cc:386
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:671
bool IsRouter() const
If the entry is a host or a router.
Definition: ndisc-cache.cc:214
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:418
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:208
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:383
bool IsReachable() const
Is the entry REACHABLE.
Definition: ndisc-cache.cc:504
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:57
Ptr< NetDevice > GetDevice() const
Get the NetDevice associated with this cache.
Definition: ndisc-cache.cc:85
TypeId SetParent(TypeId tid)
Definition: type-id.cc:638
void MarkProbe()
Changes the state to this entry to PROBE.
Definition: ndisc-cache.cc:466
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:498
static Ipv6Address MakeSolicitedAddress(Ipv6Address addr)
Make the solicited IPv6 address.
Ptr< NetDevice > m_device
The NetDevice.
Definition: ndisc-cache.h:408