A Discrete-Event Network Simulator
API
dsr-passive-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-passive-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("DsrPassiveBuffer");
44 
45 namespace dsr
46 {
47 
48 NS_OBJECT_ENSURE_REGISTERED(DsrPassiveBuffer);
49 
50 TypeId
52 {
53  static TypeId tid = TypeId("ns3::dsr::DsrPassiveBuffer")
54  .SetParent<Object>()
55  .SetGroupName("Dsr")
56  .AddConstructor<DsrPassiveBuffer>();
57  return tid;
58 }
59 
61 {
62 }
63 
65 {
66 }
67 
68 uint32_t
70 {
71  Purge();
72  return m_passiveBuffer.size();
73 }
74 
75 bool
77 {
78  Purge();
79  for (auto i = m_passiveBuffer.begin(); i != m_passiveBuffer.end(); ++i)
80  {
81  // NS_LOG_INFO ("packet id " << i->GetPacket ()->GetUid () << " " << entry.GetPacket
82  // ()->GetUid () << " source " << i->GetSource () << " " << entry.GetSource ()
83  // << " dst " << i->GetDestination () << " " <<
84  // entry.GetDestination () << " identification " <<
85  // i->GetIdentification () << " "
86  // << entry.GetIdentification () << " fragment " <<
87  // i->GetFragmentOffset () << " " <<
88  // entry.GetFragmentOffset ()
89  // << " segLeft " << i->GetSegsLeft () << " " <<
90  // entry.GetSegsLeft ());
91 
92  if ((i->GetPacket()->GetUid() == entry.GetPacket()->GetUid()) &&
93  (i->GetSource() == entry.GetSource()) && (i->GetNextHop() == entry.GetNextHop()) &&
94  (i->GetDestination() == entry.GetDestination()) &&
95  (i->GetIdentification() == entry.GetIdentification()) &&
96  (i->GetFragmentOffset() == entry.GetFragmentOffset()) &&
97  (i->GetSegsLeft() == entry.GetSegsLeft() + 1))
98  {
99  return false;
100  }
101  }
102 
103  entry.SetExpireTime(m_passiveBufferTimeout); // Initialize the send buffer timeout
104  /*
105  * Drop the most aged packet when buffer reaches to max
106  */
107  if (m_passiveBuffer.size() >= m_maxLen)
108  {
109  Drop(m_passiveBuffer.front(), "Drop the most aged packet"); // Drop the most aged packet
110  m_passiveBuffer.erase(m_passiveBuffer.begin());
111  }
112  // enqueue the entry
113  m_passiveBuffer.push_back(entry);
114  return true;
115 }
116 
117 bool
119 {
120  for (auto i = m_passiveBuffer.begin(); i != m_passiveBuffer.end(); ++i)
121  {
122  // NS_LOG_INFO ("packet id " << i->GetPacket ()->GetUid () << " " << entry.GetPacket
123  // ()->GetUid () << " source " << i->GetSource () << " " << entry.GetSource ()
124  // << " dst " << i->GetDestination () << " " <<
125  // entry.GetDestination () << " identification " <<
126  // i->GetIdentification () << " "
127  // << entry.GetIdentification () << " fragment " <<
128  // i->GetFragmentOffset () << " " <<
129  // entry.GetFragmentOffset ()
130  // << " segLeft " << (uint32_t) i->GetSegsLeft () << " "
131  // << (uint32_t) entry.GetSegsLeft ());
132 
133  if ((i->GetPacket()->GetUid() == entry.GetPacket()->GetUid()) &&
134  (i->GetSource() == entry.GetSource()) && (i->GetNextHop() == entry.GetNextHop()) &&
135  (i->GetDestination() == entry.GetDestination()) &&
136  (i->GetIdentification() == entry.GetIdentification()) &&
137  (i->GetFragmentOffset() == entry.GetFragmentOffset()) &&
138  (i->GetSegsLeft() == entry.GetSegsLeft() + 1))
139  {
140  i = m_passiveBuffer.erase(
141  i); // Erase the same maintain buffer entry for the received packet
142  return true;
143  }
144  }
145  return false;
146 }
147 
148 bool
150 {
151  Purge();
152  /*
153  * Dequeue the entry with destination address dst
154  */
155  for (auto i = m_passiveBuffer.begin(); i != m_passiveBuffer.end(); ++i)
156  {
157  if (i->GetDestination() == dst)
158  {
159  entry = *i;
160  i = m_passiveBuffer.erase(i);
161  NS_LOG_DEBUG("Packet size while dequeuing " << entry.GetPacket()->GetSize());
162  return true;
163  }
164  }
165  return false;
166 }
167 
168 bool
170 {
171  /*
172  * Make sure if the send buffer contains entry with certain dst
173  */
174  for (auto i = m_passiveBuffer.begin(); i != m_passiveBuffer.end(); ++i)
175  {
176  if (i->GetDestination() == dst)
177  {
178  NS_LOG_DEBUG("Found the packet");
179  return true;
180  }
181  }
182  return false;
183 }
184 
186 struct IsExpired
187 {
193  bool operator()(const DsrPassiveBuffEntry& e) const
194  {
195  // NS_LOG_DEBUG("Expire time for packet in req queue: "<<e.GetExpireTime ());
196  return (e.GetExpireTime() < Seconds(0));
197  }
198 };
199 
200 void
202 {
203  /*
204  * Purge the buffer to eliminate expired entries
205  */
206  NS_LOG_DEBUG("The passive buffer size " << m_passiveBuffer.size());
207  IsExpired pred;
208  for (auto i = m_passiveBuffer.begin(); i != m_passiveBuffer.end(); ++i)
209  {
210  if (pred(*i))
211  {
212  NS_LOG_DEBUG("Dropping Queue Packets");
213  Drop(*i, "Drop out-dated packet ");
214  }
215  }
216  m_passiveBuffer.erase(std::remove_if(m_passiveBuffer.begin(), m_passiveBuffer.end(), pred),
217  m_passiveBuffer.end());
218 }
219 
220 void
222 {
223  NS_LOG_LOGIC(reason << en.GetPacket()->GetUid() << " " << en.GetDestination());
224  // en.GetErrorCallback () (en.GetPacket (), en.GetDestination (),
225  // Socket::ERROR_NOROUTETOHOST);
226 }
227 
228 void
230 {
231  NS_LOG_LOGIC(reason << en.GetPacket()->GetUid() << " " << en.GetSource() << " "
232  << en.GetNextHop());
233  // en.GetErrorCallback () (en.GetPacket (), en.GetDestination (),
234  // Socket::ERROR_NOROUTETOHOST);
235 }
236 } // namespace dsr
237 } // namespace ns3
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
A base class which provides memory management and object aggregation.
Definition: object.h:89
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
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
DSR Passive Buffer Entry.
void SetExpireTime(Time exp)
Set expire time.
Ipv4Address GetDestination() const
Get destination address function.
Ipv4Address GetNextHop() const
Get next hop address function.
Ipv4Address GetSource() const
Get source address function.
Time GetExpireTime() const
Get expire time.
uint8_t GetSegsLeft() const
Get segments left function.
uint16_t GetIdentification() const
Get identification function.
uint16_t GetFragmentOffset() const
Get fragment offset function.
Ptr< const Packet > GetPacket() const
Get packet function.
bool Enqueue(DsrPassiveBuffEntry &entry)
Push entry in queue, if there is no entry with the same packet and destination address in queue.
uint32_t GetSize()
Number of entries.
bool Find(Ipv4Address dst)
Finds whether a packet with destination dst exists in the queue.
bool Dequeue(Ipv4Address dst, DsrPassiveBuffEntry &entry)
Return first found (the earliest) entry for given destination.
void DropLink(DsrPassiveBuffEntry en, std::string reason)
Notify that packet is dropped from queue by timeout.
uint32_t m_maxLen
The maximum number of packets that we allow a routing protocol to buffer.
Time m_passiveBufferTimeout
The maximum period of time that a routing protocol is allowed to buffer a packet for,...
void Purge()
Remove all expired entries.
std::vector< DsrPassiveBuffEntry > m_passiveBuffer
The send buffer to cache unsent packet.
void Drop(DsrPassiveBuffEntry en, std::string reason)
Notify that packet is dropped from queue by timeout.
static TypeId GetTypeId()
Get the type ID.
bool AllEqual(DsrPassiveBuffEntry &entry)
Check if all the entries in passive buffer entry is all equal or not.
#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_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
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.
IsExpired structure.
bool operator()(const DsrPassiveBuffEntry &e) const
Check for an expired entry.