A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
dsr-maintain-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-maintain-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("DsrMaintainBuffer");
33
34namespace dsr
35{
36
39{
40 Purge();
41 return m_maintainBuffer.size();
42}
43
44bool
46{
47 Purge();
48 for (auto i = m_maintainBuffer.begin(); i != m_maintainBuffer.end(); ++i)
49 {
50 // NS_LOG_INFO ("nexthop " << i->GetNextHop () << " " << entry.GetNextHop () << " our
51 // add " << i->GetOurAdd () << " " << entry.GetOurAdd ()
52 // << " src " << i->GetSrc () << " " << entry.GetSrc () << "
53 // dst " << i->GetDst () << " " << entry.GetDst ()
54 // << " ackId " << i->GetAckId () << " " << entry.GetAckId ()
55 // << " SegsLeft " << (uint32_t)i->GetSegsLeft () << " " <<
56 // (uint32_t)entry.GetSegsLeft ()
57 // );
58
59 if ((i->GetNextHop() == entry.GetNextHop()) && (i->GetOurAdd() == entry.GetOurAdd()) &&
60 (i->GetSrc() == entry.GetSrc()) && (i->GetDst() == entry.GetDst()) &&
61 (i->GetAckId() == entry.GetAckId()) && (i->GetSegsLeft() == entry.GetSegsLeft()))
62 {
63 NS_LOG_DEBUG("Same maintenance entry found");
64 return false;
65 }
66 }
67
68 entry.SetExpireTime(m_maintainBufferTimeout);
69 if (m_maintainBuffer.size() >= m_maxLen)
70 {
71 NS_LOG_DEBUG("Drop the most aged packet");
72 m_maintainBuffer.erase(m_maintainBuffer.begin()); // Drop the most aged packet
73 }
74 m_maintainBuffer.push_back(entry);
75 return true;
76}
77
78void
80{
81 NS_LOG_FUNCTION(this << nextHop);
82 Purge();
83 NS_LOG_INFO("Drop Packet With next hop " << nextHop);
84
85 auto new_end =
86 std::remove_if(m_maintainBuffer.begin(),
87 m_maintainBuffer.end(),
88 [&](const DsrMaintainBuffEntry& en) { return en.GetNextHop() == nextHop; });
90}
91
92bool
94{
95 Purge();
96 for (auto i = m_maintainBuffer.begin(); i != m_maintainBuffer.end(); ++i)
97 {
98 if (i->GetNextHop() == nextHop)
99 {
100 entry = *i;
101 i = m_maintainBuffer.erase(i);
102 NS_LOG_DEBUG("Packet size while dequeuing " << entry.GetPacket()->GetSize());
103 return true;
104 }
105 }
106 return false;
107}
108
109bool
111{
112 for (auto i = m_maintainBuffer.begin(); i != m_maintainBuffer.end(); ++i)
113 {
114 if (i->GetNextHop() == nextHop)
115 {
116 NS_LOG_DEBUG("Found the packet in maintenance buffer");
117 return true;
118 }
119 }
120 return false;
121}
122
123bool
125{
126 for (auto i = m_maintainBuffer.begin(); i != m_maintainBuffer.end(); ++i)
127 {
128 // NS_LOG_DEBUG ("nexthop " << i->GetNextHop () << " " << entry.GetNextHop () << " our
129 // address " << i->GetOurAdd () << " " << entry.GetOurAdd ()
130 // << " src " << i->GetSrc () << " " << entry.GetSrc () << "
131 // dst " << i->GetDst () << " " << entry.GetDst ()
132 // << " ackId " << i->GetAckId () << " " << entry.GetAckId
133 // ());
134
135 if ((i->GetOurAdd() == entry.GetOurAdd()) && (i->GetNextHop() == entry.GetNextHop()) &&
136 (i->GetSrc() == entry.GetSrc()) && (i->GetDst() == entry.GetDst()) &&
137 (i->GetAckId() == entry.GetAckId()) && (i->GetSegsLeft() == entry.GetSegsLeft()))
138 {
139 i = m_maintainBuffer.erase(
140 i); // Erase the same maintain buffer entry for the received packet
141 return true;
142 }
143 }
144 return false;
145}
146
147bool
149{
150 for (auto i = m_maintainBuffer.begin(); i != m_maintainBuffer.end(); ++i)
151 {
152 // NS_LOG_DEBUG ("nexthop " << i->GetNextHop () << " " << entry.GetNextHop () << " our
153 // address " << i->GetOurAdd () << " " << entry.GetOurAdd ()
154 // << " src " << i->GetSrc () << " " << entry.GetSrc () << "
155 // dst " << i->GetDst () << " " << entry.GetDst ()
156 // << " ackId " << i->GetAckId () << " " << entry.GetAckId
157 // ());
158
159 if ((i->GetOurAdd() == entry.GetOurAdd()) && (i->GetNextHop() == entry.GetNextHop()) &&
160 (i->GetSrc() == entry.GetSrc()) && (i->GetDst() == entry.GetDst()) &&
161 (i->GetAckId() == entry.GetAckId()))
162 {
163 i = m_maintainBuffer.erase(
164 i); // Erase the same maintain buffer entry for the received packet
165 return true;
166 }
167 }
168 return false;
169}
170
171bool
173{
174 NS_LOG_DEBUG("The maintenance buffer size " << m_maintainBuffer.size());
175 for (auto i = m_maintainBuffer.begin(); i != m_maintainBuffer.end(); ++i)
176 {
177 // NS_LOG_DEBUG ("src " << i->GetSrc () << " " << entry.GetSrc () << " dst " <<
178 // i->GetDst () << " " << entry.GetDst ()
179 // << " SegsLeft " << (uint32_t)i->GetSegsLeft () << " " <<
180 // (uint32_t)entry.GetSegsLeft () << " ackId " <<
181 // (uint32_t)i->GetAckId () << " "
182 // << (uint32_t)entry.GetAckId ()
183 // );
184
185 if ((i->GetSrc() == entry.GetSrc()) && (i->GetDst() == entry.GetDst()) &&
186 (i->GetSegsLeft() == entry.GetSegsLeft()) && (i->GetAckId() == entry.GetAckId()))
187 {
188 i = m_maintainBuffer.erase(
189 i); // Erase the same maintain buffer entry for the promisc received packet
190 return true;
191 }
192 }
193 return false;
194}
195
196bool
198{
199 NS_LOG_DEBUG("The maintenance buffer size " << m_maintainBuffer.size());
200 for (auto i = m_maintainBuffer.begin(); i != m_maintainBuffer.end(); ++i)
201 {
202 // NS_LOG_DEBUG ("src " << i->GetSrc () << " " << entry.GetSrc () << " dst " <<
203 // i->GetDst () << " " << entry.GetDst ()
204 // << " OurAddress " << i->GetOurAdd () << " " << entry.GetOurAdd
205 // () << " next hop " << i->GetNextHop () << " "
206 // << entry.GetNextHop ()
207 // );
208
209 if ((i->GetSrc() == entry.GetSrc()) && (i->GetDst() == entry.GetDst()) &&
210 (i->GetOurAdd() == entry.GetOurAdd()) && (i->GetNextHop() == entry.GetNextHop()))
211 {
212 i = m_maintainBuffer.erase(
213 i); // Erase the same maintain buffer entry for the promisc received packet
214 return true;
215 }
216 }
217 return false;
218}
219
220/// IsExpired structure
221struct IsExpired
222{
223 /**
224 * @brief comparison operator
225 * @param e maintain buffer entry
226 * @return true if the entry is expired
227 */
228 bool operator()(const DsrMaintainBuffEntry& e) const
229 {
230 // NS_LOG_DEBUG("Expire time for packet in req queue: "<<e.GetExpireTime ());
231 return (e.GetExpireTime().IsStrictlyNegative());
232 }
233};
234
235void
237{
238 NS_LOG_DEBUG("Purging Maintenance Buffer");
240 m_maintainBuffer.erase(std::remove_if(m_maintainBuffer.begin(), m_maintainBuffer.end(), pred),
241 m_maintainBuffer.end());
242}
243
244} // namespace dsr
245} // namespace ns3
Ipv4 addresses are stored in host order in this class.
bool IsStrictlyNegative() const
Exactly equivalent to t < 0.
Definition nstime.h:331
DSR Maintain Buffer Entry.
Time GetExpireTime() const
Get expiration time.
bool Dequeue(Ipv4Address dst, DsrMaintainBuffEntry &entry)
Return first found (the earliest) entry for given destination.
Time m_maintainBufferTimeout
The maximum period of time that a routing protocol is allowed to buffer a packet for,...
void DropPacketWithNextHop(Ipv4Address nextHop)
Remove all packets with next hop IP address dst.
bool AllEqual(DsrMaintainBuffEntry &entry)
Verify if all the elements in the maintenance buffer entry is the same.
bool LinkEqual(DsrMaintainBuffEntry &entry)
Verify if the maintain buffer entry is the same in every field for link ack.
bool Find(Ipv4Address nextHop)
Finds whether a packet with next hop dst exists in the queue.
uint32_t m_maxLen
The maximum number of packets that we allow a routing protocol to buffer.
uint32_t GetSize()
Number of entries.
bool PromiscEqual(DsrMaintainBuffEntry &entry)
Verify if the maintain buffer entry is the same in every field for promiscuous ack.
bool NetworkEqual(DsrMaintainBuffEntry &entry)
Verify if the maintain buffer entry is the same in every field for network ack.
bool Enqueue(DsrMaintainBuffEntry &entry)
Push entry in queue, if there is no entry with the same packet and destination address in queue.
std::vector< DsrMaintainBuffEntry > m_maintainBuffer
The vector of maintain buffer entries.
void Purge()
Remove all expired entries.
#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_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:264
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
Every class exported by the ns3 library is enclosed in the ns3 namespace.
IsExpired structure.
bool operator()(const DsrMaintainBuffEntry &e) const
comparison operator