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 * 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: Yufei Cheng <yfcheng@ittc.ku.edu>
18 *
19 * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director
20 * ResiliNets Research Group https://resilinets.org/
21 * Information and Telecommunication Technology Center (ITTC)
22 * and Department of Electrical Engineering and Computer Science
23 * The University of Kansas Lawrence, KS USA.
24 *
25 * Work supported in part by NSF FIND (Future Internet Design) Program
26 * under grant CNS-0626918 (Postmodern Internet Architecture),
27 * NSF grant CNS-1050226 (Multilayer Network Resilience Analysis and Experimentation on GENI),
28 * US Department of Defense (DoD), and ITTC at The University of Kansas.
29 */
30
31#include "dsr-maintain-buff.h"
32
33#include "ns3/ipv4-route.h"
34#include "ns3/log.h"
35#include "ns3/socket.h"
36
37#include <algorithm>
38#include <functional>
39
40namespace ns3
41{
42
43NS_LOG_COMPONENT_DEFINE("DsrMaintainBuffer");
44
45namespace dsr
46{
47
50{
51 Purge();
52 return m_maintainBuffer.size();
53}
54
55bool
57{
58 Purge();
59 for (auto i = m_maintainBuffer.begin(); i != m_maintainBuffer.end(); ++i)
60 {
61 // NS_LOG_INFO ("nexthop " << i->GetNextHop () << " " << entry.GetNextHop () << " our
62 // add " << i->GetOurAdd () << " " << entry.GetOurAdd ()
63 // << " src " << i->GetSrc () << " " << entry.GetSrc () << "
64 // dst " << i->GetDst () << " " << entry.GetDst ()
65 // << " ackId " << i->GetAckId () << " " << entry.GetAckId ()
66 // << " SegsLeft " << (uint32_t)i->GetSegsLeft () << " " <<
67 // (uint32_t)entry.GetSegsLeft ()
68 // );
69
70 if ((i->GetNextHop() == entry.GetNextHop()) && (i->GetOurAdd() == entry.GetOurAdd()) &&
71 (i->GetSrc() == entry.GetSrc()) && (i->GetDst() == entry.GetDst()) &&
72 (i->GetAckId() == entry.GetAckId()) && (i->GetSegsLeft() == entry.GetSegsLeft()))
73 {
74 NS_LOG_DEBUG("Same maintenance entry found");
75 return false;
76 }
77 }
78
80 if (m_maintainBuffer.size() >= m_maxLen)
81 {
82 NS_LOG_DEBUG("Drop the most aged packet");
83 m_maintainBuffer.erase(m_maintainBuffer.begin()); // Drop the most aged packet
84 }
85 m_maintainBuffer.push_back(entry);
86 return true;
87}
88
89void
91{
92 NS_LOG_FUNCTION(this << nextHop);
93 Purge();
94 NS_LOG_INFO("Drop Packet With next hop " << nextHop);
95
96 auto new_end =
97 std::remove_if(m_maintainBuffer.begin(),
98 m_maintainBuffer.end(),
99 [&](const DsrMaintainBuffEntry& en) { return en.GetNextHop() == nextHop; });
100 m_maintainBuffer.erase(new_end, m_maintainBuffer.end());
101}
102
103bool
105{
106 Purge();
107 for (auto i = m_maintainBuffer.begin(); i != m_maintainBuffer.end(); ++i)
108 {
109 if (i->GetNextHop() == nextHop)
110 {
111 entry = *i;
112 i = m_maintainBuffer.erase(i);
113 NS_LOG_DEBUG("Packet size while dequeuing " << entry.GetPacket()->GetSize());
114 return true;
115 }
116 }
117 return false;
118}
119
120bool
122{
123 for (auto i = m_maintainBuffer.begin(); i != m_maintainBuffer.end(); ++i)
124 {
125 if (i->GetNextHop() == nextHop)
126 {
127 NS_LOG_DEBUG("Found the packet in maintenance buffer");
128 return true;
129 }
130 }
131 return false;
132}
133
134bool
136{
137 for (auto i = m_maintainBuffer.begin(); i != m_maintainBuffer.end(); ++i)
138 {
139 // NS_LOG_DEBUG ("nexthop " << i->GetNextHop () << " " << entry.GetNextHop () << " our
140 // address " << i->GetOurAdd () << " " << entry.GetOurAdd ()
141 // << " src " << i->GetSrc () << " " << entry.GetSrc () << "
142 // dst " << i->GetDst () << " " << entry.GetDst ()
143 // << " ackId " << i->GetAckId () << " " << entry.GetAckId
144 // ());
145
146 if ((i->GetOurAdd() == entry.GetOurAdd()) && (i->GetNextHop() == entry.GetNextHop()) &&
147 (i->GetSrc() == entry.GetSrc()) && (i->GetDst() == entry.GetDst()) &&
148 (i->GetAckId() == entry.GetAckId()) && (i->GetSegsLeft() == entry.GetSegsLeft()))
149 {
150 i = m_maintainBuffer.erase(
151 i); // Erase the same maintain buffer entry for the received packet
152 return true;
153 }
154 }
155 return false;
156}
157
158bool
160{
161 for (auto i = m_maintainBuffer.begin(); i != m_maintainBuffer.end(); ++i)
162 {
163 // NS_LOG_DEBUG ("nexthop " << i->GetNextHop () << " " << entry.GetNextHop () << " our
164 // address " << i->GetOurAdd () << " " << entry.GetOurAdd ()
165 // << " src " << i->GetSrc () << " " << entry.GetSrc () << "
166 // dst " << i->GetDst () << " " << entry.GetDst ()
167 // << " ackId " << i->GetAckId () << " " << entry.GetAckId
168 // ());
169
170 if ((i->GetOurAdd() == entry.GetOurAdd()) && (i->GetNextHop() == entry.GetNextHop()) &&
171 (i->GetSrc() == entry.GetSrc()) && (i->GetDst() == entry.GetDst()) &&
172 (i->GetAckId() == entry.GetAckId()))
173 {
174 i = m_maintainBuffer.erase(
175 i); // Erase the same maintain buffer entry for the received packet
176 return true;
177 }
178 }
179 return false;
180}
181
182bool
184{
185 NS_LOG_DEBUG("The maintenance buffer size " << m_maintainBuffer.size());
186 for (auto i = m_maintainBuffer.begin(); i != m_maintainBuffer.end(); ++i)
187 {
188 // NS_LOG_DEBUG ("src " << i->GetSrc () << " " << entry.GetSrc () << " dst " <<
189 // i->GetDst () << " " << entry.GetDst ()
190 // << " SegsLeft " << (uint32_t)i->GetSegsLeft () << " " <<
191 // (uint32_t)entry.GetSegsLeft () << " ackId " <<
192 // (uint32_t)i->GetAckId () << " "
193 // << (uint32_t)entry.GetAckId ()
194 // );
195
196 if ((i->GetSrc() == entry.GetSrc()) && (i->GetDst() == entry.GetDst()) &&
197 (i->GetSegsLeft() == entry.GetSegsLeft()) && (i->GetAckId() == entry.GetAckId()))
198 {
199 i = m_maintainBuffer.erase(
200 i); // Erase the same maintain buffer entry for the promisc received packet
201 return true;
202 }
203 }
204 return false;
205}
206
207bool
209{
210 NS_LOG_DEBUG("The maintenance buffer size " << m_maintainBuffer.size());
211 for (auto i = m_maintainBuffer.begin(); i != m_maintainBuffer.end(); ++i)
212 {
213 // NS_LOG_DEBUG ("src " << i->GetSrc () << " " << entry.GetSrc () << " dst " <<
214 // i->GetDst () << " " << entry.GetDst ()
215 // << " OurAddress " << i->GetOurAdd () << " " << entry.GetOurAdd
216 // () << " next hop " << i->GetNextHop () << " "
217 // << entry.GetNextHop ()
218 // );
219
220 if ((i->GetSrc() == entry.GetSrc()) && (i->GetDst() == entry.GetDst()) &&
221 (i->GetOurAdd() == entry.GetOurAdd()) && (i->GetNextHop() == entry.GetNextHop()))
222 {
223 i = m_maintainBuffer.erase(
224 i); // Erase the same maintain buffer entry for the promisc received packet
225 return true;
226 }
227 }
228 return false;
229}
230
231/// IsExpired structure
232struct IsExpired
233{
234 /**
235 * \brief comparison operator
236 * \param e maintain buffer entry
237 * \return true if the entry is expired
238 */
239 bool operator()(const DsrMaintainBuffEntry& e) const
240 {
241 // NS_LOG_DEBUG("Expire time for packet in req queue: "<<e.GetExpireTime ());
242 return (e.GetExpireTime() < Seconds(0));
243 }
244};
245
246void
248{
249 NS_LOG_DEBUG("Purging Maintenance Buffer");
250 IsExpired pred;
251 m_maintainBuffer.erase(std::remove_if(m_maintainBuffer.begin(), m_maintainBuffer.end(), pred),
252 m_maintainBuffer.end());
253}
254
255} // namespace dsr
256} // namespace ns3
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:861
DSR Maintain Buffer Entry.
uint8_t GetSegsLeft() const
Get segments left.
void SetExpireTime(Time exp)
Set expiration time.
Ipv4Address GetSrc() const
Get source address.
Ptr< const Packet > GetPacket() const
Get packet.
uint16_t GetAckId() const
Get acknowledge ID.
Ipv4Address GetOurAdd() const
Get local address of entry.
Time GetExpireTime() const
Get expiration time.
Ipv4Address GetNextHop() const
Get next hop of entry.
Ipv4Address GetDst() const
Get destination address.
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:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
#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:275
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
Every class exported by the ns3 library is enclosed in the ns3 namespace.
IsExpired structure.
bool operator()(const DsrMaintainBuffEntry &e) const
comparison operator