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