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 (LinkEqual (*i, link))
100  {
101  DropLink (*i, "DropPacketForErrLink");
102  }
103  }
104  m_errorBuffer.erase (std::remove_if (m_errorBuffer.begin (), m_errorBuffer.end (),
105  std::bind2nd (std::ptr_fun (DsrErrorBuffer::LinkEqual), link)), m_errorBuffer.end ());
106 }
107 
108 bool
110 {
111  Purge ();
112  /*
113  * Dequeue the entry with destination address dst
114  */
115  for (std::vector<DsrErrorBuffEntry>::iterator i = m_errorBuffer.begin (); i != m_errorBuffer.end (); ++i)
116  {
117  if (i->GetDestination () == dst)
118  {
119  entry = *i;
120  i = m_errorBuffer.erase (i);
121  NS_LOG_DEBUG ("Packet size while dequeuing " << entry.GetPacket ()->GetSize ());
122  return true;
123  }
124  }
125  return false;
126 }
127 
128 bool
130 {
131  /*
132  * Make sure if the send buffer contains entry with certain dst
133  */
134  for (std::vector<DsrErrorBuffEntry>::const_iterator i = m_errorBuffer.begin (); i
135  != m_errorBuffer.end (); ++i)
136  {
137  if (i->GetDestination () == dst)
138  {
139  NS_LOG_DEBUG ("Found the packet");
140  return true;
141  }
142  }
143  return false;
144 }
145 
147 struct IsExpired
148 {
154  bool
156  {
157  // NS_LOG_DEBUG("Expire time for packet in req queue: "<<e.GetExpireTime ());
158  return (e.GetExpireTime () < Seconds (0));
159  }
160 };
161 
162 void
164 {
165  /*
166  * Purge the buffer to eliminate expired entries
167  */
168  NS_LOG_DEBUG ("The error buffer size " << m_errorBuffer.size ());
169  IsExpired pred;
170  for (std::vector<DsrErrorBuffEntry>::iterator i = m_errorBuffer.begin (); i
171  != m_errorBuffer.end (); ++i)
172  {
173  if (pred (*i))
174  {
175  NS_LOG_DEBUG ("Dropping Queue Packets");
176  Drop (*i, "Drop out-dated packet ");
177  }
178  }
179  m_errorBuffer.erase (std::remove_if (m_errorBuffer.begin (), m_errorBuffer.end (), pred),
180  m_errorBuffer.end ());
181 }
182 
183 void
184 DsrErrorBuffer::Drop (DsrErrorBuffEntry en, std::string reason)
185 {
186  NS_LOG_LOGIC (reason << en.GetPacket ()->GetUid () << " " << en.GetDestination ());
187 // en.GetErrorCallback () (en.GetPacket (), en.GetDestination (),
188 // Socket::ERROR_NOROUTETOHOST);
189  return;
190 }
191 
192 void
194 {
195  NS_LOG_LOGIC (reason << en.GetPacket ()->GetUid () << " " << en.GetSource () << " " << en.GetNextHop ());
196 // en.GetErrorCallback () (en.GetPacket (), en.GetDestination (),
197 // Socket::ERROR_NOROUTETOHOST);
198  return;
199 }
200 } // namespace dsr
201 } // namespace ns3
uint32_t GetSize()
Returns the number of entries in the queue.
void SetExpireTime(Time exp)
Set expire time.
Time GetExpireTime() const
Get expire time.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
Ipv4Address GetDestination() const
Get destination address.
Definition: dsr-errorbuff.h:99
bool Find(Ipv4Address dst)
Finds whether a packet with destination dst exists in the queue.
uint64_t GetUid(void) const
Returns the packet's Uid.
Definition: packet.cc:367
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
uint32_t m_maxLen
The maximum number of packets that we allow a routing protocol to buffer.
bool operator()(DsrErrorBuffEntry const &e) const
comparison operator
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:796
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:277
bool Dequeue(Ipv4Address dst, DsrErrorBuffEntry &entry)
Return first found (the earliest) entry for given destination.
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")
Ipv4Address GetSource() const
Get source address.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
Ptr< const Packet > GetPacket() const
Get packet from entry.
Definition: dsr-errorbuff.h:83
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:269
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:993
static bool LinkEqual(DsrErrorBuffEntry en, const std::vector< Ipv4Address > link)
Check if the send buffer entry is the same or not.
Ipv4Address GetNextHop() const
Get next hop.
IsExpired structure.
Time m_errorBufferTimeout
The maximum period of time that a routing protocol is allowed to buffer a packet for, seconds.