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 (auto i = m_errorBuffer.begin(); i != m_errorBuffer.end(); ++i)
60  {
61  NS_LOG_INFO("packet id " << i->GetPacket()->GetUid() << " " << entry.GetPacket()->GetUid()
62  << " source " << i->GetSource() << " " << entry.GetSource()
63  << " next hop " << i->GetNextHop() << " " << entry.GetNextHop()
64  << " dst " << i->GetDestination() << " "
65  << entry.GetDestination());
66 
68  if ((i->GetPacket()->GetUid() == entry.GetPacket()->GetUid()) &&
69  (i->GetSource() == entry.GetSource()) && (i->GetNextHop() == entry.GetSource()) &&
70  (i->GetDestination() == entry.GetDestination()))
71  {
72  return false;
73  }
74  }
75 
76  entry.SetExpireTime(m_errorBufferTimeout); // Initialize the send buffer timeout
77  /*
78  * Drop the most aged packet when buffer reaches to max
79  */
80  if (m_errorBuffer.size() >= m_maxLen)
81  {
82  Drop(m_errorBuffer.front(), "Drop the most aged packet"); // Drop the most aged packet
83  m_errorBuffer.erase(m_errorBuffer.begin());
84  }
85  // enqueue the entry
86  m_errorBuffer.push_back(entry);
87  return true;
88 }
89 
90 void
92 {
93  NS_LOG_FUNCTION(this << source << nextHop);
94  Purge();
95  std::vector<Ipv4Address> list;
96  list.push_back(source);
97  list.push_back(nextHop);
98  const std::vector<Ipv4Address> link = list;
99  /*
100  * Drop the packet with the error link source----------nextHop
101  */
102  for (auto i = m_errorBuffer.begin(); i != m_errorBuffer.end(); ++i)
103  {
104  if ((i->GetSource() == link[0]) && (i->GetNextHop() == link[1]))
105  {
106  DropLink(*i, "DropPacketForErrLink");
107  }
108  }
109 
110  auto new_end =
111  std::remove_if(m_errorBuffer.begin(),
112  m_errorBuffer.end(),
113  [&](const DsrErrorBuffEntry& en) {
114  return (en.GetSource() == link[0]) && (en.GetNextHop() == link[1]);
115  });
116  m_errorBuffer.erase(new_end, m_errorBuffer.end());
117 }
118 
119 bool
121 {
122  Purge();
123  /*
124  * Dequeue the entry with destination address dst
125  */
126  for (auto i = m_errorBuffer.begin(); i != m_errorBuffer.end(); ++i)
127  {
128  if (i->GetDestination() == dst)
129  {
130  entry = *i;
131  i = m_errorBuffer.erase(i);
132  NS_LOG_DEBUG("Packet size while dequeuing " << entry.GetPacket()->GetSize());
133  return true;
134  }
135  }
136  return false;
137 }
138 
139 bool
141 {
142  /*
143  * Make sure if the send buffer contains entry with certain dst
144  */
145  for (auto i = m_errorBuffer.begin(); i != m_errorBuffer.end(); ++i)
146  {
147  if (i->GetDestination() == dst)
148  {
149  NS_LOG_DEBUG("Found the packet");
150  return true;
151  }
152  }
153  return false;
154 }
155 
157 struct IsExpired
158 {
164  bool operator()(const DsrErrorBuffEntry& e) const
165  {
166  // NS_LOG_DEBUG("Expire time for packet in req queue: "<<e.GetExpireTime ());
167  return (e.GetExpireTime() < Seconds(0));
168  }
169 };
170 
171 void
173 {
174  /*
175  * Purge the buffer to eliminate expired entries
176  */
177  NS_LOG_DEBUG("The error buffer size " << m_errorBuffer.size());
178  IsExpired pred;
179  for (auto i = m_errorBuffer.begin(); i != m_errorBuffer.end(); ++i)
180  {
181  if (pred(*i))
182  {
183  NS_LOG_DEBUG("Dropping Queue Packets");
184  Drop(*i, "Drop out-dated packet ");
185  }
186  }
187  m_errorBuffer.erase(std::remove_if(m_errorBuffer.begin(), m_errorBuffer.end(), pred),
188  m_errorBuffer.end());
189 }
190 
191 void
193 {
194  NS_LOG_LOGIC(reason << en.GetPacket()->GetUid() << " " << en.GetDestination());
195  // en.GetErrorCallback () (en.GetPacket (), en.GetDestination (),
196  // Socket::ERROR_NOROUTETOHOST);
197 }
198 
199 void
201 {
202  NS_LOG_LOGIC(reason << en.GetPacket()->GetUid() << " " << en.GetSource() << " "
203  << en.GetNextHop());
204  // en.GetErrorCallback () (en.GetPacket (), en.GetDestination (),
205  // Socket::ERROR_NOROUTETOHOST);
206 }
207 } // namespace dsr
208 } // 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
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:1326
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