View | Details | Raw Unified | Return to bug 253
Collapse All | Expand All

(-)a/src/internet-stack/arp-cache.cc (-3 / +124 lines)
 Lines 22-27    Link Here 
22
#include "ns3/simulator.h"
22
#include "ns3/simulator.h"
23
#include "ns3/uinteger.h"
23
#include "ns3/uinteger.h"
24
#include "ns3/log.h"
24
#include "ns3/log.h"
25
#include "ns3/node.h"
25
26
26
#include "arp-cache.h"
27
#include "arp-cache.h"
27
#include "arp-header.h"
28
#include "arp-header.h"
 Lines 47-56   ArpCache::GetTypeId (void) Link Here 
47
                   MakeTimeAccessor (&ArpCache::m_deadTimeout),
48
                   MakeTimeAccessor (&ArpCache::m_deadTimeout),
48
                   MakeTimeChecker ())
49
                   MakeTimeChecker ())
49
    .AddAttribute ("WaitReplyTimeout",
50
    .AddAttribute ("WaitReplyTimeout",
50
                   "When this timeout expires, the matching cache entry is marked dead",
51
                   "When this timeout expires, the cache entries will be scanned and entries in WaitReply state will resend ArpRequest unless MaxRetries has been exceeded, in which case the entry is marked dead",           
51
                   TimeValue (Seconds (1)),
52
                   TimeValue (Seconds (1)),
52
                   MakeTimeAccessor (&ArpCache::m_waitReplyTimeout),
53
                   MakeTimeAccessor (&ArpCache::m_waitReplyTimeout),
53
                   MakeTimeChecker ())
54
                   MakeTimeChecker ())
55
    .AddAttribute ("MaxRetries",                   
56
                   "Number of retransmissions of ArpRequest before marking dead",                  
57
                   UintegerValue (3),
58
                   MakeUintegerAccessor (&ArpCache::m_maxRetries),
59
                   MakeUintegerChecker<uint32_t> ())
54
    .AddAttribute ("PendingQueueSize",
60
    .AddAttribute ("PendingQueueSize",
55
                   "The size of the queue for packets pending an arp reply.",
61
                   "The size of the queue for packets pending an arp reply.",
56
                   UintegerValue (3),
62
                   UintegerValue (3),
 Lines 79-84   ArpCache::DoDispose (void) Link Here 
79
  Flush ();
85
  Flush ();
80
  m_device = 0;
86
  m_device = 0;
81
  m_interface = 0;
87
  m_interface = 0;
88
  if (!m_waitReplyTimer.IsRunning ())
89
    {
90
      Simulator::Remove (m_waitReplyTimer);
91
    }
82
  Object::DoDispose ();
92
  Object::DoDispose ();
83
}
93
}
84
94
 Lines 143-148   ArpCache::GetWaitReplyTimeout (void) con Link Here 
143
}
153
}
144
154
145
void 
155
void 
156
ArpCache::SetArpRequestCallback (Callback<void, Ptr<const ArpCache>,
157
                             Ipv4Address> arpRequestCallback)
158
{
159
  NS_LOG_FUNCTION_NOARGS ();
160
  m_arpRequestCallback = arpRequestCallback;
161
}
162
163
void 
164
ArpCache::StartWaitReplyTimer (void)
165
{
166
  NS_LOG_FUNCTION_NOARGS ();
167
  if (!m_waitReplyTimer.IsRunning ())
168
    {
169
      NS_LOG_LOGIC ("Starting WaitReplyTimer at " << Simulator::Now ().GetSeconds ());
170
      m_waitReplyTimer = Simulator::Schedule (m_waitReplyTimeout, 
171
        &ArpCache::HandleWaitReplyTimeout, this);
172
    }
173
}
174
175
void
176
ArpCache::HandleWaitReplyTimeout (void)
177
{
178
  NS_LOG_FUNCTION_NOARGS ();
179
  ArpCache::Entry* entry;
180
  bool restartWaitReplyTimer = false;
181
  for (CacheI i = m_arpCache.begin (); i != m_arpCache.end (); i++) 
182
    {
183
      entry = (*i).second;
184
      if (entry != 0 && entry->IsWaitReply ())
185
        {
186
          if (entry->GetRetries () < m_maxRetries )
187
            {
188
              NS_LOG_LOGIC ("node="<< m_device->GetNode ()->GetId () <<
189
                ", ArpWaitTimeout for " << entry->GetIpv4Address () <<
190
                " expired -- retransmitting arp request since retries = " << 
191
                entry->GetRetries ());
192
              m_arpRequestCallback (this, entry->GetIpv4Address ());
193
              restartWaitReplyTimer = true;
194
              entry->IncrementRetries ();
195
            }
196
          else
197
            {
198
              NS_LOG_LOGIC ("node="<<m_device->GetNode ()->GetId () <<
199
                ", wait reply for " << entry->GetIpv4Address () << 
200
                " expired -- drop since max retries exceeded: " << 
201
                 entry->GetRetries ());
202
              entry->MarkDead ();
203
              entry->ClearRetries ();
204
              Ptr<Packet> pending = entry->DequeuePending();
205
              while (pending != 0)
206
                {
207
                  m_dropTrace (pending);
208
                  pending = entry->DequeuePending();
209
                }
210
            }
211
       }
212
213
    }
214
  if (restartWaitReplyTimer)
215
    {
216
      NS_LOG_LOGIC ("Restarting WaitReplyTimer at " << Simulator::Now ().GetSeconds ());
217
      m_waitReplyTimer = Simulator::Schedule (m_waitReplyTimeout, 
218
        &ArpCache::HandleWaitReplyTimeout, this);
219
    }
220
}
221
222
void 
146
ArpCache::Flush (void)
223
ArpCache::Flush (void)
147
{
224
{
148
  NS_LOG_FUNCTION_NOARGS ();
225
  NS_LOG_FUNCTION_NOARGS ();
 Lines 151-156   ArpCache::Flush (void) Link Here 
151
      delete (*i).second;
228
      delete (*i).second;
152
    }
229
    }
153
  m_arpCache.erase (m_arpCache.begin (), m_arpCache.end ());
230
  m_arpCache.erase (m_arpCache.begin (), m_arpCache.end ());
231
}
232
233
void
234
ArpCache::SetDropTrace ( TracedCallback<Ptr<const Packet> > dropTrace)
235
{
236
  NS_LOG_FUNCTION_NOARGS ();
237
  m_dropTrace = dropTrace;
154
}
238
}
155
239
156
ArpCache::Entry *
240
ArpCache::Entry *
 Lines 173-184   ArpCache::Add (Ipv4Address to) Link Here 
173
257
174
  ArpCache::Entry *entry = new ArpCache::Entry (this);
258
  ArpCache::Entry *entry = new ArpCache::Entry (this);
175
  m_arpCache[to] = entry;  
259
  m_arpCache[to] = entry;  
260
  entry->SetIpv4Address (to);
176
  return entry;
261
  return entry;
177
}
262
}
178
263
179
ArpCache::Entry::Entry (ArpCache *arp)
264
ArpCache::Entry::Entry (ArpCache *arp)
180
  : m_arp (arp),
265
  : m_arp (arp),
181
    m_state (ALIVE)
266
    m_state (ALIVE),
267
    m_retries (0)
182
{
268
{
183
  NS_LOG_FUNCTION_NOARGS ();
269
  NS_LOG_FUNCTION_NOARGS ();
184
}
270
}
 Lines 209-214   ArpCache::Entry::MarkDead (void) Link Here 
209
{
295
{
210
  NS_LOG_FUNCTION_NOARGS ();
296
  NS_LOG_FUNCTION_NOARGS ();
211
  m_state = DEAD;
297
  m_state = DEAD;
298
  m_retries = 0;
212
  UpdateSeen ();
299
  UpdateSeen ();
213
}
300
}
214
void
301
void
 Lines 218-223   ArpCache::Entry::MarkAlive (Address macA Link Here 
218
  NS_ASSERT (m_state == WAIT_REPLY);
305
  NS_ASSERT (m_state == WAIT_REPLY);
219
  m_macAddress = macAddress;
306
  m_macAddress = macAddress;
220
  m_state = ALIVE;
307
  m_state = ALIVE;
308
  m_retries = 0;
221
  UpdateSeen ();
309
  UpdateSeen ();
222
}
310
}
223
311
 Lines 246-260   ArpCache::Entry::MarkWaitReply (Ptr<Pack Link Here 
246
  m_state = WAIT_REPLY;
334
  m_state = WAIT_REPLY;
247
  m_pending.push_back (waiting);
335
  m_pending.push_back (waiting);
248
  UpdateSeen ();
336
  UpdateSeen ();
337
  m_arp->StartWaitReplyTimer ();
249
}
338
}
250
339
251
Address
340
Address
252
ArpCache::Entry::GetMacAddress (void)
341
ArpCache::Entry::GetMacAddress (void) const
253
{
342
{
254
  NS_LOG_FUNCTION_NOARGS ();
343
  NS_LOG_FUNCTION_NOARGS ();
255
  NS_ASSERT (m_state == ALIVE);
344
  NS_ASSERT (m_state == ALIVE);
256
  return m_macAddress;
345
  return m_macAddress;
257
}
346
}
347
Ipv4Address 
348
ArpCache::Entry::GetIpv4Address (void) const
349
{
350
  NS_LOG_FUNCTION_NOARGS ();
351
  return m_ipv4Address;
352
}
353
void 
354
ArpCache::Entry::SetIpv4Address (Ipv4Address destination)
355
{
356
  NS_LOG_FUNCTION (this << destination);
357
  m_ipv4Address = destination;
358
}
359
258
bool 
360
bool 
259
ArpCache::Entry::IsExpired (void)
361
ArpCache::Entry::IsExpired (void)
260
{
362
{
 Lines 307-312   ArpCache::Entry::UpdateSeen (void) Link Here 
307
  NS_LOG_FUNCTION_NOARGS ();
409
  NS_LOG_FUNCTION_NOARGS ();
308
  m_lastSeen = Simulator::Now ();
410
  m_lastSeen = Simulator::Now ();
309
}
411
}
412
uint32_t
413
ArpCache::Entry::GetRetries (void) const
414
{
415
  NS_LOG_FUNCTION_NOARGS ();
416
  return m_retries;
417
}
418
void
419
ArpCache::Entry::IncrementRetries (void)
420
{
421
  NS_LOG_FUNCTION_NOARGS ();
422
  m_retries++;
423
  UpdateSeen ();
424
}
425
void
426
ArpCache::Entry::ClearRetries (void)
427
{
428
  NS_LOG_FUNCTION_NOARGS ();
429
  m_retries = 0;
430
}
310
431
311
} // namespace ns3
432
} // namespace ns3
312
433
(-)a/src/internet-stack/arp-cache.h (-2 / +59 lines)
 Lines 22-27    Link Here 
22
22
23
#include <stdint.h>
23
#include <stdint.h>
24
#include <list>
24
#include <list>
25
#include "ns3/simulator.h"
26
#include "ns3/callback.h"
25
#include "ns3/packet.h"
27
#include "ns3/packet.h"
26
#include "ns3/nstime.h"
28
#include "ns3/nstime.h"
27
#include "ns3/net-device.h"
29
#include "ns3/net-device.h"
 Lines 29-34    Link Here 
29
#include "ns3/address.h"
31
#include "ns3/address.h"
30
#include "ns3/ptr.h"
32
#include "ns3/ptr.h"
31
#include "ns3/object.h"
33
#include "ns3/object.h"
34
#include "ns3/traced-callback.h"
32
#include "sgi-hashmap.h"
35
#include "sgi-hashmap.h"
33
36
34
namespace ns3 {
37
namespace ns3 {
 Lines 72-78   public: Link Here 
72
  Time GetWaitReplyTimeout (void) const;
75
  Time GetWaitReplyTimeout (void) const;
73
76
74
  /**
77
  /**
75
   * \brief Do lookup in the ARP chache against an IP address
78
   * This callback is set when the ArpCache is set up and allows
79
   * the cache to generate an Arp request when the WaitReply
80
   * time expires and a retransmission must be sent
81
   *
82
   * \param arpRequestCallback Callback for transmitting an Arp request.
83
   */
84
  void SetArpRequestCallback (Callback<void, Ptr<const ArpCache>, 
85
                             Ipv4Address> arpRequestCallback);
86
  /**
87
   * This method will schedule a timeout at WaitReplyTimeout interval
88
   * in the future, unless a timer is already running for the cache,
89
   * in which case this method does nothing.
90
   */
91
  void StartWaitReplyTimer (void);
92
  /**
93
   * \brief Do lookup in the ARP cache against an IP address
76
   * \param destination The destination IPv4 address to lookup the MAC address
94
   * \param destination The destination IPv4 address to lookup the MAC address
77
   * of
95
   * of
78
   * \return An ArpCache::Entry with info about layer 2
96
   * \return An ArpCache::Entry with info about layer 2
 Lines 86-91   public: Link Here 
86
   * \brief Clear the ArpCache of all entries
104
   * \brief Clear the ArpCache of all entries
87
   */
105
   */
88
  void Flush (void);
106
  void Flush (void);
107
  /**
108
   * \brief Assign a drop trace to the internal drop trace callback
109
   * \param dropTrace drop trace to assign
110
   */
111
  void SetDropTrace ( TracedCallback<Ptr<const Packet> > dropTrace);
89
112
90
  /**
113
  /**
91
   * \brief A record that that holds information about an ArpCache entry
114
   * \brief A record that that holds information about an ArpCache entry
 Lines 131-137   public: Link Here 
131
    /**
154
    /**
132
     * \return The MacAddress of this entry
155
     * \return The MacAddress of this entry
133
     */
156
     */
134
    Address GetMacAddress (void);
157
    Address GetMacAddress (void) const;
158
    /**
159
     * \return The Ipv4Address for this entry
160
     */
161
    Ipv4Address GetIpv4Address (void) const;
162
    /**
163
     * \param The Ipv4Address for this entry
164
     */
165
    void SetIpv4Address (Ipv4Address destination);
135
    /**
166
    /**
136
     * \return True if this entry has timedout; false otherwise.
167
     * \return True if this entry has timedout; false otherwise.
137
     */
168
     */
 Lines 142-147   public: Link Here 
142
     *            packets are pending.
173
     *            packets are pending.
143
     */
174
     */
144
    Ptr<Packet> DequeuePending (void);
175
    Ptr<Packet> DequeuePending (void);
176
    /**
177
     * \returns number of retries that have been sent for an ArpRequest
178
     *  in WaitReply state.
179
     */
180
    uint32_t GetRetries (void) const;
181
    /**
182
     * \brief Increment the counter of number of retries for an entry
183
     */
184
    void IncrementRetries (void);
185
    /**
186
     * \brief Zero the counter of number of retries for an entry
187
     */
188
    void ClearRetries (void);
189
145
  private:
190
  private:
146
    enum ArpCacheEntryState_e {
191
    enum ArpCacheEntryState_e {
147
      ALIVE,
192
      ALIVE,
 Lines 154-160   public: Link Here 
154
    ArpCacheEntryState_e m_state;
199
    ArpCacheEntryState_e m_state;
155
    Time m_lastSeen;
200
    Time m_lastSeen;
156
    Address m_macAddress;
201
    Address m_macAddress;
202
    Ipv4Address m_ipv4Address;
157
    std::list<Ptr<Packet> > m_pending;
203
    std::list<Ptr<Packet> > m_pending;
204
    uint32_t m_retries;
158
  };
205
  };
159
206
160
private:
207
private:
 Lines 168-175   private: Link Here 
168
  Time m_aliveTimeout;
215
  Time m_aliveTimeout;
169
  Time m_deadTimeout;
216
  Time m_deadTimeout;
170
  Time m_waitReplyTimeout;
217
  Time m_waitReplyTimeout;
218
  EventId m_waitReplyTimer;
219
  Callback<void, Ptr<const ArpCache>, Ipv4Address> m_arpRequestCallback;
220
  uint32_t m_maxRetries;
221
    /**
222
     * This function is an event handler for the event that the
223
     * ArpCache wants to check whether it must retry any Arp requests.
224
     * If there are no Arp requests pending, this event is not scheduled.
225
     */
226
    void HandleWaitReplyTimeout (void);
171
  uint32_t m_pendingQueueSize;
227
  uint32_t m_pendingQueueSize;
172
  Cache m_arpCache;
228
  Cache m_arpCache;
229
  TracedCallback<Ptr<const Packet> > m_dropTrace;
173
};
230
};
174
231
175
232
(-)a/src/internet-stack/arp-l3-protocol.cc (+9 lines)
 Lines 95-100   ArpL3Protocol::CreateCache (Ptr<NetDevic Link Here 
95
  cache->SetDevice (device, interface);
95
  cache->SetDevice (device, interface);
96
  NS_ASSERT (device->IsBroadcast ());
96
  NS_ASSERT (device->IsBroadcast ());
97
  device->SetLinkChangeCallback (MakeCallback (&ArpCache::Flush, cache));
97
  device->SetLinkChangeCallback (MakeCallback (&ArpCache::Flush, cache));
98
  cache->SetArpRequestCallback (MakeCallback (&ArpL3Protocol::SendArpRequest, this));
99
  cache->SetDropTrace (m_dropTrace);
98
  m_cacheList.push_back (cache);
100
  m_cacheList.push_back (cache);
99
  return cache;
101
  return cache;
100
}
102
}
 Lines 218-223   ArpL3Protocol::Lookup (Ptr<Packet> packe Link Here 
218
            } 
220
            } 
219
          else if (entry->IsWaitReply ()) 
221
          else if (entry->IsWaitReply ()) 
220
            {
222
            {
223
              NS_ASSERT (false); // Test for unreachable code, remove later
221
              NS_LOG_LOGIC ("node="<<m_node->GetId ()<<
224
              NS_LOG_LOGIC ("node="<<m_node->GetId ()<<
222
                        ", wait reply for " << destination << " expired -- drop");
225
                        ", wait reply for " << destination << " expired -- drop");
223
              entry->MarkDead ();
226
              entry->MarkDead ();
 Lines 266-271   ArpL3Protocol::Lookup (Ptr<Packet> packe Link Here 
266
      SendArpRequest (cache, destination);
269
      SendArpRequest (cache, destination);
267
    }
270
    }
268
  return false;
271
  return false;
272
}
273
274
TracedCallback<Ptr<const Packet> > 
275
ArpL3Protocol::GetDropTrace (void) const
276
{
277
  return m_dropTrace;
269
}
278
}
270
279
271
void
280
void
(-)a/src/internet-stack/arp-l3-protocol.h (-1 / +5 lines)
 Lines 52-58   public: Link Here 
52
  Ptr<ArpCache> CreateCache (Ptr<NetDevice> device, Ptr<Ipv4Interface> interface);
52
  Ptr<ArpCache> CreateCache (Ptr<NetDevice> device, Ptr<Ipv4Interface> interface);
53
53
54
  /**
54
  /**
55
   * \brief Recieve a packet
55
   * \brief Receive a packet
56
   */
56
   */
57
  void Receive(Ptr<NetDevice> device, Ptr<Packet> p, uint16_t protocol, const Address &from, const Address &to,
57
  void Receive(Ptr<NetDevice> device, Ptr<Packet> p, uint16_t protocol, const Address &from, const Address &to,
58
               NetDevice::PacketType packetType);
58
               NetDevice::PacketType packetType);
 Lines 69-74   public: Link Here 
69
	       Ptr<NetDevice> device,
69
	       Ptr<NetDevice> device,
70
               Ptr<ArpCache> cache,
70
               Ptr<ArpCache> cache,
71
	       Address *hardwareDestination);
71
	       Address *hardwareDestination);
72
  /**
73
   * \brief Public accessor to ArpL3Protocol drop trace
74
   */
75
  TracedCallback<Ptr<const Packet> > GetDropTrace (void) const;
72
protected:
76
protected:
73
  virtual void DoDispose (void);
77
  virtual void DoDispose (void);
74
private:
78
private:

Return to bug 253