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 except auto-generated entries
149 *
150 * If you wish to remove all entries, including auto-generated entries,
151 * call this method and then also call RemoveAutoGeneratedEntries().
152 *
153 * @see RemoveAutoGenerated
154 */
155 void Flush();
156
157 /**
158 * @brief Print the ARP cache entries
159 *
160 * @param stream the ostream the ARP cache entries is printed to
161 */
163
164 /**
165 * @brief Clear the ArpCache of all Auto-Generated entries
166 */
168
169 /**
170 * @brief Pair of a packet and an Ipv4 header.
171 */
172 typedef std::pair<Ptr<Packet>, Ipv4Header> Ipv4PayloadHeaderPair;
173
174 /**
175 * @brief A record that that holds information about an ArpCache entry
176 */
177 class Entry
178 {
179 public:
180 /**
181 * @brief Constructor
182 * @param arp The ArpCache this entry belongs to
183 */
184 Entry(ArpCache* arp);
185
186 /**
187 * @brief Changes the state of this entry to dead
188 */
189 void MarkDead();
190 /**
191 * @param macAddress
192 */
193 void MarkAlive(Address macAddress);
194 /**
195 * @param waiting
196 */
198 /**
199 * @brief Changes the state of this entry to Permanent.
200 *
201 * The entry must have a valid MacAddress.
202 */
203 void MarkPermanent();
204 /**
205 * @brief Changes the state of this entry to auto-generated.
206 *
207 * The entry must have a valid MacAddress.
208 */
209 void MarkAutoGenerated();
210 /**
211 * @param waiting
212 * @return
213 */
215 /**
216 * @return True if the state of this entry is dead; false otherwise.
217 */
218 bool IsDead();
219 /**
220 * @return True if the state of this entry is alive; false otherwise.
221 */
222 bool IsAlive();
223 /**
224 * @return True if the state of this entry is wait_reply; false otherwise.
225 */
226 bool IsWaitReply();
227 /**
228 * @return True if the state of this entry is permanent; false otherwise.
229 */
230 bool IsPermanent();
231 /**
232 * @return True if the state of this entry is auto-generated; false otherwise.
233 */
234 bool IsAutoGenerated();
235 /**
236 * @return The MacAddress of this entry
237 */
238 Address GetMacAddress() const;
239 /**
240 * @return The Ipv4Address for this entry
241 */
243 /**
244 * @param macAddress The MacAddress for this entry
245 */
246 void SetMacAddress(Address macAddress);
247 /**
248 * @param destination The Ipv4Address for this entry
249 */
250 void SetIpv4Address(Ipv4Address destination);
251 /**
252 * @return True if this entry has timed out; false otherwise.
253 *
254 * This function returns true if the time elapsed strictly exceeds
255 * the timeout value (i.e., is not less than or equal to the timeout).
256 */
257 bool IsExpired() const;
258 /**
259 * @returns 0 is no packet is pending, the next packet to send if
260 * packets are pending.
261 */
263 /**
264 * @brief Clear the pending packet list
265 */
266 void ClearPendingPacket();
267 /**
268 * @returns number of retries that have been sent for an ArpRequest
269 * in WaitReply state.
270 */
271 uint32_t GetRetries() const;
272 /**
273 * @brief Increment the counter of number of retries for an entry
274 */
275 void IncrementRetries();
276 /**
277 * @brief Zero the counter of number of retries for an entry
278 */
279 void ClearRetries();
280
281 /**
282 * @brief Update the entry when seeing a packet
283 */
284 void UpdateSeen();
285
286 /**
287 * @brief Print this ARP entry to the given output stream
288 *
289 * @param os The output stream to which this ARP entry is printed
290 */
291 void Print(std::ostream& os) const;
292
293 /**
294 * @brief Returns the entry timeout
295 * @returns the entry timeout
296 */
297 Time GetTimeout() const;
298
299 private:
300 /**
301 * @brief ARP cache entry states
302 */
311
312 ArpCache* m_arp; //!< pointer to the ARP cache owning the entry
313 ArpCacheEntryState_e m_state; //!< state of the entry
314 Time m_lastSeen; //!< last moment a packet from that address has been seen
315 Address m_macAddress; //!< entry's MAC address
316 Ipv4Address m_ipv4Address; //!< entry's IP address
317 std::list<Ipv4PayloadHeaderPair> m_pending; //!< list of pending packets for the entry's IP
318 uint32_t m_retries; //!< retry counter
319 };
320
321 private:
322 /**
323 * @brief ARP Cache container
324 */
325 typedef std::map<Ipv4Address, ArpCache::Entry*> Cache;
326 /**
327 * @brief ARP Cache container iterator
328 */
329 typedef std::map<Ipv4Address, ArpCache::Entry*>::iterator CacheI;
330
331 void DoDispose() override;
332
333 Ptr<NetDevice> m_device; //!< NetDevice associated with the cache
334 Ptr<Ipv4Interface> m_interface; //!< Ipv4Interface associated with the cache
335 Time m_aliveTimeout; //!< cache alive state timeout
336 Time m_deadTimeout; //!< cache dead state timeout
337 Time m_waitReplyTimeout; //!< cache reply state timeout
338 EventId m_waitReplyTimer; //!< cache alive state timer
340 m_arpRequestCallback; //!< reply timeout callback
341 uint32_t m_maxRetries; //!< max retries for a resolution
342
343 /**
344 * This function is an event handler for the event that the
345 * ArpCache wants to check whether it must retry any Arp requests.
346 * If there are no Arp requests pending, this event is not scheduled.
347 */
349 uint32_t m_pendingQueueSize; //!< number of packets waiting for a resolution
350 Cache m_arpCache; //!< the ARP cache
352 m_dropTrace; //!< trace for packets dropped by the ARP cache queue
353};
354
355/**
356 * @brief Stream insertion operator.
357 *
358 * @param os the stream
359 * @param entry the ARP entry
360 * @returns a reference to the stream
361 */
362std::ostream& operator<<(std::ostream& os, const ArpCache::Entry& entry);
363
364} // namespace ns3
365
366#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:178
bool IsDead()
Definition arp-cache.cc:393
void SetIpv4Address(Ipv4Address destination)
Definition arp-cache.cc:523
void MarkAutoGenerated()
Changes the state of this entry to auto-generated.
Definition arp-cache.cc:460
bool IsAlive()
Definition arp-cache.cc:400
std::list< Ipv4PayloadHeaderPair > m_pending
list of pending packets for the entry's IP
Definition arp-cache.h:317
ArpCache * m_arp
pointer to the ARP cache owning the entry
Definition arp-cache.h:312
Ipv4Address m_ipv4Address
entry's IP address
Definition arp-cache.h:316
Time m_lastSeen
last moment a packet from that address has been seen
Definition arp-cache.h:314
bool UpdateWaitReply(Ipv4PayloadHeaderPair waiting)
Definition arp-cache.cc:471
Address m_macAddress
entry's MAC address
Definition arp-cache.h:315
Address GetMacAddress() const
Definition arp-cache.cc:502
void ClearPendingPacket()
Clear the pending packet list.
Definition arp-cache.cc:576
void MarkPermanent()
Changes the state of this entry to Permanent.
Definition arp-cache.cc:449
bool IsExpired() const
Definition arp-cache.cc:549
void ClearRetries()
Zero the counter of number of retries for an entry.
Definition arp-cache.cc:605
ArpCacheEntryState_e
ARP cache entry states.
Definition arp-cache.h:304
@ WAIT_REPLY
Definition arp-cache.h:306
@ ALIVE
Definition arp-cache.h:305
@ STATIC_AUTOGENERATED
Definition arp-cache.h:309
@ PERMANENT
Definition arp-cache.h:308
@ DEAD
Definition arp-cache.h:307
uint32_t GetRetries() const
Definition arp-cache.cc:590
void UpdateSeen()
Update the entry when seeing a packet.
Definition arp-cache.cc:583
uint32_t m_retries
retry counter
Definition arp-cache.h:318
bool IsWaitReply()
Definition arp-cache.cc:407
void IncrementRetries()
Increment the counter of number of retries for an entry.
Definition arp-cache.cc:597
void MarkAlive(Address macAddress)
Definition arp-cache.cc:438
bool IsAutoGenerated()
Definition arp-cache.cc:421
void MarkDead()
Changes the state of this entry to dead.
Definition arp-cache.cc:428
void Print(std::ostream &os) const
Print this ARP entry to the given output stream.
Definition arp-cache.cc:612
void SetMacAddress(Address macAddress)
Definition arp-cache.cc:509
void MarkWaitReply(Ipv4PayloadHeaderPair waiting)
Definition arp-cache.cc:488
Ipv4PayloadHeaderPair DequeuePending()
Definition arp-cache.cc:559
ArpCacheEntryState_e m_state
state of the entry
Definition arp-cache.h:313
Ipv4Address GetIpv4Address() const
Definition arp-cache.cc:516
bool IsPermanent()
Definition arp-cache.cc:414
Time GetTimeout() const
Returns the entry timeout.
Definition arp-cache.cc:530
Entry(ArpCache *arp)
Constructor.
Definition arp-cache.cc:384
Time GetWaitReplyTimeout() const
Get the time the entry will be in WAIT_REPLY state.
Definition arp-cache.cc:163
void Remove(ArpCache::Entry *entry)
Remove an entry.
Definition arp-cache.cc:367
void SetWaitReplyTimeout(Time waitReplyTimeout)
Set the time the entry will be in WAIT_REPLY state.
Definition arp-cache.cc:142
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:190
Time GetAliveTimeout() const
Get the time the entry will be in ALIVE state (unless refreshed)
Definition arp-cache.cc:149
std::map< Ipv4Address, ArpCache::Entry * > Cache
ARP Cache container.
Definition arp-cache.h:325
uint32_t m_maxRetries
max retries for a resolution
Definition arp-cache.h:341
EventId m_waitReplyTimer
cache alive state timer
Definition arp-cache.h:338
Time m_aliveTimeout
cache alive state timeout
Definition arp-cache.h:335
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:170
Time m_deadTimeout
cache dead state timeout
Definition arp-cache.h:336
Time m_waitReplyTimeout
cache reply state timeout
Definition arp-cache.h:337
Ptr< Ipv4Interface > m_interface
Ipv4Interface associated with the cache.
Definition arp-cache.h:334
void PrintArpCache(Ptr< OutputStreamWrapper > stream)
Print the ARP cache entries.
Definition arp-cache.cc:263
void Flush()
Clear the ArpCache of all entries except auto-generated entries.
Definition arp-cache.cc:238
void SetAliveTimeout(Time aliveTimeout)
Set the time the entry will be in ALIVE state (unless refreshed)
Definition arp-cache.cc:128
uint32_t m_pendingQueueSize
number of packets waiting for a resolution
Definition arp-cache.h:349
TracedCallback< Ptr< const Packet > > m_dropTrace
trace for packets dropped by the ARP cache queue
Definition arp-cache.h:352
ArpCache::Entry * Add(Ipv4Address to)
Add an Ipv4Address to this ARP cache.
Definition arp-cache.cc:355
void SetDevice(Ptr< NetDevice > device, Ptr< Ipv4Interface > interface)
Set the NetDevice and Ipv4Interface associated with the ArpCache.
Definition arp-cache.cc:106
Ptr< Ipv4Interface > GetInterface() const
Returns the Ipv4Interface that this ARP cache is associated with.
Definition arp-cache.cc:121
std::list< ArpCache::Entry * > LookupInverse(Address destination)
Do lookup in the ARP cache against a MAC address.
Definition arp-cache.cc:326
Callback< void, Ptr< const ArpCache >, Ipv4Address > m_arpRequestCallback
reply timeout callback
Definition arp-cache.h:340
std::map< Ipv4Address, ArpCache::Entry * >::iterator CacheI
ARP Cache container iterator.
Definition arp-cache.h:329
ArpCache::Entry * Lookup(Ipv4Address destination)
Do lookup in the ARP cache against an IP address.
Definition arp-cache.cc:343
Cache m_arpCache
the ARP cache
Definition arp-cache.h:350
void RemoveAutoGeneratedEntries()
Clear the ArpCache of all Auto-Generated entries.
Definition arp-cache.cc:307
~ArpCache() override
Definition arp-cache.cc:82
Ptr< NetDevice > m_device
NetDevice associated with the cache.
Definition arp-cache.h:333
void StartWaitReplyTimer()
This method will schedule a timeout at WaitReplyTimeout interval in the future, unless a timer is alr...
Definition arp-cache.cc:177
Time GetDeadTimeout() const
Get the time the entry will be in DEAD state before being removed.
Definition arp-cache.cc:156
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:172
void SetDeadTimeout(Time deadTimeout)
Set the time the entry will be in DEAD state before being removed.
Definition arp-cache.cc:135
Ptr< NetDevice > GetDevice() const
Returns the NetDevice that this ARP cache is associated with.
Definition arp-cache.cc:114
ArpCache(const ArpCache &)=delete
Callback template class.
Definition callback.h:422
An identifier for simulation events.
Definition event-id.h:44
Ipv4 addresses are stored in host order in this class.
Packet header for IPv4.
Definition ipv4-header.h:23
The IPv4 representation of a network interface.
Network layer to device interface.
Definition net-device.h:87
Object()
Constructor.
Definition object.cc:96
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:67
Simulation virtual time values and global simulation resolution.
Definition nstime.h:96
Forward calls to a chain of Callback.
a unique identifier for an interface.
Definition type-id.h:49
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition angles.cc:148