A Discrete-Event Network Simulator
API
dsr-errorbuff.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2011 Yufei Cheng
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Yufei Cheng <yfcheng@ittc.ku.edu>
19  *
20  * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director
21  * ResiliNets Research Group http://wiki.ittc.ku.edu/resilinets
22  * Information and Telecommunication Technology Center (ITTC)
23  * and Department of Electrical Engineering and Computer Science
24  * The University of Kansas Lawrence, KS USA.
25  *
26  * Work supported in part by NSF FIND (Future Internet Design) Program
27  * under grant CNS-0626918 (Postmodern Internet Architecture),
28  * NSF grant CNS-1050226 (Multilayer Network Resilience Analysis and Experimentation on GENI),
29  * US Department of Defense (DoD), and ITTC at The University of Kansas.
30  */
31 
32 #include "dsr-errorbuff.h"
33 #include <algorithm>
34 #include <functional>
35 #include "ns3/ipv4-route.h"
36 #include "ns3/socket.h"
37 #include "ns3/log.h"
38 
39 namespace ns3 {
40 
41 NS_LOG_COMPONENT_DEFINE ("DsrErrorBuffer");
42 
43 namespace dsr {
44 
45 uint32_t
47 {
48  Purge ();
49  return m_errorBuffer.size ();
50 }
51 
52 bool
54 {
55  Purge ();
56  for (std::vector<DsrErrorBuffEntry>::const_iterator i = m_errorBuffer.begin (); i
57  != m_errorBuffer.end (); ++i)
58  {
59  NS_LOG_INFO ("packet id " << i->GetPacket ()->GetUid () << " " << entry.GetPacket ()->GetUid () << " source " << i->GetSource () << " " << entry.GetSource ()
60  << " next hop " << i->GetNextHop () << " " << entry.GetNextHop () << " dst " << i->GetDestination () << " " << entry.GetDestination ());
61 
63  if ((i->GetPacket ()->GetUid () == entry.GetPacket ()->GetUid ()) && (i->GetSource () == entry.GetSource ()) && (i->GetNextHop () == entry.GetSource ())
64  && (i->GetDestination () == entry.GetDestination ()))
65  {
66  return false;
67  }
68  }
69 
70  entry.SetExpireTime (m_errorBufferTimeout); // Initialize the send buffer timeout
71  /*
72  * Drop the most aged packet when buffer reaches to max
73  */
74  if (m_errorBuffer.size () >= m_maxLen)
75  {
76  Drop (m_errorBuffer.front (), "Drop the most aged packet"); // Drop the most aged packet
77  m_errorBuffer.erase (m_errorBuffer.begin ());
78  }
79  // enqueue the entry
80  m_errorBuffer.push_back (entry);
81  return true;
82 }
83 
84 void
86 {
87  NS_LOG_FUNCTION (this << source << nextHop);
88  Purge ();
89  std::vector<Ipv4Address> list;
90  list.push_back (source);
91  list.push_back (nextHop);
92  const std::vector<Ipv4Address> link = list;
93  /*
94  * Drop the packet with the error link source----------nextHop
95  */
96  for (std::vector<DsrErrorBuffEntry>::iterator i = m_errorBuffer.begin (); i
97  != m_errorBuffer.end (); ++i)
98  {
99  if ((i->GetSource () == link[0]) && (i->GetNextHop () == link[1]))
100  {
101  DropLink (*i, "DropPacketForErrLink");
102  }
103  }
104 
105  auto new_end = std::remove_if (m_errorBuffer.begin (), m_errorBuffer.end (), [&](const DsrErrorBuffEntry& en)
106  { return (en.GetSource () == link[0]) && (en.GetNextHop () == link[1]); });
107  m_errorBuffer.erase (new_end, m_errorBuffer.end ());
108 }
109 
110 bool
112 {
113  Purge ();
114  /*
115  * Dequeue the entry with destination address dst
116  */
117  for (std::vector<DsrErrorBuffEntry>::iterator i = m_errorBuffer.begin (); i != m_errorBuffer.end (); ++i)
118  {
119  if (i->GetDestination () == dst)
120  {
121  entry = *i;
122  i = m_errorBuffer.erase (i);
123  NS_LOG_DEBUG ("Packet size while dequeuing " << entry.GetPacket ()->GetSize ());
124  return true;
125  }
126  }
127  return false;
128 }
129 
130 bool
132 {
133  /*
134  * Make sure if the send buffer contains entry with certain dst
135  */
136  for (std::vector<DsrErrorBuffEntry>::const_iterator i = m_errorBuffer.begin (); i
137  != m_errorBuffer.end (); ++i)
138  {
139  if (i->GetDestination () == dst)
140  {
141  NS_LOG_DEBUG ("Found the packet");
142  return true;
143  }
144  }
145  return false;
146 }
147 
149 struct IsExpired
150 {
156  bool
158  {
159  // NS_LOG_DEBUG("Expire time for packet in req queue: "<<e.GetExpireTime ());
160  return (e.GetExpireTime () < Seconds (0));
161  }
162 };
163 
164 void
166 {
167  /*
168  * Purge the buffer to eliminate expired entries
169  */
170  NS_LOG_DEBUG ("The error buffer size " << m_errorBuffer.size ());
171  IsExpired pred;
172  for (std::vector<DsrErrorBuffEntry>::iterator i = m_errorBuffer.begin (); i
173  != m_errorBuffer.end (); ++i)
174  {
175  if (pred (*i))
176  {
177  NS_LOG_DEBUG ("Dropping Queue Packets");
178  Drop (*i, "Drop out-dated packet ");
179  }
180  }
181  m_errorBuffer.erase (std::remove_if (m_errorBuffer.begin (), m_errorBuffer.end (), pred),
182  m_errorBuffer.end ());
183 }
184 
185 void
186 DsrErrorBuffer::Drop (DsrErrorBuffEntry en, std::string reason)
187 {
188  NS_LOG_LOGIC (reason << en.GetPacket ()->GetUid () << " " << en.GetDestination ());
189 // en.GetErrorCallback () (en.GetPacket (), en.GetDestination (),
190 // Socket::ERROR_NOROUTETOHOST);
191  return;
192 }
193 
194 void
196 {
197  NS_LOG_LOGIC (reason << en.GetPacket ()->GetUid () << " " << en.GetSource () << " " << en.GetNextHop ());
198 // en.GetErrorCallback () (en.GetPacket (), en.GetDestination (),
199 // Socket::ERROR_NOROUTETOHOST);
200  return;
201 }
202 } // namespace dsr
203 } // namespace ns3
uint32_t GetSize()
Returns the number of entries in the queue.
Ipv4Address GetNextHop() const
Get next hop.
void SetExpireTime(Time exp)
Set expire time.
uint64_t GetUid(void) const
Returns the packet&#39;s Uid.
Definition: packet.cc:390
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:852
bool Find(Ipv4Address dst)
Finds whether a packet with destination dst exists in the queue.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:204
uint32_t m_maxLen
The maximum number of packets that we allow a routing protocol to buffer.
DSR Error Buffer Entry.
Definition: dsr-errorbuff.h:45
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:280
Ipv4Address GetSource() const
Get source address.
bool Dequeue(Ipv4Address dst, DsrErrorBuffEntry &entry)
Return first found (the earliest) entry for given destination.
Time GetExpireTime() const
Get expire time.
void DropLink(DsrErrorBuffEntry en, std::string reason)
Notify that packet is dropped from queue by link error.
void DropPacketForErrLink(Ipv4Address source, Ipv4Address nextHop)
Remove all packets with the error link.
bool Enqueue(DsrErrorBuffEntry &entry)
Push entry in queue, if there is no entry with the same packet and destination address in queue...
#define list
void Purge()
Remove all expired entries.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
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.
NS_LOG_LOGIC("Net device "<< nd<< " is not bridged")
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
Ipv4Address GetDestination() const
Get destination address.
Definition: dsr-errorbuff.h:99
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:272
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1062
bool operator()(DsrErrorBuffEntry const &e) const
comparison operator
Ptr< const Packet > GetPacket() const
Get packet from entry.
Definition: dsr-errorbuff.h:83
IsExpired structure.
Time m_errorBufferTimeout
The maximum period of time that a routing protocol is allowed to buffer a packet for, seconds.