A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
arp-cache.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2006 INRIA
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18 */
19#ifndef ARP_CACHE_H
20#define ARP_CACHE_H
21
22#include "ns3/address.h"
23#include "ns3/callback.h"
24#include "ns3/ipv4-address.h"
25#include "ns3/net-device.h"
26#include "ns3/nstime.h"
27#include "ns3/object.h"
28#include "ns3/output-stream-wrapper.h"
29#include "ns3/packet.h"
30#include "ns3/ptr.h"
31#include "ns3/simulator.h"
32#include "ns3/traced-callback.h"
33
34#include <list>
35#include <map>
36#include <stdint.h>
37
38namespace ns3
39{
40
41class NetDevice;
42class Ipv4Interface;
43class Ipv4Header;
44
45/**
46 * \ingroup arp
47 * \brief An ARP cache
48 *
49 * A cached lookup table for translating layer 3 addresses to layer 2.
50 * This implementation does lookups from IPv4 to a MAC address
51 */
52class ArpCache : public Object
53{
54 public:
55 /**
56 * \brief Get the type ID.
57 * \return the object TypeId
58 */
59 static TypeId GetTypeId();
60 class Entry;
61
62 ArpCache();
63 ~ArpCache() override;
64
65 // Delete copy constructor and assignment operator to avoid misuse
66 ArpCache(const ArpCache&) = delete;
67 ArpCache& operator=(const ArpCache&) = delete;
68
69 /**
70 * \brief Set the NetDevice and Ipv4Interface associated with the ArpCache
71 *
72 * \param device The hardware NetDevice associated with this ARP cache
73 * \param interface the Ipv4Interface associated with this ARP cache
74 */
75 void SetDevice(Ptr<NetDevice> device, Ptr<Ipv4Interface> interface);
76 /**
77 * \brief Returns the NetDevice that this ARP cache is associated with
78 * \return The NetDevice that this ARP cache is associated with
79 */
81 /**
82 * \brief Returns the Ipv4Interface that this ARP cache is associated with
83 * \return the Ipv4Interface that this ARP cache is associated with
84 */
86
87 /**
88 * \brief Set the time the entry will be in ALIVE state (unless refreshed)
89 * \param aliveTimeout the Alive state timeout
90 */
91 void SetAliveTimeout(Time aliveTimeout);
92 /**
93 * \brief Set the time the entry will be in DEAD state before being removed
94 * \param deadTimeout the Dead state timeout
95 */
96 void SetDeadTimeout(Time deadTimeout);
97 /**
98 * \brief Set the time the entry will be in WAIT_REPLY state
99 * \param waitReplyTimeout the WAIT_REPLY state timeout
100 */
101 void SetWaitReplyTimeout(Time waitReplyTimeout);
102
103 /**
104 * \brief Get the time the entry will be in ALIVE state (unless refreshed)
105 * \returns the Alive state timeout
106 */
107 Time GetAliveTimeout() const;
108 /**
109 * \brief Get the time the entry will be in DEAD state before being removed
110 * \returns the Dead state timeout
111 */
112 Time GetDeadTimeout() const;
113 /**
114 * \brief Get the time the entry will be in WAIT_REPLY state
115 * \returns the WAIT_REPLY state timeout
116 */
118
119 /**
120 * This callback is set when the ArpCache is set up and allows
121 * the cache to generate an Arp request when the WaitReply
122 * time expires and a retransmission must be sent
123 *
124 * \param arpRequestCallback Callback for transmitting an Arp request.
125 */
126 void SetArpRequestCallback(Callback<void, Ptr<const ArpCache>, Ipv4Address> arpRequestCallback);
127 /**
128 * This method will schedule a timeout at WaitReplyTimeout interval
129 * in the future, unless a timer is already running for the cache,
130 * in which case this method does nothing.
131 */
132 void StartWaitReplyTimer();
133 /**
134 * \brief Do lookup in the ARP cache against an IP address
135 * \param destination The destination IPv4 address to lookup the MAC address
136 * of
137 * \return An ArpCache::Entry with info about layer 2
138 */
139 ArpCache::Entry* Lookup(Ipv4Address destination);
140 /**
141 * \brief Do lookup in the ARP cache against a MAC address
142 * \param destination The destination MAC address to lookup
143 * of
144 * \return A std::list of ArpCache::Entry with info about layer 2
145 */
146 std::list<ArpCache::Entry*> LookupInverse(Address destination);
147 /**
148 * \brief Add an Ipv4Address to this ARP cache
149 * \param to the destination address of the ARP entry.
150 * \returns A pointer to a new ARP Entry.
151 */
153 /**
154 * \brief Remove an entry.
155 * \param entry pointer to delete it from the list
156 */
157 void Remove(ArpCache::Entry* entry);
158 /**
159 * \brief Clear the ArpCache of all entries
160 */
161 void Flush();
162
163 /**
164 * \brief Print the ARP cache entries
165 *
166 * \param stream the ostream the ARP cache entries is printed to
167 */
169
170 /**
171 * \brief Clear the ArpCache of all Auto-Generated entries
172 */
174
175 /**
176 * \brief Pair of a packet and an Ipv4 header.
177 */
178 typedef std::pair<Ptr<Packet>, Ipv4Header> Ipv4PayloadHeaderPair;
179
180 /**
181 * \brief A record that that holds information about an ArpCache entry
182 */
183 class Entry
184 {
185 public:
186 /**
187 * \brief Constructor
188 * \param arp The ArpCache this entry belongs to
189 */
190 Entry(ArpCache* arp);
191
192 /**
193 * \brief Changes the state of this entry to dead
194 */
195 void MarkDead();
196 /**
197 * \param macAddress
198 */
199 void MarkAlive(Address macAddress);
200 /**
201 * \param waiting
202 */
204 /**
205 * \brief Changes the state of this entry to Permanent.
206 *
207 * The entry must have a valid MacAddress.
208 */
209 void MarkPermanent();
210 /**
211 * \brief Changes the state of this entry to auto-generated.
212 *
213 * The entry must have a valid MacAddress.
214 */
215 void MarkAutoGenerated();
216 /**
217 * \param waiting
218 * \return
219 */
221 /**
222 * \return True if the state of this entry is dead; false otherwise.
223 */
224 bool IsDead();
225 /**
226 * \return True if the state of this entry is alive; false otherwise.
227 */
228 bool IsAlive();
229 /**
230 * \return True if the state of this entry is wait_reply; false otherwise.
231 */
232 bool IsWaitReply();
233 /**
234 * \return True if the state of this entry is permanent; false otherwise.
235 */
236 bool IsPermanent();
237 /**
238 * \return True if the state of this entry is auto-generated; false otherwise.
239 */
240 bool IsAutoGenerated();
241 /**
242 * \return The MacAddress of this entry
243 */
244 Address GetMacAddress() const;
245 /**
246 * \return The Ipv4Address for this entry
247 */
249 /**
250 * \param macAddress The MacAddress for this entry
251 */
252 void SetMacAddress(Address macAddress);
253 /**
254 * \param destination The Ipv4Address for this entry
255 */
256 void SetIpv4Address(Ipv4Address destination);
257 /**
258 * \return True if this entry has timed out; false otherwise.
259 *
260 * This function returns true if the time elapsed strictly exceeds
261 * the timeout value (i.e., is not less than or equal to the timeout).
262 */
263 bool IsExpired() const;
264 /**
265 * \returns 0 is no packet is pending, the next packet to send if
266 * packets are pending.
267 */
269 /**
270 * \brief Clear the pending packet list
271 */
272 void ClearPendingPacket();
273 /**
274 * \returns number of retries that have been sent for an ArpRequest
275 * in WaitReply state.
276 */
277 uint32_t GetRetries() const;
278 /**
279 * \brief Increment the counter of number of retries for an entry
280 */
281 void IncrementRetries();
282 /**
283 * \brief Zero the counter of number of retries for an entry
284 */
285 void ClearRetries();
286
287 /**
288 * \brief Update the entry when seeing a packet
289 */
290 void UpdateSeen();
291
292 private:
293 /**
294 * \brief ARP cache entry states
295 */
297 {
303 };
304
305 /**
306 * \brief Returns the entry timeout
307 * \returns the entry timeout
308 */
309 Time GetTimeout() const;
310
311 ArpCache* m_arp; //!< pointer to the ARP cache owning the entry
312 ArpCacheEntryState_e m_state; //!< state of the entry
313 Time m_lastSeen; //!< last moment a packet from that address has been seen
314 Address m_macAddress; //!< entry's MAC address
315 Ipv4Address m_ipv4Address; //!< entry's IP address
316 std::list<Ipv4PayloadHeaderPair> m_pending; //!< list of pending packets for the entry's IP
317 uint32_t m_retries; //!< rerty counter
318 };
319
320 private:
321 /**
322 * \brief ARP Cache container
323 */
324 typedef std::map<Ipv4Address, ArpCache::Entry*> Cache;
325 /**
326 * \brief ARP Cache container iterator
327 */
328 typedef std::map<Ipv4Address, ArpCache::Entry*>::iterator CacheI;
329
330 void DoDispose() override;
331
332 Ptr<NetDevice> m_device; //!< NetDevice associated with the cache
333 Ptr<Ipv4Interface> m_interface; //!< Ipv4Interface associated with the cache
334 Time m_aliveTimeout; //!< cache alive state timeout
335 Time m_deadTimeout; //!< cache dead state timeout
336 Time m_waitReplyTimeout; //!< cache reply state timeout
337 EventId m_waitReplyTimer; //!< cache alive state timer
339 m_arpRequestCallback; //!< reply timeout callback
340 uint32_t m_maxRetries; //!< max retries for a resolution
341
342 /**
343 * This function is an event handler for the event that the
344 * ArpCache wants to check whether it must retry any Arp requests.
345 * If there are no Arp requests pending, this event is not scheduled.
346 */
348 uint32_t m_pendingQueueSize; //!< number of packets waiting for a resolution
349 Cache m_arpCache; //!< the ARP cache
351 m_dropTrace; //!< trace for packets dropped by the ARP cache queue
352};
353
354} // namespace ns3
355
356#endif /* ARP_CACHE_H */
a polymophic address class
Definition: address.h:101
A record that that holds information about an ArpCache entry.
Definition: arp-cache.h:184
bool IsDead()
Definition: arp-cache.cc:390
void SetIpv4Address(Ipv4Address destination)
Definition: arp-cache.cc:520
void MarkAutoGenerated()
Changes the state of this entry to auto-generated.
Definition: arp-cache.cc:457
bool IsAlive()
Definition: arp-cache.cc:397
std::list< Ipv4PayloadHeaderPair > m_pending
list of pending packets for the entry's IP
Definition: arp-cache.h:316
ArpCache * m_arp
pointer to the ARP cache owning the entry
Definition: arp-cache.h:311
Ipv4Address m_ipv4Address
entry's IP address
Definition: arp-cache.h:315
Time m_lastSeen
last moment a packet from that address has been seen
Definition: arp-cache.h:313
bool UpdateWaitReply(Ipv4PayloadHeaderPair waiting)
Definition: arp-cache.cc:468
Address m_macAddress
entry's MAC address
Definition: arp-cache.h:314
Address GetMacAddress() const
Definition: arp-cache.cc:499
void ClearPendingPacket()
Clear the pending packet list.
Definition: arp-cache.cc:574
void MarkPermanent()
Changes the state of this entry to Permanent.
Definition: arp-cache.cc:446
bool IsExpired() const
Definition: arp-cache.cc:547
void ClearRetries()
Zero the counter of number of retries for an entry.
Definition: arp-cache.cc:603
ArpCacheEntryState_e
ARP cache entry states.
Definition: arp-cache.h:297
@ WAIT_REPLY
Definition: arp-cache.h:299
@ ALIVE
Definition: arp-cache.h:298
@ STATIC_AUTOGENERATED
Definition: arp-cache.h:302
@ PERMANENT
Definition: arp-cache.h:301
@ DEAD
Definition: arp-cache.h:300
uint32_t GetRetries() const
Definition: arp-cache.cc:588
void UpdateSeen()
Update the entry when seeing a packet.
Definition: arp-cache.cc:581
uint32_t m_retries
rerty counter
Definition: arp-cache.h:317
bool IsWaitReply()
Definition: arp-cache.cc:404
void IncrementRetries()
Increment the counter of number of retries for an entry.
Definition: arp-cache.cc:595
void MarkAlive(Address macAddress)
Definition: arp-cache.cc:435
bool IsAutoGenerated()
Definition: arp-cache.cc:418
void MarkDead()
Changes the state of this entry to dead.
Definition: arp-cache.cc:425
void SetMacAddress(Address macAddress)
Definition: arp-cache.cc:506
void MarkWaitReply(Ipv4PayloadHeaderPair waiting)
Definition: arp-cache.cc:485
Ipv4PayloadHeaderPair DequeuePending()
Definition: arp-cache.cc:557
ArpCacheEntryState_e m_state
state of the entry
Definition: arp-cache.h:312
Ipv4Address GetIpv4Address() const
Definition: arp-cache.cc:513
bool IsPermanent()
Definition: arp-cache.cc:411
Time GetTimeout() const
Returns the entry timeout.
Definition: arp-cache.cc:527
An ARP cache.
Definition: arp-cache.h:53
Time GetWaitReplyTimeout() const
Get the time the entry will be in WAIT_REPLY state.
Definition: arp-cache.cc:170
void Remove(ArpCache::Entry *entry)
Remove an entry.
Definition: arp-cache.cc:364
void SetWaitReplyTimeout(Time waitReplyTimeout)
Set the time the entry will be in WAIT_REPLY state.
Definition: arp-cache.cc:149
void HandleWaitReplyTimeout()
This function is an event handler for the event that the ArpCache wants to check whether it must retr...
Definition: arp-cache.cc:197
Time GetAliveTimeout() const
Get the time the entry will be in ALIVE state (unless refreshed)
Definition: arp-cache.cc:156
std::map< Ipv4Address, ArpCache::Entry * > Cache
ARP Cache container.
Definition: arp-cache.h:324
uint32_t m_maxRetries
max retries for a resolution
Definition: arp-cache.h:340
EventId m_waitReplyTimer
cache alive state timer
Definition: arp-cache.h:337
Time m_aliveTimeout
cache alive state timeout
Definition: arp-cache.h:334
ArpCache & operator=(const ArpCache &)=delete
void DoDispose() override
Destructor implementation.
Definition: arp-cache.cc:99
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:177
Time m_deadTimeout
cache dead state timeout
Definition: arp-cache.h:335
Time m_waitReplyTimeout
cache reply state timeout
Definition: arp-cache.h:336
Ptr< Ipv4Interface > m_interface
Ipv4Interface associated with the cache.
Definition: arp-cache.h:333
void PrintArpCache(Ptr< OutputStreamWrapper > stream)
Print the ARP cache entries.
Definition: arp-cache.cc:262
void Flush()
Clear the ArpCache of all entries.
Definition: arp-cache.cc:245
void SetAliveTimeout(Time aliveTimeout)
Set the time the entry will be in ALIVE state (unless refreshed)
Definition: arp-cache.cc:135
uint32_t m_pendingQueueSize
number of packets waiting for a resolution
Definition: arp-cache.h:348
TracedCallback< Ptr< const Packet > > m_dropTrace
trace for packets dropped by the ARP cache queue
Definition: arp-cache.h:351
ArpCache::Entry * Add(Ipv4Address to)
Add an Ipv4Address to this ARP cache.
Definition: arp-cache.cc:352
void SetDevice(Ptr< NetDevice > device, Ptr< Ipv4Interface > interface)
Set the NetDevice and Ipv4Interface associated with the ArpCache.
Definition: arp-cache.cc:113
Ptr< Ipv4Interface > GetInterface() const
Returns the Ipv4Interface that this ARP cache is associated with.
Definition: arp-cache.cc:128
std::list< ArpCache::Entry * > LookupInverse(Address destination)
Do lookup in the ARP cache against a MAC address.
Definition: arp-cache.cc:323
Callback< void, Ptr< const ArpCache >, Ipv4Address > m_arpRequestCallback
reply timeout callback
Definition: arp-cache.h:339
std::map< Ipv4Address, ArpCache::Entry * >::iterator CacheI
ARP Cache container iterator.
Definition: arp-cache.h:328
ArpCache::Entry * Lookup(Ipv4Address destination)
Do lookup in the ARP cache against an IP address.
Definition: arp-cache.cc:340
Cache m_arpCache
the ARP cache
Definition: arp-cache.h:349
void RemoveAutoGeneratedEntries()
Clear the ArpCache of all Auto-Generated entries.
Definition: arp-cache.cc:306
~ArpCache() override
Definition: arp-cache.cc:93
Ptr< NetDevice > m_device
NetDevice associated with the cache.
Definition: arp-cache.h:332
void StartWaitReplyTimer()
This method will schedule a timeout at WaitReplyTimeout interval in the future, unless a timer is alr...
Definition: arp-cache.cc:184
Time GetDeadTimeout() const
Get the time the entry will be in DEAD state before being removed.
Definition: arp-cache.cc:163
static TypeId GetTypeId()
Get the type ID.
Definition: arp-cache.cc:41
std::pair< Ptr< Packet >, Ipv4Header > Ipv4PayloadHeaderPair
Pair of a packet and an Ipv4 header.
Definition: arp-cache.h:178
void SetDeadTimeout(Time deadTimeout)
Set the time the entry will be in DEAD state before being removed.
Definition: arp-cache.cc:142
Ptr< NetDevice > GetDevice() const
Returns the NetDevice that this ARP cache is associated with.
Definition: arp-cache.cc:121
ArpCache(const ArpCache &)=delete
Callback template class.
Definition: callback.h:438
An identifier for simulation events.
Definition: event-id.h:55
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
Packet header for IPv4.
Definition: ipv4-header.h:34
A base class which provides memory management and object aggregation.
Definition: object.h:89
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
Forward calls to a chain of Callback.
a unique identifier for an interface.
Definition: type-id.h:59
Every class exported by the ns3 library is enclosed in the ns3 namespace.