A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
dsr-passive-buff.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011 Yufei Cheng
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Yufei Cheng <yfcheng@ittc.ku.edu>
7 *
8 * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director
9 * ResiliNets Research Group https://resilinets.org/
10 * Information and Telecommunication Technology Center (ITTC)
11 * and Department of Electrical Engineering and Computer Science
12 * The University of Kansas Lawrence, KS USA.
13 *
14 * Work supported in part by NSF FIND (Future Internet Design) Program
15 * under grant CNS-0626918 (Postmodern Internet Architecture),
16 * NSF grant CNS-1050226 (Multilayer Network Resilience Analysis and Experimentation on GENI),
17 * US Department of Defense (DoD), and ITTC at The University of Kansas.
18 */
19
20#include "dsr-passive-buff.h"
21
22#include "ns3/ipv4-route.h"
23#include "ns3/log.h"
24#include "ns3/socket.h"
25
26#include <algorithm>
27#include <functional>
28
29namespace ns3
30{
31
32NS_LOG_COMPONENT_DEFINE("DsrPassiveBuffer");
33
34namespace dsr
35{
36
37NS_OBJECT_ENSURE_REGISTERED(DsrPassiveBuffer);
38
39TypeId
41{
42 static TypeId tid = TypeId("ns3::dsr::DsrPassiveBuffer")
44 .SetGroupName("Dsr")
45 .AddConstructor<DsrPassiveBuffer>();
46 return tid;
47}
48
52
56
59{
60 Purge();
61 return m_passiveBuffer.size();
62}
63
64bool
66{
67 Purge();
68 for (auto i = m_passiveBuffer.begin(); i != m_passiveBuffer.end(); ++i)
69 {
70 // NS_LOG_INFO ("packet id " << i->GetPacket ()->GetUid () << " " << entry.GetPacket
71 // ()->GetUid () << " source " << i->GetSource () << " " << entry.GetSource ()
72 // << " dst " << i->GetDestination () << " " <<
73 // entry.GetDestination () << " identification " <<
74 // i->GetIdentification () << " "
75 // << entry.GetIdentification () << " fragment " <<
76 // i->GetFragmentOffset () << " " <<
77 // entry.GetFragmentOffset ()
78 // << " segLeft " << i->GetSegsLeft () << " " <<
79 // entry.GetSegsLeft ());
80
81 if ((i->GetPacket()->GetUid() == entry.GetPacket()->GetUid()) &&
82 (i->GetSource() == entry.GetSource()) && (i->GetNextHop() == entry.GetNextHop()) &&
83 (i->GetDestination() == entry.GetDestination()) &&
84 (i->GetIdentification() == entry.GetIdentification()) &&
85 (i->GetFragmentOffset() == entry.GetFragmentOffset()) &&
86 (i->GetSegsLeft() == entry.GetSegsLeft() + 1))
87 {
88 return false;
89 }
90 }
91
92 entry.SetExpireTime(m_passiveBufferTimeout); // Initialize the send buffer timeout
93 /*
94 * Drop the most aged packet when buffer reaches to max
95 */
96 if (m_passiveBuffer.size() >= m_maxLen)
97 {
98 Drop(m_passiveBuffer.front(), "Drop the most aged packet"); // Drop the most aged packet
99 m_passiveBuffer.erase(m_passiveBuffer.begin());
100 }
101 // enqueue the entry
102 m_passiveBuffer.push_back(entry);
103 return true;
104}
105
106bool
108{
109 for (auto i = m_passiveBuffer.begin(); i != m_passiveBuffer.end(); ++i)
110 {
111 // NS_LOG_INFO ("packet id " << i->GetPacket ()->GetUid () << " " << entry.GetPacket
112 // ()->GetUid () << " source " << i->GetSource () << " " << entry.GetSource ()
113 // << " dst " << i->GetDestination () << " " <<
114 // entry.GetDestination () << " identification " <<
115 // i->GetIdentification () << " "
116 // << entry.GetIdentification () << " fragment " <<
117 // i->GetFragmentOffset () << " " <<
118 // entry.GetFragmentOffset ()
119 // << " segLeft " << (uint32_t) i->GetSegsLeft () << " "
120 // << (uint32_t) entry.GetSegsLeft ());
121
122 if ((i->GetPacket()->GetUid() == entry.GetPacket()->GetUid()) &&
123 (i->GetSource() == entry.GetSource()) && (i->GetNextHop() == entry.GetNextHop()) &&
124 (i->GetDestination() == entry.GetDestination()) &&
125 (i->GetIdentification() == entry.GetIdentification()) &&
126 (i->GetFragmentOffset() == entry.GetFragmentOffset()) &&
127 (i->GetSegsLeft() == entry.GetSegsLeft() + 1))
128 {
129 i = m_passiveBuffer.erase(
130 i); // Erase the same maintain buffer entry for the received packet
131 return true;
132 }
133 }
134 return false;
135}
136
137bool
139{
140 Purge();
141 /*
142 * Dequeue the entry with destination address dst
143 */
144 for (auto i = m_passiveBuffer.begin(); i != m_passiveBuffer.end(); ++i)
145 {
146 if (i->GetDestination() == dst)
147 {
148 entry = *i;
149 i = m_passiveBuffer.erase(i);
150 NS_LOG_DEBUG("Packet size while dequeuing " << entry.GetPacket()->GetSize());
151 return true;
152 }
153 }
154 return false;
155}
156
157bool
159{
160 /*
161 * Make sure if the send buffer contains entry with certain dst
162 */
163 for (auto i = m_passiveBuffer.begin(); i != m_passiveBuffer.end(); ++i)
164 {
165 if (i->GetDestination() == dst)
166 {
167 NS_LOG_DEBUG("Found the packet");
168 return true;
169 }
170 }
171 return false;
172}
173
174/// IsExpired structure
175struct IsExpired
176{
177 /**
178 * Check for an expired entry
179 * \param e passive buffer entry
180 * \return true if equal
181 */
182 bool operator()(const DsrPassiveBuffEntry& e) const
183 {
184 // NS_LOG_DEBUG("Expire time for packet in req queue: "<<e.GetExpireTime ());
185 return (e.GetExpireTime() < Seconds(0));
186 }
187};
188
189void
191{
192 /*
193 * Purge the buffer to eliminate expired entries
194 */
195 NS_LOG_DEBUG("The passive buffer size " << m_passiveBuffer.size());
196 IsExpired pred;
197 for (auto i = m_passiveBuffer.begin(); i != m_passiveBuffer.end(); ++i)
198 {
199 if (pred(*i))
200 {
201 NS_LOG_DEBUG("Dropping Queue Packets");
202 Drop(*i, "Drop out-dated packet ");
203 }
204 }
205 m_passiveBuffer.erase(std::remove_if(m_passiveBuffer.begin(), m_passiveBuffer.end(), pred),
206 m_passiveBuffer.end());
207}
208
209void
211{
212 NS_LOG_LOGIC(reason << en.GetPacket()->GetUid() << " " << en.GetDestination());
213 // en.GetErrorCallback () (en.GetPacket (), en.GetDestination (),
214 // Socket::ERROR_NOROUTETOHOST);
215}
216
217void
219{
220 NS_LOG_LOGIC(reason << en.GetPacket()->GetUid() << " " << en.GetSource() << " "
221 << en.GetNextHop());
222 // en.GetErrorCallback () (en.GetPacket (), en.GetDestination (),
223 // Socket::ERROR_NOROUTETOHOST);
224}
225} // namespace dsr
226} // namespace ns3
Ipv4 addresses are stored in host order in this class.
A base class which provides memory management and object aggregation.
Definition object.h:78
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition packet.h:850
uint64_t GetUid() const
Returns the packet's Uid.
Definition packet.cc:401
a unique identifier for an interface.
Definition type-id.h:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
DSR Passive Buffer Entry.
void SetExpireTime(Time exp)
Set expire time.
Ipv4Address GetDestination() const
Get destination address function.
Ptr< const Packet > GetPacket() const
Get packet function.
Ipv4Address GetNextHop() const
Get next hop address function.
Ipv4Address GetSource() const
Get source address function.
Time GetExpireTime() const
Get expire time.
uint8_t GetSegsLeft() const
Get segments left function.
uint16_t GetIdentification() const
Get identification function.
uint16_t GetFragmentOffset() const
Get fragment offset function.
bool Enqueue(DsrPassiveBuffEntry &entry)
Push entry in queue, if there is no entry with the same packet and destination address in queue.
uint32_t GetSize()
Number of entries.
bool Find(Ipv4Address dst)
Finds whether a packet with destination dst exists in the queue.
bool Dequeue(Ipv4Address dst, DsrPassiveBuffEntry &entry)
Return first found (the earliest) entry for given destination.
void DropLink(DsrPassiveBuffEntry en, std::string reason)
Notify that packet is dropped from queue by timeout.
uint32_t m_maxLen
The maximum number of packets that we allow a routing protocol to buffer.
Time m_passiveBufferTimeout
The maximum period of time that a routing protocol is allowed to buffer a packet for,...
void Purge()
Remove all expired entries.
std::vector< DsrPassiveBuffEntry > m_passiveBuffer
The send buffer to cache unsent packet.
void Drop(DsrPassiveBuffEntry en, std::string reason)
Notify that packet is dropped from queue by timeout.
static TypeId GetTypeId()
Get the type ID.
bool AllEqual(DsrPassiveBuffEntry &entry)
Check if all the entries in passive buffer entry is all equal or not.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:257
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition log.h:271
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Every class exported by the ns3 library is enclosed in the ns3 namespace.
IsExpired structure.
bool operator()(const DsrPassiveBuffEntry &e) const
Check for an expired entry.