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 if (i->second->IsStale ())
191  {
192  *os << " STALE\n";
193  }
194  else if (i->second->IsPermanent ())
195  {
196  *os << " PERMANENT\n";
197  }
198  else
199  {
200  NS_FATAL_ERROR ("Test for possibly unreachable code-- please file a bug report, with a test case, if this is ever hit");
201  }
202  }
203 }
204 
206  : m_ndCache (nd),
207  m_waiting (),
208  m_router (false),
209  m_nudTimer (Timer::CANCEL_ON_DESTROY),
210  m_lastReachabilityConfirmation (Seconds (0.0)),
211  m_nsRetransmit (0)
212 {
214 }
215 
217 {
218  NS_LOG_FUNCTION (this << router);
219  m_router = router;
220 }
221 
223 {
225  return m_router;
226 }
227 
229 {
230  NS_LOG_FUNCTION (this << p.second << p.first);
231 
232  if (m_waiting.size () >= m_ndCache->GetUnresQlen ())
233  {
234  /* we store only m_unresQlen packet => first packet in first packet remove */
236  m_waiting.pop_front ();
237  }
238  m_waiting.push_back (p);
239 }
240 
242 {
245  m_waiting.clear ();
246 }
247 
249 {
251  this->MarkStale ();
252 }
253 
255 {
257  Ptr<Icmpv6L4Protocol> icmpv6 = m_ndCache->GetDevice ()->GetNode ()->GetObject<Ipv6L3Protocol> ()->GetIcmpv6 ();
258  Ipv6Address addr;
259 
260  /* determine source address */
261  if (m_ipv6Address.IsLinkLocal ())
262  {
263  addr = m_ndCache->GetInterface ()->GetLinkLocalAddress ().GetAddress ();;
264  }
265  else if (!m_ipv6Address.IsAny ())
266  {
267  addr = m_ndCache->GetInterface ()->GetAddressMatchingDestination (m_ipv6Address).GetAddress ();
268 
269  if (addr.IsAny ()) /* maybe address has expired */
270  {
271  /* delete the entry */
272  m_ndCache->Remove (this);
273  return;
274  }
275  }
276 
277  if (m_nsRetransmit < icmpv6->MAX_MULTICAST_SOLICIT)
278  {
279  m_nsRetransmit++;
280 
281  icmpv6->SendNS (addr, Ipv6Address::MakeSolicitedAddress (m_ipv6Address), m_ipv6Address, m_ndCache->GetDevice ()->GetAddress ());
282  /* arm the timer again */
283  StartRetransmitTimer ();
284  }
285  else
286  {
287  Ipv6PayloadHeaderPair malformedPacket = m_waiting.front ();
288  if (malformedPacket.first == 0)
289  {
290  malformedPacket.first = Create<Packet> ();
291  }
292  else
293  {
294  malformedPacket.first->AddHeader (malformedPacket.second);
295  }
296 
297  icmpv6->SendErrorDestinationUnreachable (malformedPacket.first, addr, Icmpv6Header::ICMPV6_ADDR_UNREACHABLE);
298 
299  /* delete the entry */
300  m_ndCache->Remove (this);
301  }
302 }
303 
305 {
307  Ptr<Ipv6L3Protocol> ipv6 = m_ndCache->GetDevice ()->GetNode ()->GetObject<Ipv6L3Protocol> ();
308  Ptr<Icmpv6L4Protocol> icmpv6 = ipv6->GetIcmpv6 ();
309  Ipv6Address addr;
310 
311  this->MarkProbe ();
312 
313  if (m_ipv6Address.IsLinkLocal ())
314  {
315  addr = m_ndCache->GetInterface ()->GetLinkLocalAddress ().GetAddress ();
316  }
317  else if (!m_ipv6Address.IsAny ())
318  {
319  addr = m_ndCache->GetInterface ()->GetAddressMatchingDestination (m_ipv6Address).GetAddress ();
320  if (addr.IsAny ()) /* maybe address has expired */
321  {
322  /* delete the entry */
323  m_ndCache->Remove (this);
324  return;
325  }
326  }
327  else
328  {
329  /* should not happen */
330  return;
331  }
332 
333  Ipv6PayloadHeaderPair p = icmpv6->ForgeNS (addr, m_ipv6Address, m_ipv6Address, m_ndCache->GetDevice ()->GetAddress ());
334  p.first->AddHeader (p.second);
335  m_ndCache->GetDevice ()->Send (p.first, this->GetMacAddress (), Ipv6L3Protocol::PROT_NUMBER);
336 
337  m_nsRetransmit = 1;
338  StartProbeTimer ();
339 }
340 
342 {
344  Ptr<Ipv6L3Protocol> ipv6 = m_ndCache->GetDevice ()->GetNode ()->GetObject<Ipv6L3Protocol> ();
345  Ptr<Icmpv6L4Protocol> icmpv6 = ipv6->GetIcmpv6 ();
346 
347  if (m_nsRetransmit < icmpv6->MAX_UNICAST_SOLICIT)
348  {
349  m_nsRetransmit++;
350 
351  Ipv6Address addr;
352 
353  if (m_ipv6Address.IsLinkLocal ())
354  {
355  addr = m_ndCache->GetInterface ()->GetLinkLocalAddress ().GetAddress ();
356  }
357  else if (!m_ipv6Address.IsAny ())
358  {
359  addr = m_ndCache->GetInterface ()->GetAddressMatchingDestination (m_ipv6Address).GetAddress ();
360  if (addr.IsAny ()) /* maybe address has expired */
361  {
362  /* delete the entry */
363  m_ndCache->Remove (this);
364  return;
365  }
366  }
367  else
368  {
369  /* should not happen */
370  return;
371  }
372 
373  /* icmpv6->SendNS (m_ndCache->GetInterface ()->GetLinkLocalAddress (), m_ipv6Address, m_ipv6Address, m_ndCache->GetDevice ()->GetAddress ()); */
374  Ipv6PayloadHeaderPair p = icmpv6->ForgeNS (addr, m_ipv6Address, m_ipv6Address, m_ndCache->GetDevice ()->GetAddress ());
375  p.first->AddHeader (p.second);
376  m_ndCache->GetDevice ()->Send (p.first, this->GetMacAddress (), Ipv6L3Protocol::PROT_NUMBER);
377 
378  /* arm the timer again */
379  StartProbeTimer ();
380  }
381  else
382  {
383  /* delete the entry */
384  m_ndCache->Remove (this);
385  }
386 }
387 
389 {
390  NS_LOG_FUNCTION (this << ipv6Address);
391  m_ipv6Address = ipv6Address;
392 }
393 
395 {
397  return m_lastReachabilityConfirmation;
398 }
399 
401 {
403 }
404 
406 {
408  if (m_nudTimer.IsRunning ())
409  {
410  m_nudTimer.Cancel ();
411  }
412 
413  m_nudTimer.SetFunction (&NdiscCache::Entry::FunctionReachableTimeout, this);
414  m_nudTimer.SetDelay (MilliSeconds (Icmpv6L4Protocol::REACHABLE_TIME));
415  m_nudTimer.Schedule ();
416 }
417 
419 {
421  if (m_nudTimer.IsRunning ())
422  {
423  m_nudTimer.Cancel ();
424  }
425  m_nudTimer.SetFunction (&NdiscCache::Entry::FunctionProbeTimeout, this);
426  m_nudTimer.SetDelay (MilliSeconds (Icmpv6L4Protocol::RETRANS_TIMER));
427  m_nudTimer.Schedule ();
428 }
429 
431 {
433  if (m_nudTimer.IsRunning ())
434  {
435  m_nudTimer.Cancel ();
436  }
437  m_nudTimer.SetFunction (&NdiscCache::Entry::FunctionDelayTimeout, this);
438  m_nudTimer.SetDelay (Seconds (Icmpv6L4Protocol::DELAY_FIRST_PROBE_TIME));
439  m_nudTimer.Schedule ();
440 }
441 
443 {
445  if (m_nudTimer.IsRunning ())
446  {
447  m_nudTimer.Cancel ();
448  }
449  m_nudTimer.SetFunction (&NdiscCache::Entry::FunctionRetransmitTimeout, this);
450  m_nudTimer.SetDelay (MilliSeconds (Icmpv6L4Protocol::RETRANS_TIMER));
451  m_nudTimer.Schedule ();
452 }
453 
455 {
457  m_nudTimer.Cancel ();
458  m_nsRetransmit = 0;
459 }
460 
462 {
463  NS_LOG_FUNCTION (this << p.second << p.first);
464  m_state = INCOMPLETE;
465 
466  if (p.first)
467  {
468  m_waiting.push_back (p);
469  }
470 }
471 
472 std::list<NdiscCache::Ipv6PayloadHeaderPair> NdiscCache::Entry::MarkReachable (Address mac)
473 {
474  NS_LOG_FUNCTION (this << mac);
475  m_state = REACHABLE;
476  m_macAddress = mac;
477  return m_waiting;
478 }
479 
481 {
483  m_state = PROBE;
484 }
485 
487 {
489  m_state = STALE;
490 }
491 
493 {
495  m_state = REACHABLE;
496 }
497 
498 std::list<NdiscCache::Ipv6PayloadHeaderPair> NdiscCache::Entry::MarkStale (Address mac)
499 {
500  NS_LOG_FUNCTION (this << mac);
501  m_state = STALE;
502  m_macAddress = mac;
503  return m_waiting;
504 }
505 
507 {
509  m_state = DELAY;
510 }
511 
513 {
515  StopNudTimer ();
516  m_state = PERMANENT;
517 }
518 
520 {
522  return (m_state == STALE);
523 }
524 
526 {
528  return (m_state == REACHABLE);
529 }
530 
532 {
534  return (m_state == DELAY);
535 }
536 
538 {
540  return (m_state == INCOMPLETE);
541 }
542 
544 {
546  return (m_state == PROBE);
547 }
548 
550 {
552  return (m_state == PERMANENT);
553 }
554 
556 {
558  return m_macAddress;
559 }
560 
562 {
563  NS_LOG_FUNCTION (this << mac << int(m_state));
564  m_macAddress = mac;
565 }
566 
567 } /* namespace ns3 */
568 
bool IsAny() const
If the IPv6 address is the "Any" address.
void FunctionDelayTimeout()
Function called when delay timer timeout.
Definition: ndisc-cache.cc:304
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:394
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:555
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:454
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:145
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:339
#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:512
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:531
Ptr< Ipv6Interface > m_interface
the interface.
Definition: ndisc-cache.h:431
void FunctionRetransmitTimeout()
Function called when retransmit timer timeout.
Definition: ndisc-cache.cc:254
void MarkReachable()
Changes the state to this entry to REACHABLE.
Definition: ndisc-cache.cc:492
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:47
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:154
NdiscCache()
Constructor.
Definition: ndisc-cache.cc:52
void StartRetransmitTimer()
Start retransmit timer.
Definition: ndisc-cache.cc:442
void ClearWaitingPacket()
Clear the waiting packet list.
Definition: ndisc-cache.cc:241
Entry(NdiscCache *nd)
Constructor.
Definition: ndisc-cache.cc:205
bool IsProbe() const
Is the entry PROBE.
Definition: ndisc-cache.cc:543
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void StartDelayTimer()
Start delay timer.
Definition: ndisc-cache.cc:430
void StartReachableTimer()
Start the reachable timer.
Definition: ndisc-cache.cc:405
bool IsIncomplete() const
Is the entry INCOMPLETE.
Definition: ndisc-cache.cc:537
void FunctionReachableTimeout()
Function called when reachable timer timeout.
Definition: ndisc-cache.cc:248
void StartProbeTimer()
Start probe timer.
Definition: ndisc-cache.cc:418
static const uint32_t DEFAULT_UNRES_QLEN
Default value for unres qlen.
Definition: ndisc-cache.h:61
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:341
uint32_t m_unresQlen
Max number of packet stored in m_waiting.
Definition: ndisc-cache.h:441
void SetMacAddress(Address mac)
Set the MAC address of this entry.
Definition: ndisc-cache.cc:561
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:486
void DoDispose()
Dispose this object.
Definition: ndisc-cache.cc:63
void SetIpv6Address(Ipv6Address ipv6Address)
Set the IPv6 address.
Definition: ndisc-cache.cc:388
void MarkDelay()
Change the state to this entry to DELAY.
Definition: ndisc-cache.cc:506
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
void UpdateLastReachabilityconfirmation()
Update the time of last reachability confirmation.
Definition: ndisc-cache.cc:400
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:222
void MarkIncomplete(Ipv6PayloadHeaderPair p)
Changes the state to this entry to INCOMPLETE.
Definition: ndisc-cache.cc:461
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:436
bool IsPermanent() const
Is the entry PERMANENT.
Definition: ndisc-cache.cc:549
A record that holds information about an NdiscCache entry.
Definition: ndisc-cache.h:144
void SetRouter(bool router)
Set the node type.
Definition: ndisc-cache.cc:216
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:401
std::pair< Ptr< Packet >, Ipv6Header > Ipv6PayloadHeaderPair
Pair of a packet and an Ipv4 header.
Definition: ndisc-cache.h:138
bool IsReachable() const
Is the entry REACHABLE.
Definition: ndisc-cache.cc:525
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:228
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:826
void MarkProbe()
Changes the state to this entry to PROBE.
Definition: ndisc-cache.cc:480
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:519
static Ipv6Address MakeSolicitedAddress(Ipv6Address addr)
Make the solicited IPv6 address.
Ptr< NetDevice > m_device
The NetDevice.
Definition: ndisc-cache.h:426