A Discrete-Event Network Simulator
API
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 
40 namespace ns3
41 {
42 
43 NS_LOG_COMPONENT_DEFINE("DsrMaintainBuffer");
44 
45 namespace dsr
46 {
47 
48 uint32_t
50 {
51  Purge();
52  return m_maintainBuffer.size();
53 }
54 
55 bool
57 {
58  Purge();
59  for (std::vector<DsrMaintainBuffEntry>::const_iterator i = m_maintainBuffer.begin();
60  i != m_maintainBuffer.end();
61  ++i)
62  {
63  // NS_LOG_INFO ("nexthop " << i->GetNextHop () << " " << entry.GetNextHop () << " our
64  // add " << i->GetOurAdd () << " " << entry.GetOurAdd ()
65  // << " src " << i->GetSrc () << " " << entry.GetSrc () << "
66  // dst " << i->GetDst () << " " << entry.GetDst ()
67  // << " ackId " << i->GetAckId () << " " << entry.GetAckId ()
68  // << " SegsLeft " << (uint32_t)i->GetSegsLeft () << " " <<
69  // (uint32_t)entry.GetSegsLeft ()
70  // );
71 
72  if ((i->GetNextHop() == entry.GetNextHop()) && (i->GetOurAdd() == entry.GetOurAdd()) &&
73  (i->GetSrc() == entry.GetSrc()) && (i->GetDst() == entry.GetDst()) &&
74  (i->GetAckId() == entry.GetAckId()) && (i->GetSegsLeft() == entry.GetSegsLeft()))
75  {
76  NS_LOG_DEBUG("Same maintenance entry found");
77  return false;
78  }
79  }
80 
82  if (m_maintainBuffer.size() >= m_maxLen)
83  {
84  NS_LOG_DEBUG("Drop the most aged packet");
85  m_maintainBuffer.erase(m_maintainBuffer.begin()); // Drop the most aged packet
86  }
87  m_maintainBuffer.push_back(entry);
88  return true;
89 }
90 
91 void
93 {
94  NS_LOG_FUNCTION(this << nextHop);
95  Purge();
96  NS_LOG_INFO("Drop Packet With next hop " << nextHop);
97 
98  auto new_end =
99  std::remove_if(m_maintainBuffer.begin(),
100  m_maintainBuffer.end(),
101  [&](const DsrMaintainBuffEntry& en) { return en.GetNextHop() == nextHop; });
102  m_maintainBuffer.erase(new_end, m_maintainBuffer.end());
103 }
104 
105 bool
107 {
108  Purge();
109  for (std::vector<DsrMaintainBuffEntry>::iterator i = m_maintainBuffer.begin();
110  i != m_maintainBuffer.end();
111  ++i)
112  {
113  if (i->GetNextHop() == nextHop)
114  {
115  entry = *i;
116  i = m_maintainBuffer.erase(i);
117  NS_LOG_DEBUG("Packet size while dequeuing " << entry.GetPacket()->GetSize());
118  return true;
119  }
120  }
121  return false;
122 }
123 
124 bool
126 {
127  for (std::vector<DsrMaintainBuffEntry>::const_iterator i = m_maintainBuffer.begin();
128  i != m_maintainBuffer.end();
129  ++i)
130  {
131  if (i->GetNextHop() == nextHop)
132  {
133  NS_LOG_DEBUG("Found the packet in maintenance buffer");
134  return true;
135  }
136  }
137  return false;
138 }
139 
140 bool
142 {
143  for (std::vector<DsrMaintainBuffEntry>::iterator i = m_maintainBuffer.begin();
144  i != m_maintainBuffer.end();
145  ++i)
146  {
147  // NS_LOG_DEBUG ("nexthop " << i->GetNextHop () << " " << entry.GetNextHop () << " our
148  // address " << i->GetOurAdd () << " " << entry.GetOurAdd ()
149  // << " src " << i->GetSrc () << " " << entry.GetSrc () << "
150  // dst " << i->GetDst () << " " << entry.GetDst ()
151  // << " ackId " << i->GetAckId () << " " << entry.GetAckId
152  // ());
153 
154  if ((i->GetOurAdd() == entry.GetOurAdd()) && (i->GetNextHop() == entry.GetNextHop()) &&
155  (i->GetSrc() == entry.GetSrc()) && (i->GetDst() == entry.GetDst()) &&
156  (i->GetAckId() == entry.GetAckId()) && (i->GetSegsLeft() == entry.GetSegsLeft()))
157  {
158  i = m_maintainBuffer.erase(
159  i); // Erase the same maintain buffer entry for the received packet
160  return true;
161  }
162  }
163  return false;
164 }
165 
166 bool
168 {
169  for (std::vector<DsrMaintainBuffEntry>::iterator i = m_maintainBuffer.begin();
170  i != m_maintainBuffer.end();
171  ++i)
172  {
173  // NS_LOG_DEBUG ("nexthop " << i->GetNextHop () << " " << entry.GetNextHop () << " our
174  // address " << i->GetOurAdd () << " " << entry.GetOurAdd ()
175  // << " src " << i->GetSrc () << " " << entry.GetSrc () << "
176  // dst " << i->GetDst () << " " << entry.GetDst ()
177  // << " ackId " << i->GetAckId () << " " << entry.GetAckId
178  // ());
179 
180  if ((i->GetOurAdd() == entry.GetOurAdd()) && (i->GetNextHop() == entry.GetNextHop()) &&
181  (i->GetSrc() == entry.GetSrc()) && (i->GetDst() == entry.GetDst()) &&
182  (i->GetAckId() == entry.GetAckId()))
183  {
184  i = m_maintainBuffer.erase(
185  i); // Erase the same maintain buffer entry for the received packet
186  return true;
187  }
188  }
189  return false;
190 }
191 
192 bool
194 {
195  NS_LOG_DEBUG("The maintenance buffer size " << m_maintainBuffer.size());
196  for (std::vector<DsrMaintainBuffEntry>::iterator i = m_maintainBuffer.begin();
197  i != m_maintainBuffer.end();
198  ++i)
199  {
200  // NS_LOG_DEBUG ("src " << i->GetSrc () << " " << entry.GetSrc () << " dst " <<
201  // i->GetDst () << " " << entry.GetDst ()
202  // << " SegsLeft " << (uint32_t)i->GetSegsLeft () << " " <<
203  // (uint32_t)entry.GetSegsLeft () << " ackId " <<
204  // (uint32_t)i->GetAckId () << " "
205  // << (uint32_t)entry.GetAckId ()
206  // );
207 
208  if ((i->GetSrc() == entry.GetSrc()) && (i->GetDst() == entry.GetDst()) &&
209  (i->GetSegsLeft() == entry.GetSegsLeft()) && (i->GetAckId() == entry.GetAckId()))
210  {
211  i = m_maintainBuffer.erase(
212  i); // Erase the same maintain buffer entry for the promisc received packet
213  return true;
214  }
215  }
216  return false;
217 }
218 
219 bool
221 {
222  NS_LOG_DEBUG("The maintenance buffer size " << m_maintainBuffer.size());
223  for (std::vector<DsrMaintainBuffEntry>::iterator i = m_maintainBuffer.begin();
224  i != m_maintainBuffer.end();
225  ++i)
226  {
227  // NS_LOG_DEBUG ("src " << i->GetSrc () << " " << entry.GetSrc () << " dst " <<
228  // i->GetDst () << " " << entry.GetDst ()
229  // << " OurAddress " << i->GetOurAdd () << " " << entry.GetOurAdd
230  // () << " next hop " << i->GetNextHop () << " "
231  // << entry.GetNextHop ()
232  // );
233 
234  if ((i->GetSrc() == entry.GetSrc()) && (i->GetDst() == entry.GetDst()) &&
235  (i->GetOurAdd() == entry.GetOurAdd()) && (i->GetNextHop() == entry.GetNextHop()))
236  {
237  i = m_maintainBuffer.erase(
238  i); // Erase the same maintain buffer entry for the promisc received packet
239  return true;
240  }
241  }
242  return false;
243 }
244 
246 struct IsExpired
247 {
253  bool operator()(const DsrMaintainBuffEntry& e) const
254  {
255  // NS_LOG_DEBUG("Expire time for packet in req queue: "<<e.GetExpireTime ());
256  return (e.GetExpireTime() < Seconds(0));
257  }
258 };
259 
260 void
262 {
263  NS_LOG_DEBUG("Purging Maintenance Buffer");
264  IsExpired pred;
265  m_maintainBuffer.erase(std::remove_if(m_maintainBuffer.begin(), m_maintainBuffer.end(), pred),
266  m_maintainBuffer.end());
267 }
268 
269 } // namespace dsr
270 } // namespace ns3
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:43
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:863
DSR Maintain Buffer Entry.
uint8_t GetSegsLeft() const
Get segments left.
void SetExpireTime(Time exp)
Set expiration time.
Ptr< const Packet > GetPacket() const
Get packet.
Ipv4Address GetSrc() const
Get source address.
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:1336
Every class exported by the ns3 library is enclosed in the ns3 namespace.
IsExpired structure.
bool operator()(const DsrMaintainBuffEntry &e) const
comparison operator