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 
47 {
48  static TypeId tid = TypeId ("ns3::dsr::PassiveBuffer")
49  .SetParent<Object> ()
50  .AddConstructor<PassiveBuffer> ()
51  ;
52  return tid;
53 }
54 
56 {
57 }
58 
60 {
61 }
62 
63 uint32_t
65 {
66  Purge ();
67  return m_passiveBuffer.size ();
68 }
69 
70 bool
72 {
73  Purge ();
74  for (std::vector<PassiveBuffEntry>::const_iterator i = m_passiveBuffer.begin (); i
75  != m_passiveBuffer.end (); ++i)
76  {
77 // NS_LOG_INFO ("packet id " << i->GetPacket ()->GetUid () << " " << entry.GetPacket ()->GetUid () << " source " << i->GetSource () << " " << entry.GetSource ()
78 // << " dst " << i->GetDestination () << " " << entry.GetDestination () << " identification " << i->GetIdentification () << " "
79 // << entry.GetIdentification () << " fragment " << i->GetFragmentOffset () << " " << entry.GetFragmentOffset ()
80 // << " segLeft " << i->GetSegsLeft () << " " << entry.GetSegsLeft ());
81 
82  if ((i->GetPacket ()->GetUid () == entry.GetPacket ()->GetUid ()) && (i->GetSource () == entry.GetSource ()) && (i->GetNextHop () == entry.GetNextHop ())
83  && (i->GetDestination () == entry.GetDestination ()) && (i->GetIdentification () == entry.GetIdentification ()) && (i->GetFragmentOffset () == entry.GetFragmentOffset ())
84  && (i->GetSegsLeft () == entry.GetSegsLeft () + 1))
85  {
86  return false;
87  }
88  }
89 
90  entry.SetExpireTime (m_passiveBufferTimeout); // Initialize the send buffer timeout
91  /*
92  * Drop the most aged packet when buffer reaches to max
93  */
94  if (m_passiveBuffer.size () >= m_maxLen)
95  {
96  Drop (m_passiveBuffer.front (), "Drop the most aged packet"); // Drop the most aged packet
97  m_passiveBuffer.erase (m_passiveBuffer.begin ());
98  }
99  // enqueue the entry
100  m_passiveBuffer.push_back (entry);
101  return true;
102 }
103 
104 bool
106 {
107  for (std::vector<PassiveBuffEntry>::iterator i = m_passiveBuffer.begin (); i
108  != m_passiveBuffer.end (); ++i)
109  {
110 // NS_LOG_INFO ("packet id " << i->GetPacket ()->GetUid () << " " << entry.GetPacket ()->GetUid () << " source " << i->GetSource () << " " << entry.GetSource ()
111 // << " dst " << i->GetDestination () << " " << entry.GetDestination () << " identification " << i->GetIdentification () << " "
112 // << entry.GetIdentification () << " fragment " << i->GetFragmentOffset () << " " << entry.GetFragmentOffset ()
113 // << " segLeft " << (uint32_t) i->GetSegsLeft () << " " << (uint32_t) entry.GetSegsLeft ());
114 
115  if ((i->GetPacket ()->GetUid () == entry.GetPacket ()->GetUid ()) && (i->GetSource () == entry.GetSource ()) && (i->GetNextHop () == entry.GetNextHop ())
116  && (i->GetDestination () == entry.GetDestination ()) && (i->GetIdentification () == entry.GetIdentification ()) && (i->GetFragmentOffset () == entry.GetFragmentOffset ())
117  && (i->GetSegsLeft () == entry.GetSegsLeft () + 1))
118  {
119  i = m_passiveBuffer.erase (i); // Erase the same maintain buffer entry for the received packet
120  return true;
121  }
122  }
123  return false;
124 }
125 
126 bool
128 {
129  Purge ();
130  /*
131  * Dequeue the entry with destination address dst
132  */
133  for (std::vector<PassiveBuffEntry>::iterator i = m_passiveBuffer.begin (); i != m_passiveBuffer.end (); ++i)
134  {
135  if (i->GetDestination () == dst)
136  {
137  entry = *i;
138  i = m_passiveBuffer.erase (i);
139  NS_LOG_DEBUG ("Packet size while dequeuing " << entry.GetPacket ()->GetSize ());
140  return true;
141  }
142  }
143  return false;
144 }
145 
146 bool
148 {
149  /*
150  * Make sure if the send buffer contains entry with certain dst
151  */
152  for (std::vector<PassiveBuffEntry>::const_iterator i = m_passiveBuffer.begin (); i
153  != m_passiveBuffer.end (); ++i)
154  {
155  if (i->GetDestination () == dst)
156  {
157  NS_LOG_DEBUG ("Found the packet");
158  return true;
159  }
160  }
161  return false;
162 }
163 
164 struct IsExpired
165 {
166  bool
167  operator() (PassiveBuffEntry const & e) const
168  {
169  // NS_LOG_DEBUG("Expire time for packet in req queue: "<<e.GetExpireTime ());
170  return (e.GetExpireTime () < Seconds (0));
171  }
172 };
173 
174 void
176 {
177  /*
178  * Purge the buffer to eliminate expired entries
179  */
180  NS_LOG_DEBUG ("The passive buffer size " << m_passiveBuffer.size ());
181  IsExpired pred;
182  for (std::vector<PassiveBuffEntry>::iterator i = m_passiveBuffer.begin (); i
183  != m_passiveBuffer.end (); ++i)
184  {
185  if (pred (*i))
186  {
187  NS_LOG_DEBUG ("Dropping Queue Packets");
188  Drop (*i, "Drop out-dated packet ");
189  }
190  }
191  m_passiveBuffer.erase (std::remove_if (m_passiveBuffer.begin (), m_passiveBuffer.end (), pred),
192  m_passiveBuffer.end ());
193 }
194 
195 void
196 PassiveBuffer::Drop (PassiveBuffEntry en, std::string reason)
197 {
198  NS_LOG_LOGIC (reason << en.GetPacket ()->GetUid () << " " << en.GetDestination ());
199 // en.GetErrorCallback () (en.GetPacket (), en.GetDestination (),
200 // Socket::ERROR_NOROUTETOHOST);
201  return;
202 }
203 
204 void
206 {
207  NS_LOG_LOGIC (reason << en.GetPacket ()->GetUid () << " " << en.GetSource () << " " << en.GetNextHop ());
208 // en.GetErrorCallback () (en.GetPacket (), en.GetDestination (),
209 // Socket::ERROR_NOROUTETOHOST);
210  return;
211 }
212 } // namespace dsr
213 } // namespace ns3
bool operator()(ErrorBuffEntry const &e) const
void SetExpireTime(Time exp)
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register the class in the ns-3 factory.
Definition: object-base.h:38
DSR Passive Buffer Entry.
uint64_t GetUid(void) const
Returns the packet's Uid.
Definition: packet.cc:393
void Purge()
Remove all expired entries.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:744
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)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:233
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)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:213
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:64
Ipv4Address GetDestination() const
Ipv4Address GetNextHop() const
bool Find(Ipv4Address dst)
Finds whether a packet with destination dst exists in the queue.
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:610