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 (std::vector<DsrPassiveBuffEntry>::const_iterator i = m_passiveBuffer.begin();
80  i != m_passiveBuffer.end();
81  ++i)
82  {
83  // NS_LOG_INFO ("packet id " << i->GetPacket ()->GetUid () << " " << entry.GetPacket
84  // ()->GetUid () << " source " << i->GetSource () << " " << entry.GetSource ()
85  // << " dst " << i->GetDestination () << " " <<
86  // entry.GetDestination () << " identification " <<
87  // i->GetIdentification () << " "
88  // << entry.GetIdentification () << " fragment " <<
89  // i->GetFragmentOffset () << " " <<
90  // entry.GetFragmentOffset ()
91  // << " segLeft " << i->GetSegsLeft () << " " <<
92  // entry.GetSegsLeft ());
93 
94  if ((i->GetPacket()->GetUid() == entry.GetPacket()->GetUid()) &&
95  (i->GetSource() == entry.GetSource()) && (i->GetNextHop() == entry.GetNextHop()) &&
96  (i->GetDestination() == entry.GetDestination()) &&
97  (i->GetIdentification() == entry.GetIdentification()) &&
98  (i->GetFragmentOffset() == entry.GetFragmentOffset()) &&
99  (i->GetSegsLeft() == entry.GetSegsLeft() + 1))
100  {
101  return false;
102  }
103  }
104 
105  entry.SetExpireTime(m_passiveBufferTimeout); // Initialize the send buffer timeout
106  /*
107  * Drop the most aged packet when buffer reaches to max
108  */
109  if (m_passiveBuffer.size() >= m_maxLen)
110  {
111  Drop(m_passiveBuffer.front(), "Drop the most aged packet"); // Drop the most aged packet
112  m_passiveBuffer.erase(m_passiveBuffer.begin());
113  }
114  // enqueue the entry
115  m_passiveBuffer.push_back(entry);
116  return true;
117 }
118 
119 bool
121 {
122  for (std::vector<DsrPassiveBuffEntry>::iterator i = m_passiveBuffer.begin();
123  i != m_passiveBuffer.end();
124  ++i)
125  {
126  // NS_LOG_INFO ("packet id " << i->GetPacket ()->GetUid () << " " << entry.GetPacket
127  // ()->GetUid () << " source " << i->GetSource () << " " << entry.GetSource ()
128  // << " dst " << i->GetDestination () << " " <<
129  // entry.GetDestination () << " identification " <<
130  // i->GetIdentification () << " "
131  // << entry.GetIdentification () << " fragment " <<
132  // i->GetFragmentOffset () << " " <<
133  // entry.GetFragmentOffset ()
134  // << " segLeft " << (uint32_t) i->GetSegsLeft () << " "
135  // << (uint32_t) entry.GetSegsLeft ());
136 
137  if ((i->GetPacket()->GetUid() == entry.GetPacket()->GetUid()) &&
138  (i->GetSource() == entry.GetSource()) && (i->GetNextHop() == entry.GetNextHop()) &&
139  (i->GetDestination() == entry.GetDestination()) &&
140  (i->GetIdentification() == entry.GetIdentification()) &&
141  (i->GetFragmentOffset() == entry.GetFragmentOffset()) &&
142  (i->GetSegsLeft() == entry.GetSegsLeft() + 1))
143  {
144  i = m_passiveBuffer.erase(
145  i); // Erase the same maintain buffer entry for the received packet
146  return true;
147  }
148  }
149  return false;
150 }
151 
152 bool
154 {
155  Purge();
156  /*
157  * Dequeue the entry with destination address dst
158  */
159  for (std::vector<DsrPassiveBuffEntry>::iterator i = m_passiveBuffer.begin();
160  i != m_passiveBuffer.end();
161  ++i)
162  {
163  if (i->GetDestination() == dst)
164  {
165  entry = *i;
166  i = m_passiveBuffer.erase(i);
167  NS_LOG_DEBUG("Packet size while dequeuing " << entry.GetPacket()->GetSize());
168  return true;
169  }
170  }
171  return false;
172 }
173 
174 bool
176 {
177  /*
178  * Make sure if the send buffer contains entry with certain dst
179  */
180  for (std::vector<DsrPassiveBuffEntry>::const_iterator i = m_passiveBuffer.begin();
181  i != m_passiveBuffer.end();
182  ++i)
183  {
184  if (i->GetDestination() == dst)
185  {
186  NS_LOG_DEBUG("Found the packet");
187  return true;
188  }
189  }
190  return false;
191 }
192 
194 struct IsExpired
195 {
201  bool operator()(const DsrPassiveBuffEntry& e) const
202  {
203  // NS_LOG_DEBUG("Expire time for packet in req queue: "<<e.GetExpireTime ());
204  return (e.GetExpireTime() < Seconds(0));
205  }
206 };
207 
208 void
210 {
211  /*
212  * Purge the buffer to eliminate expired entries
213  */
214  NS_LOG_DEBUG("The passive buffer size " << m_passiveBuffer.size());
215  IsExpired pred;
216  for (std::vector<DsrPassiveBuffEntry>::iterator i = m_passiveBuffer.begin();
217  i != m_passiveBuffer.end();
218  ++i)
219  {
220  if (pred(*i))
221  {
222  NS_LOG_DEBUG("Dropping Queue Packets");
223  Drop(*i, "Drop out-dated packet ");
224  }
225  }
226  m_passiveBuffer.erase(std::remove_if(m_passiveBuffer.begin(), m_passiveBuffer.end(), pred),
227  m_passiveBuffer.end());
228 }
229 
230 void
232 {
233  NS_LOG_LOGIC(reason << en.GetPacket()->GetUid() << " " << en.GetDestination());
234  // en.GetErrorCallback () (en.GetPacket (), en.GetDestination (),
235  // Socket::ERROR_NOROUTETOHOST);
236 }
237 
238 void
240 {
241  NS_LOG_LOGIC(reason << en.GetPacket()->GetUid() << " " << en.GetSource() << " "
242  << en.GetNextHop());
243  // en.GetErrorCallback () (en.GetPacket (), en.GetDestination (),
244  // Socket::ERROR_NOROUTETOHOST);
245 }
246 } // namespace dsr
247 } // namespace ns3
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:43
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:863
uint64_t GetUid() const
Returns the packet's Uid.
Definition: packet.cc:412
a unique identifier for an interface.
Definition: type-id.h:60
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:935
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:1336
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.