A Discrete-Event Network Simulator
API
dsr-errorbuff.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-errorbuff.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("DsrErrorBuffer");
44 
45 namespace dsr
46 {
47 
48 uint32_t
50 {
51  Purge();
52  return m_errorBuffer.size();
53 }
54 
55 bool
57 {
58  Purge();
59  for (std::vector<DsrErrorBuffEntry>::const_iterator i = m_errorBuffer.begin();
60  i != m_errorBuffer.end();
61  ++i)
62  {
63  NS_LOG_INFO("packet id " << i->GetPacket()->GetUid() << " " << entry.GetPacket()->GetUid()
64  << " source " << i->GetSource() << " " << entry.GetSource()
65  << " next hop " << i->GetNextHop() << " " << entry.GetNextHop()
66  << " dst " << i->GetDestination() << " "
67  << entry.GetDestination());
68 
70  if ((i->GetPacket()->GetUid() == entry.GetPacket()->GetUid()) &&
71  (i->GetSource() == entry.GetSource()) && (i->GetNextHop() == entry.GetSource()) &&
72  (i->GetDestination() == entry.GetDestination()))
73  {
74  return false;
75  }
76  }
77 
78  entry.SetExpireTime(m_errorBufferTimeout); // Initialize the send buffer timeout
79  /*
80  * Drop the most aged packet when buffer reaches to max
81  */
82  if (m_errorBuffer.size() >= m_maxLen)
83  {
84  Drop(m_errorBuffer.front(), "Drop the most aged packet"); // Drop the most aged packet
85  m_errorBuffer.erase(m_errorBuffer.begin());
86  }
87  // enqueue the entry
88  m_errorBuffer.push_back(entry);
89  return true;
90 }
91 
92 void
94 {
95  NS_LOG_FUNCTION(this << source << nextHop);
96  Purge();
97  std::vector<Ipv4Address> list;
98  list.push_back(source);
99  list.push_back(nextHop);
100  const std::vector<Ipv4Address> link = list;
101  /*
102  * Drop the packet with the error link source----------nextHop
103  */
104  for (std::vector<DsrErrorBuffEntry>::iterator i = m_errorBuffer.begin();
105  i != m_errorBuffer.end();
106  ++i)
107  {
108  if ((i->GetSource() == link[0]) && (i->GetNextHop() == link[1]))
109  {
110  DropLink(*i, "DropPacketForErrLink");
111  }
112  }
113 
114  auto new_end =
115  std::remove_if(m_errorBuffer.begin(),
116  m_errorBuffer.end(),
117  [&](const DsrErrorBuffEntry& en) {
118  return (en.GetSource() == link[0]) && (en.GetNextHop() == link[1]);
119  });
120  m_errorBuffer.erase(new_end, m_errorBuffer.end());
121 }
122 
123 bool
125 {
126  Purge();
127  /*
128  * Dequeue the entry with destination address dst
129  */
130  for (std::vector<DsrErrorBuffEntry>::iterator i = m_errorBuffer.begin();
131  i != m_errorBuffer.end();
132  ++i)
133  {
134  if (i->GetDestination() == dst)
135  {
136  entry = *i;
137  i = m_errorBuffer.erase(i);
138  NS_LOG_DEBUG("Packet size while dequeuing " << entry.GetPacket()->GetSize());
139  return true;
140  }
141  }
142  return false;
143 }
144 
145 bool
147 {
148  /*
149  * Make sure if the send buffer contains entry with certain dst
150  */
151  for (std::vector<DsrErrorBuffEntry>::const_iterator i = m_errorBuffer.begin();
152  i != m_errorBuffer.end();
153  ++i)
154  {
155  if (i->GetDestination() == dst)
156  {
157  NS_LOG_DEBUG("Found the packet");
158  return true;
159  }
160  }
161  return false;
162 }
163 
165 struct IsExpired
166 {
172  bool operator()(const DsrErrorBuffEntry& e) const
173  {
174  // NS_LOG_DEBUG("Expire time for packet in req queue: "<<e.GetExpireTime ());
175  return (e.GetExpireTime() < Seconds(0));
176  }
177 };
178 
179 void
181 {
182  /*
183  * Purge the buffer to eliminate expired entries
184  */
185  NS_LOG_DEBUG("The error buffer size " << m_errorBuffer.size());
186  IsExpired pred;
187  for (std::vector<DsrErrorBuffEntry>::iterator i = m_errorBuffer.begin();
188  i != m_errorBuffer.end();
189  ++i)
190  {
191  if (pred(*i))
192  {
193  NS_LOG_DEBUG("Dropping Queue Packets");
194  Drop(*i, "Drop out-dated packet ");
195  }
196  }
197  m_errorBuffer.erase(std::remove_if(m_errorBuffer.begin(), m_errorBuffer.end(), pred),
198  m_errorBuffer.end());
199 }
200 
201 void
203 {
204  NS_LOG_LOGIC(reason << en.GetPacket()->GetUid() << " " << en.GetDestination());
205  // en.GetErrorCallback () (en.GetPacket (), en.GetDestination (),
206  // Socket::ERROR_NOROUTETOHOST);
207 }
208 
209 void
211 {
212  NS_LOG_LOGIC(reason << en.GetPacket()->GetUid() << " " << en.GetSource() << " "
213  << en.GetNextHop());
214  // en.GetErrorCallback () (en.GetPacket (), en.GetDestination (),
215  // Socket::ERROR_NOROUTETOHOST);
216 }
217 } // namespace dsr
218 } // 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
uint64_t GetUid() const
Returns the packet's Uid.
Definition: packet.cc:412
DSR Error Buffer Entry.
Definition: dsr-errorbuff.h:48
void SetExpireTime(Time exp)
Set expire time.
Ipv4Address GetNextHop() const
Get next hop.
Ptr< const Packet > GetPacket() const
Get packet from entry.
Definition: dsr-errorbuff.h:91
Ipv4Address GetSource() const
Get source address.
Time GetExpireTime() const
Get expire time.
Ipv4Address GetDestination() const
Get destination address.
Time m_errorBufferTimeout
The maximum period of time that a routing protocol is allowed to buffer a packet for,...
uint32_t m_maxLen
The maximum number of packets that we allow a routing protocol to buffer.
bool Enqueue(DsrErrorBuffEntry &entry)
Push entry in queue, if there is no entry with the same packet and destination address in queue.
void DropLink(DsrErrorBuffEntry en, std::string reason)
Notify that packet is dropped from queue by link error.
bool Dequeue(Ipv4Address dst, DsrErrorBuffEntry &entry)
Return first found (the earliest) entry for given destination.
bool Find(Ipv4Address dst)
Finds whether a packet with destination dst exists in the queue.
void DropPacketForErrLink(Ipv4Address source, Ipv4Address nextHop)
Remove all packets with the error link.
uint32_t GetSize()
Returns the number of entries in the queue.
std::vector< DsrErrorBuffEntry > m_errorBuffer
The send buffer to cache unsent packet.
void Drop(DsrErrorBuffEntry en, std::string reason)
Notify that packet is dropped from queue by timeout.
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_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:282
#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.
#define list
IsExpired structure.
bool operator()(const DsrErrorBuffEntry &e) const
comparison operator