A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
dsr-passive-buff.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-passive-buff.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 NS_LOG_COMPONENT_DEFINE ("PassiveBuffer");
40 
41 namespace ns3 {
42 namespace dsr {
43 
44 NS_OBJECT_ENSURE_REGISTERED (PassiveBuffer)
45  ;
46 
48 {
49  static TypeId tid = TypeId ("ns3::dsr::PassiveBuffer")
50  .SetParent<Object> ()
51  .AddConstructor<PassiveBuffer> ()
52  ;
53  return tid;
54 }
55 
57 {
58 }
59 
61 {
62 }
63 
64 uint32_t
66 {
67  Purge ();
68  return m_passiveBuffer.size ();
69 }
70 
71 bool
73 {
74  Purge ();
75  for (std::vector<PassiveBuffEntry>::const_iterator i = m_passiveBuffer.begin (); i
76  != m_passiveBuffer.end (); ++i)
77  {
78 // NS_LOG_INFO ("packet id " << i->GetPacket ()->GetUid () << " " << entry.GetPacket ()->GetUid () << " source " << i->GetSource () << " " << entry.GetSource ()
79 // << " dst " << i->GetDestination () << " " << entry.GetDestination () << " identification " << i->GetIdentification () << " "
80 // << entry.GetIdentification () << " fragment " << i->GetFragmentOffset () << " " << entry.GetFragmentOffset ()
81 // << " segLeft " << i->GetSegsLeft () << " " << entry.GetSegsLeft ());
82 
83  if ((i->GetPacket ()->GetUid () == entry.GetPacket ()->GetUid ()) && (i->GetSource () == entry.GetSource ()) && (i->GetNextHop () == entry.GetNextHop ())
84  && (i->GetDestination () == entry.GetDestination ()) && (i->GetIdentification () == entry.GetIdentification ()) && (i->GetFragmentOffset () == entry.GetFragmentOffset ())
85  && (i->GetSegsLeft () == entry.GetSegsLeft () + 1))
86  {
87  return false;
88  }
89  }
90 
91  entry.SetExpireTime (m_passiveBufferTimeout); // Initialize the send buffer timeout
92  /*
93  * Drop the most aged packet when buffer reaches to max
94  */
95  if (m_passiveBuffer.size () >= m_maxLen)
96  {
97  Drop (m_passiveBuffer.front (), "Drop the most aged packet"); // Drop the most aged packet
98  m_passiveBuffer.erase (m_passiveBuffer.begin ());
99  }
100  // enqueue the entry
101  m_passiveBuffer.push_back (entry);
102  return true;
103 }
104 
105 bool
107 {
108  for (std::vector<PassiveBuffEntry>::iterator i = m_passiveBuffer.begin (); i
109  != m_passiveBuffer.end (); ++i)
110  {
111 // NS_LOG_INFO ("packet id " << i->GetPacket ()->GetUid () << " " << entry.GetPacket ()->GetUid () << " source " << i->GetSource () << " " << entry.GetSource ()
112 // << " dst " << i->GetDestination () << " " << entry.GetDestination () << " identification " << i->GetIdentification () << " "
113 // << entry.GetIdentification () << " fragment " << i->GetFragmentOffset () << " " << entry.GetFragmentOffset ()
114 // << " segLeft " << (uint32_t) i->GetSegsLeft () << " " << (uint32_t) entry.GetSegsLeft ());
115 
116  if ((i->GetPacket ()->GetUid () == entry.GetPacket ()->GetUid ()) && (i->GetSource () == entry.GetSource ()) && (i->GetNextHop () == entry.GetNextHop ())
117  && (i->GetDestination () == entry.GetDestination ()) && (i->GetIdentification () == entry.GetIdentification ()) && (i->GetFragmentOffset () == entry.GetFragmentOffset ())
118  && (i->GetSegsLeft () == entry.GetSegsLeft () + 1))
119  {
120  m_passiveBuffer.erase (i); // Erase the same maintain buffer entry for the received packet
121  return true;
122  }
123  }
124  return false;
125 }
126 
127 bool
129 {
130  Purge ();
131  /*
132  * Dequeue the entry with destination address dst
133  */
134  for (std::vector<PassiveBuffEntry>::iterator i = m_passiveBuffer.begin (); i != m_passiveBuffer.end (); ++i)
135  {
136  if (i->GetDestination () == dst)
137  {
138  entry = *i;
139  m_passiveBuffer.erase (i);
140  NS_LOG_DEBUG ("Packet size while dequeuing " << entry.GetPacket ()->GetSize ());
141  return true;
142  }
143  }
144  return false;
145 }
146 
147 bool
149 {
150  /*
151  * Make sure if the send buffer contains entry with certain dst
152  */
153  for (std::vector<PassiveBuffEntry>::const_iterator i = m_passiveBuffer.begin (); i
154  != m_passiveBuffer.end (); ++i)
155  {
156  if (i->GetDestination () == dst)
157  {
158  NS_LOG_DEBUG ("Found the packet");
159  return true;
160  }
161  }
162  return false;
163 }
164 
165 struct IsExpired
166 {
167  bool
168  operator() (PassiveBuffEntry const & e) const
169  {
170  // NS_LOG_DEBUG("Expire time for packet in req queue: "<<e.GetExpireTime ());
171  return (e.GetExpireTime () < Seconds (0));
172  }
173 };
174 
175 void
177 {
178  /*
179  * Purge the buffer to eliminate expired entries
180  */
181  NS_LOG_DEBUG ("The passive buffer size " << m_passiveBuffer.size ());
182  IsExpired pred;
183  for (std::vector<PassiveBuffEntry>::iterator i = m_passiveBuffer.begin (); i
184  != m_passiveBuffer.end (); ++i)
185  {
186  if (pred (*i))
187  {
188  NS_LOG_DEBUG ("Dropping Queue Packets");
189  Drop (*i, "Drop out-dated packet ");
190  }
191  }
192  m_passiveBuffer.erase (std::remove_if (m_passiveBuffer.begin (), m_passiveBuffer.end (), pred),
193  m_passiveBuffer.end ());
194 }
195 
196 void
197 PassiveBuffer::Drop (PassiveBuffEntry en, std::string reason)
198 {
199  NS_LOG_LOGIC (reason << en.GetPacket ()->GetUid () << " " << en.GetDestination ());
200 // en.GetErrorCallback () (en.GetPacket (), en.GetDestination (),
201 // Socket::ERROR_NOROUTETOHOST);
202  return;
203 }
204 
205 void
207 {
208  NS_LOG_LOGIC (reason << en.GetPacket ()->GetUid () << " " << en.GetSource () << " " << en.GetNextHop ());
209 // en.GetErrorCallback () (en.GetPacket (), en.GetDestination (),
210 // Socket::ERROR_NOROUTETOHOST);
211  return;
212 }
213 } // namespace dsr
214 } // namespace ns3
bool operator()(ErrorBuffEntry const &e) const
void SetExpireTime(Time exp)
DSR Passive Buffer Entry.
uint64_t GetUid(void) const
A packet is allocated a new uid when it is created empty or with zero-filled payload.
Definition: packet.cc:393
void Purge()
Remove all expired entries.
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
uint32_t GetSize(void) const
Definition: packet.h:650
uint16_t GetFragmentOffset() const
bool Enqueue(PassiveBuffEntry &entry)
Push entry in queue, if there is no entry with the same packet and destination address in queue...
bool Dequeue(Ipv4Address dst, PassiveBuffEntry &entry)
Return first found (the earliest) entry for given destination.
uint32_t GetSize()
Number of entries.
Ipv4Address GetSource() const
bool AllEqual(PassiveBuffEntry &entry)
Check if all the entries in passive buffer entry is all equal or not.
Time m_passiveBufferTimeout
The maximum period of time that a routing protocol is allowed to buffer a packet for, seconds.
uint8_t GetSegsLeft() const
#define NS_LOG_LOGIC(msg)
Definition: log.h:368
uint16_t GetIdentification() const
void DropLink(PassiveBuffEntry en, std::string reason)
Notify that packet is dropped from queue by timeout.
std::vector< PassiveBuffEntry > m_passiveBuffer
The send buffer to cache unsent packet.
void Drop(PassiveBuffEntry en, std::string reason)
Notify that packet is dropped from queue by timeout.
Time GetExpireTime() const
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
#define NS_LOG_DEBUG(msg)
Definition: log.h:289
Ptr< const Packet > GetPacket() const
uint32_t m_maxLen
The maximum number of packets that we allow a routing protocol to buffer.
a base class which provides memory management and object aggregation
Definition: object.h:63
Ipv4Address GetDestination() const
Ipv4Address GetNextHop() const
bool Find(Ipv4Address dst)
Finds whether a packet with destination dst exists in the queue.
NS_LOG_COMPONENT_DEFINE("PassiveBuffer")
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:611