A Discrete-Event Network Simulator
API
arp-cache.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2006 INRIA
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: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 #include "ns3/assert.h"
21 #include "ns3/packet.h"
22 #include "ns3/simulator.h"
23 #include "ns3/uinteger.h"
24 #include "ns3/log.h"
25 #include "ns3/node.h"
26 #include "ns3/trace-source-accessor.h"
27 #include "ns3/names.h"
28 
29 #include "arp-cache.h"
30 #include "arp-header.h"
31 #include "ipv4-interface.h"
32 
33 namespace ns3 {
34 
35 NS_LOG_COMPONENT_DEFINE ("ArpCache");
36 
38 
39 TypeId
41 {
42  static TypeId tid = TypeId ("ns3::ArpCache")
43  .SetParent<Object> ()
44  .SetGroupName ("Internet")
45  .AddAttribute ("AliveTimeout",
46  "When this timeout expires, "
47  "the matching cache entry needs refreshing",
48  TimeValue (Seconds (120)),
50  MakeTimeChecker ())
51  .AddAttribute ("DeadTimeout",
52  "When this timeout expires, "
53  "a new attempt to resolve the matching entry is made",
54  TimeValue (Seconds (100)),
56  MakeTimeChecker ())
57  .AddAttribute ("WaitReplyTimeout",
58  "When this timeout expires, "
59  "the cache entries will be scanned and "
60  "entries in WaitReply state will resend ArpRequest "
61  "unless MaxRetries has been exceeded, "
62  "in which case the entry is marked dead",
63  TimeValue (Seconds (1)),
65  MakeTimeChecker ())
66  .AddAttribute ("MaxRetries",
67  "Number of retransmissions of ArpRequest "
68  "before marking dead",
69  UintegerValue (3),
71  MakeUintegerChecker<uint32_t> ())
72  .AddAttribute ("PendingQueueSize",
73  "The size of the queue for packets pending an arp reply.",
74  UintegerValue (3),
76  MakeUintegerChecker<uint32_t> ())
77  .AddTraceSource ("Drop",
78  "Packet dropped due to ArpCache entry "
79  "in WaitReply expiring.",
81  "ns3::Packet::TracedCallback")
82  ;
83  return tid;
84 }
85 
87  : m_device (0),
88  m_interface (0)
89 {
90  NS_LOG_FUNCTION (this);
91 }
92 
94 {
95  NS_LOG_FUNCTION (this);
96 }
97 
98 void
100 {
101  NS_LOG_FUNCTION (this);
102  Flush ();
103  m_device = 0;
104  m_interface = 0;
105  if (!m_waitReplyTimer.IsRunning ())
106  {
108  }
110 }
111 
112 void
114 {
115  NS_LOG_FUNCTION (this << device << interface);
116  m_device = device;
117  m_interface = interface;
118 }
119 
122 {
123  NS_LOG_FUNCTION (this);
124  return m_device;
125 }
126 
129 {
130  NS_LOG_FUNCTION (this);
131  return m_interface;
132 }
133 
134 void
136 {
137  NS_LOG_FUNCTION (this << aliveTimeout);
138  m_aliveTimeout = aliveTimeout;
139 }
140 void
142 {
143  NS_LOG_FUNCTION (this << deadTimeout);
144  m_deadTimeout = deadTimeout;
145 }
146 void
148 {
149  NS_LOG_FUNCTION (this << waitReplyTimeout);
150  m_waitReplyTimeout = waitReplyTimeout;
151 }
152 
153 Time
155 {
156  NS_LOG_FUNCTION (this);
157  return m_aliveTimeout;
158 }
159 Time
161 {
162  NS_LOG_FUNCTION (this);
163  return m_deadTimeout;
164 }
165 Time
167 {
168  NS_LOG_FUNCTION (this);
169  return m_waitReplyTimeout;
170 }
171 
172 void
174  Ipv4Address> arpRequestCallback)
175 {
176  NS_LOG_FUNCTION (this << &arpRequestCallback);
177  m_arpRequestCallback = arpRequestCallback;
178 }
179 
180 void
182 {
183  NS_LOG_FUNCTION (this);
184  if (!m_waitReplyTimer.IsRunning ())
185  {
186  NS_LOG_LOGIC ("Starting WaitReplyTimer at " << Simulator::Now () << " for " <<
190  }
191 }
192 
193 void
195 {
196  NS_LOG_FUNCTION (this);
197  ArpCache::Entry* entry;
198  bool restartWaitReplyTimer = false;
199  for (CacheI i = m_arpCache.begin (); i != m_arpCache.end (); i++)
200  {
201  entry = (*i).second;
202  if (entry != 0 && entry->IsWaitReply ())
203  {
204  if (entry->GetRetries () < m_maxRetries)
205  {
206  NS_LOG_LOGIC ("node="<< m_device->GetNode ()->GetId () <<
207  ", ArpWaitTimeout for " << entry->GetIpv4Address () <<
208  " expired -- retransmitting arp request since retries = " <<
209  entry->GetRetries ());
210  m_arpRequestCallback (this, entry->GetIpv4Address ());
211  restartWaitReplyTimer = true;
212  entry->IncrementRetries ();
213  }
214  else
215  {
216  NS_LOG_LOGIC ("node="<<m_device->GetNode ()->GetId () <<
217  ", wait reply for " << entry->GetIpv4Address () <<
218  " expired -- drop since max retries exceeded: " <<
219  entry->GetRetries ());
220  entry->MarkDead ();
221  entry->ClearRetries ();
222  Ipv4PayloadHeaderPair pending = entry->DequeuePending ();
223  while (pending.first != 0)
224  {
225  // add the Ipv4 header for tracing purposes
226  pending.first->AddHeader (pending.second);
227  m_dropTrace (pending.first);
228  pending = entry->DequeuePending ();
229  }
230  }
231  }
232 
233  }
234  if (restartWaitReplyTimer)
235  {
236  NS_LOG_LOGIC ("Restarting WaitReplyTimer at " << Simulator::Now ().GetSeconds ());
239  }
240 }
241 
242 void
244 {
245  NS_LOG_FUNCTION (this);
246  for (CacheI i = m_arpCache.begin (); i != m_arpCache.end (); i++)
247  {
248  delete (*i).second;
249  }
250  m_arpCache.erase (m_arpCache.begin (), m_arpCache.end ());
252  {
253  NS_LOG_LOGIC ("Stopping WaitReplyTimer at " << Simulator::Now ().GetSeconds () << " due to ArpCache flush");
255  }
256 }
257 
258 void
260 {
261  NS_LOG_FUNCTION (this << stream);
262  std::ostream* os = stream->GetStream ();
263 
264  for (CacheI i = m_arpCache.begin (); i != m_arpCache.end (); i++)
265  {
266  *os << i->first << " dev ";
267  std::string found = Names::FindName (m_device);
268  if (Names::FindName (m_device) != "")
269  {
270  *os << found;
271  }
272  else
273  {
274  *os << static_cast<int> (m_device->GetIfIndex ());
275  }
276 
277  *os << " lladdr " << i->second->GetMacAddress ();
278 
279  if (i->second->IsAlive ())
280  {
281  *os << " REACHABLE\n";
282  }
283  else if (i->second->IsWaitReply ())
284  {
285  *os << " DELAY\n";
286  }
287  else if (i->second->IsPermanent ())
288  {
289  *os << " PERMANENT\n";
290  }
291  else
292  {
293  *os << " STALE\n";
294  }
295  }
296 }
297 
300 {
301  NS_LOG_FUNCTION (this << to);
302  if (m_arpCache.find (to) != m_arpCache.end ())
303  {
304  ArpCache::Entry *entry = m_arpCache[to];
305  return entry;
306  }
307  return 0;
308 }
309 
312 {
313  NS_LOG_FUNCTION (this << to);
314  NS_ASSERT (m_arpCache.find (to) == m_arpCache.end ());
315 
316  ArpCache::Entry *entry = new ArpCache::Entry (this);
317  m_arpCache[to] = entry;
318  entry->SetIpv4Address (to);
319  return entry;
320 }
321 
322 void
324 {
325  NS_LOG_FUNCTION (this << entry);
326 
327  for (CacheI i = m_arpCache.begin (); i != m_arpCache.end (); i++)
328  {
329  if ((*i).second == entry)
330  {
331  m_arpCache.erase (i);
332  entry->ClearPendingPacket (); //clear the pending packets for entry's ipaddress
333  delete entry;
334  return;
335  }
336  }
337  NS_LOG_WARN ("Entry not found in this ARP Cache");
338 }
339 
341  : m_arp (arp),
342  m_state (ALIVE),
343  m_retries (0)
344 {
345  NS_LOG_FUNCTION (this << arp);
346 }
347 
348 
349 bool
351 {
352  NS_LOG_FUNCTION (this);
353  return (m_state == DEAD) ? true : false;
354 }
355 bool
357 {
358  NS_LOG_FUNCTION (this);
359  return (m_state == ALIVE) ? true : false;
360 }
361 bool
363 {
364  NS_LOG_FUNCTION (this);
365  return (m_state == WAIT_REPLY) ? true : false;
366 }
367 bool
369 {
370  NS_LOG_FUNCTION (this);
371  return (m_state == PERMANENT) ? true : false;
372 }
373 
374 
375 void
377 {
378  NS_LOG_FUNCTION (this);
379  NS_ASSERT (m_state == ALIVE || m_state == WAIT_REPLY || m_state == DEAD);
380  m_state = DEAD;
381  ClearRetries ();
382  UpdateSeen ();
383 }
384 void
386 {
387  NS_LOG_FUNCTION (this << macAddress);
388  NS_ASSERT (m_state == WAIT_REPLY);
389  m_macAddress = macAddress;
390  m_state = ALIVE;
391  ClearRetries ();
392  UpdateSeen ();
393 }
394 void
396 {
397  NS_LOG_FUNCTION (this << m_macAddress);
398  NS_ASSERT (!m_macAddress.IsInvalid ());
399 
400  m_state = PERMANENT;
401  ClearRetries ();
402  UpdateSeen ();
403 }
404 bool
406 {
407  NS_LOG_FUNCTION (this << waiting.first);
408  NS_ASSERT (m_state == WAIT_REPLY);
409  /* We are already waiting for an answer so
410  * we dump the previously waiting packet and
411  * replace it with this one.
412  */
413  if (m_pending.size () >= m_arp->m_pendingQueueSize)
414  {
415  return false;
416  }
417  m_pending.push_back (waiting);
418  return true;
419 }
420 void
422 {
423  NS_LOG_FUNCTION (this << waiting.first);
424  NS_ASSERT (m_state == ALIVE || m_state == DEAD);
425  NS_ASSERT (m_pending.empty ());
426  NS_ASSERT_MSG (waiting.first, "Can not add a null packet to the ARP queue");
427 
428  m_state = WAIT_REPLY;
429  m_pending.push_back (waiting);
430  UpdateSeen ();
431  m_arp->StartWaitReplyTimer ();
432 }
433 
434 Address
436 {
437  NS_LOG_FUNCTION (this);
438  return m_macAddress;
439 }
440 void
442 {
443  NS_LOG_FUNCTION (this);
444  m_macAddress = macAddress;
445 }
448 {
449  NS_LOG_FUNCTION (this);
450  return m_ipv4Address;
451 }
452 void
454 {
455  NS_LOG_FUNCTION (this << destination);
456  m_ipv4Address = destination;
457 }
458 Time
460 {
461  NS_LOG_FUNCTION (this);
462  switch (m_state) {
464  return m_arp->GetWaitReplyTimeout ();
466  return m_arp->GetDeadTimeout ();
468  return m_arp->GetAliveTimeout ();
470  return Time::Max ();
471  default:
472  NS_ASSERT (false);
473  return Seconds (0);
474  /* NOTREACHED */
475  }
476 }
477 bool
479 {
480  NS_LOG_FUNCTION (this);
481  Time timeout = GetTimeout ();
482  Time delta = Simulator::Now () - m_lastSeen;
483  NS_LOG_DEBUG ("delta=" << delta.GetSeconds () << "s");
484  if (delta > timeout)
485  {
486  return true;
487  }
488  return false;
489 }
492 {
493  NS_LOG_FUNCTION (this);
494  if (m_pending.empty ())
495  {
496  Ipv4Header h;
497  return Ipv4PayloadHeaderPair (0, h);
498  }
499  else
500  {
501  Ipv4PayloadHeaderPair p = m_pending.front ();
502  m_pending.pop_front ();
503  return p;
504  }
505 }
506 void
508 {
509  NS_LOG_FUNCTION (this);
510  m_pending.clear ();
511 }
512 void
514 {
515  NS_LOG_FUNCTION (this);
516  m_lastSeen = Simulator::Now ();
517 }
518 uint32_t
520 {
521  NS_LOG_FUNCTION (this);
522  return m_retries;
523 }
524 void
526 {
527  NS_LOG_FUNCTION (this);
528  m_retries++;
529  UpdateSeen ();
530 }
531 void
533 {
534  NS_LOG_FUNCTION (this);
535  m_retries = 0;
536 }
537 
538 } // namespace ns3
539 
void SetDevice(Ptr< NetDevice > device, Ptr< Ipv4Interface > interface)
Set the NetDevice and Ipv4Interface associated with the ArpCache.
Definition: arp-cache.cc:113
uint32_t m_maxRetries
max retries for a resolution
Definition: arp-cache.h:322
void StartWaitReplyTimer(void)
This method will schedule a timeout at WaitReplyTimeout interval in the future, unless a timer is alr...
Definition: arp-cache.cc:181
void MarkPermanent(void)
Changes the state of this entry to Permanent.
Definition: arp-cache.cc:395
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
void SetArpRequestCallback(Callback< void, Ptr< const ArpCache >, Ipv4Address > arpRequestCallback)
This callback is set when the ArpCache is set up and allows the cache to generate an Arp request when...
Definition: arp-cache.cc:173
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 "...
Callback template class.
Definition: callback.h:1164
ArpCache::Entry * Add(Ipv4Address to)
Add an Ipv4Address to this ARP cache.
Definition: arp-cache.cc:311
virtual void DoDispose(void)
Destructor implementation.
Definition: arp-cache.cc:99
static TypeId GetTypeId(void)
Get the type ID.
Definition: arp-cache.cc:40
void PrintArpCache(Ptr< OutputStreamWrapper > stream)
Print the ARP cache entries.
Definition: arp-cache.cc:259
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
void SetMacAddresss(Address macAddress)
Definition: arp-cache.cc:441
ArpCache::Entry * Lookup(Ipv4Address destination)
Do lookup in the ARP cache against an IP address.
Definition: arp-cache.cc:299
#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
bool IsPermanent(void)
Definition: arp-cache.cc:368
Callback< void, Ptr< const ArpCache >, Ipv4Address > m_arpRequestCallback
reply timeout callback
Definition: arp-cache.h:321
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
Time GetDeadTimeout(void) const
Get the time the entry will be in DEAD state before being removed.
Definition: arp-cache.cc:160
Ptr< NetDevice > GetDevice(void) const
Returns the NetDevice that this ARP cache is associated with.
Definition: arp-cache.cc:121
bool IsRunning(void) const
This method is syntactic sugar for !IsExpired().
Definition: event-id.cc:65
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:339
ns3::Time timeout
bool IsAlive(void)
Definition: arp-cache.cc:356
EventId m_waitReplyTimer
cache alive state timer
Definition: arp-cache.h:320
a polymophic address class
Definition: address.h:90
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:446
static Time Max()
Maximum representable Time.
Definition: nstime.h:259
Packet header for IPv4.
Definition: ipv4-header.h:31
Cache m_arpCache
the ARP cache
Definition: arp-cache.h:331
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:341
void SetIpv4Address(Ipv4Address destination)
Definition: arp-cache.cc:453
void HandleWaitReplyTimeout(void)
This function is an event handler for the event that the ArpCache wants to check whether it must retr...
Definition: arp-cache.cc:194
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1216
AttributeValue implementation for Time.
Definition: nstime.h:957
Hold an unsigned integer type.
Definition: uinteger.h:44
uint32_t GetRetries(void) const
Definition: arp-cache.cc:519
bool IsDead(void)
Definition: arp-cache.cc:350
Ptr< Ipv4Interface > m_interface
Ipv4Interface associated with the cache.
Definition: arp-cache.h:316
bool IsExpired(void) const
Definition: arp-cache.cc:478
Ipv4Address GetIpv4Address(void) const
Definition: arp-cache.cc:447
void MarkWaitReply(Ipv4PayloadHeaderPair waiting)
Definition: arp-cache.cc:421
Time m_waitReplyTimeout
cache reply state timeout
Definition: arp-cache.h:319
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:252
Address GetMacAddress(void) const
Definition: arp-cache.cc:435
void SetDeadTimeout(Time deadTimeout)
Set the time the entry will be in DEAD state before being removed.
Definition: arp-cache.cc:141
void SetAliveTimeout(Time aliveTimeout)
Set the time the entry will be in ALIVE state (unless refreshed)
Definition: arp-cache.cc:135
Ptr< Ipv4Interface > GetInterface(void) const
Returns the Ipv4Interface that this ARP cache is associated with.
Definition: arp-cache.cc:128
static void Remove(const EventId &id)
Remove an event from the event list.
Definition: simulator.cc:301
void IncrementRetries(void)
Increment the counter of number of retries for an entry.
Definition: arp-cache.cc:525
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void ClearPendingPacket(void)
Clear the pending packet list.
Definition: arp-cache.cc:507
void MarkAlive(Address macAddress)
Definition: arp-cache.cc:385
Time GetAliveTimeout(void) const
Get the time the entry will be in ALIVE state (unless refreshed)
Definition: arp-cache.cc:154
A record that that holds information about an ArpCache entry.
Definition: arp-cache.h:179
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: nstime.h:958
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:223
void Flush(void)
Clear the ArpCache of all entries.
Definition: arp-cache.cc:243
void Remove(ArpCache::Entry *entry)
Remove an entry.
Definition: arp-cache.cc:323
Entry(ArpCache *arp)
Constructor.
Definition: arp-cache.cc:340
Time GetTimeout(void) const
Returns the entry timeout.
Definition: arp-cache.cc:459
void UpdateSeen(void)
Update the entry when seeing a packet.
Definition: arp-cache.cc:513
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:90
TracedCallback< Ptr< const Packet > > m_dropTrace
trace for packets dropped by the ARP cache queue
Definition: arp-cache.h:332
bool UpdateWaitReply(Ipv4PayloadHeaderPair waiting)
Definition: arp-cache.cc:405
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
Ipv4PayloadHeaderPair DequeuePending(void)
Definition: arp-cache.cc:491
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:228
sgi::hash_map< Ipv4Address, ArpCache::Entry *, Ipv4AddressHash >::iterator CacheI
ARP Cache container iterator.
Definition: arp-cache.h:311
std::pair< Ptr< Packet >, Ipv4Header > Ipv4PayloadHeaderPair
Pair of a packet and an Ipv4 header.
Definition: arp-cache.h:174
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:236
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
bool IsWaitReply(void)
Definition: arp-cache.cc:362
An ARP cache.
Definition: arp-cache.h:51
void Cancel(void)
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition: event-id.cc:53
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
Time GetWaitReplyTimeout(void) const
Get the time the entry will be in WAIT_REPLY state.
Definition: arp-cache.cc:166
void ClearRetries(void)
Zero the counter of number of retries for an entry.
Definition: arp-cache.cc:532
Ptr< NetDevice > m_device
NetDevice associated with the cache.
Definition: arp-cache.h:315
void MarkDead(void)
Changes the state of this entry to dead.
Definition: arp-cache.cc:376
Time m_deadTimeout
cache dead state timeout
Definition: arp-cache.h:318
A base class which provides memory management and object aggregation.
Definition: object.h:87
uint32_t m_pendingQueueSize
number of packets waiting for a resolution
Definition: arp-cache.h:330
void SetWaitReplyTimeout(Time waitReplyTimeout)
Set the time the entry will be in WAIT_REPLY state.
Definition: arp-cache.cc:147
Time m_aliveTimeout
cache alive state timeout
Definition: arp-cache.h:317
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
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:826
std::ostream * GetStream(void)
Return a pointer to an ostream previously set in the wrapper.