A Discrete-Event Network Simulator
API
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 namespace ns3 {
40 
41 NS_LOG_COMPONENT_DEFINE ("DsrPassiveBuffer");
42 
43 namespace dsr {
44 
45 NS_OBJECT_ENSURE_REGISTERED (DsrPassiveBuffer);
46 
48 {
49  static TypeId tid = TypeId ("ns3::dsr::DsrPassiveBuffer")
50  .SetParent<Object> ()
51  .SetGroupName ("Dsr")
52  .AddConstructor<DsrPassiveBuffer> ()
53  ;
54  return tid;
55 }
56 
58 {
59 }
60 
62 {
63 }
64 
65 uint32_t
67 {
68  Purge ();
69  return m_passiveBuffer.size ();
70 }
71 
72 bool
74 {
75  Purge ();
76  for (std::vector<DsrPassiveBuffEntry>::const_iterator i = m_passiveBuffer.begin (); i
77  != m_passiveBuffer.end (); ++i)
78  {
79 // NS_LOG_INFO ("packet id " << i->GetPacket ()->GetUid () << " " << entry.GetPacket ()->GetUid () << " source " << i->GetSource () << " " << entry.GetSource ()
80 // << " dst " << i->GetDestination () << " " << entry.GetDestination () << " identification " << i->GetIdentification () << " "
81 // << entry.GetIdentification () << " fragment " << i->GetFragmentOffset () << " " << entry.GetFragmentOffset ()
82 // << " segLeft " << i->GetSegsLeft () << " " << entry.GetSegsLeft ());
83 
84  if ((i->GetPacket ()->GetUid () == entry.GetPacket ()->GetUid ()) && (i->GetSource () == entry.GetSource ()) && (i->GetNextHop () == entry.GetNextHop ())
85  && (i->GetDestination () == entry.GetDestination ()) && (i->GetIdentification () == entry.GetIdentification ()) && (i->GetFragmentOffset () == entry.GetFragmentOffset ())
86  && (i->GetSegsLeft () == entry.GetSegsLeft () + 1))
87  {
88  return false;
89  }
90  }
91 
92  entry.SetExpireTime (m_passiveBufferTimeout); // Initialize the send buffer timeout
93  /*
94  * Drop the most aged packet when buffer reaches to max
95  */
96  if (m_passiveBuffer.size () >= m_maxLen)
97  {
98  Drop (m_passiveBuffer.front (), "Drop the most aged packet"); // Drop the most aged packet
99  m_passiveBuffer.erase (m_passiveBuffer.begin ());
100  }
101  // enqueue the entry
102  m_passiveBuffer.push_back (entry);
103  return true;
104 }
105 
106 bool
108 {
109  for (std::vector<DsrPassiveBuffEntry>::iterator i = m_passiveBuffer.begin (); i
110  != m_passiveBuffer.end (); ++i)
111  {
112 // NS_LOG_INFO ("packet id " << i->GetPacket ()->GetUid () << " " << entry.GetPacket ()->GetUid () << " source " << i->GetSource () << " " << entry.GetSource ()
113 // << " dst " << i->GetDestination () << " " << entry.GetDestination () << " identification " << i->GetIdentification () << " "
114 // << entry.GetIdentification () << " fragment " << i->GetFragmentOffset () << " " << entry.GetFragmentOffset ()
115 // << " segLeft " << (uint32_t) i->GetSegsLeft () << " " << (uint32_t) entry.GetSegsLeft ());
116 
117  if ((i->GetPacket ()->GetUid () == entry.GetPacket ()->GetUid ()) && (i->GetSource () == entry.GetSource ()) && (i->GetNextHop () == entry.GetNextHop ())
118  && (i->GetDestination () == entry.GetDestination ()) && (i->GetIdentification () == entry.GetIdentification ()) && (i->GetFragmentOffset () == entry.GetFragmentOffset ())
119  && (i->GetSegsLeft () == entry.GetSegsLeft () + 1))
120  {
121  i = m_passiveBuffer.erase (i); // Erase the same maintain buffer entry for the received packet
122  return true;
123  }
124  }
125  return false;
126 }
127 
128 bool
130 {
131  Purge ();
132  /*
133  * Dequeue the entry with destination address dst
134  */
135  for (std::vector<DsrPassiveBuffEntry>::iterator i = m_passiveBuffer.begin (); i != m_passiveBuffer.end (); ++i)
136  {
137  if (i->GetDestination () == dst)
138  {
139  entry = *i;
140  i = m_passiveBuffer.erase (i);
141  NS_LOG_DEBUG ("Packet size while dequeuing " << entry.GetPacket ()->GetSize ());
142  return true;
143  }
144  }
145  return false;
146 }
147 
148 bool
150 {
151  /*
152  * Make sure if the send buffer contains entry with certain dst
153  */
154  for (std::vector<DsrPassiveBuffEntry>::const_iterator i = m_passiveBuffer.begin (); i
155  != m_passiveBuffer.end (); ++i)
156  {
157  if (i->GetDestination () == dst)
158  {
159  NS_LOG_DEBUG ("Found the packet");
160  return true;
161  }
162  }
163  return false;
164 }
165 
166 struct IsExpired
167 {
168  bool
170  {
171  // NS_LOG_DEBUG("Expire time for packet in req queue: "<<e.GetExpireTime ());
172  return (e.GetExpireTime () < Seconds (0));
173  }
174 };
175 
176 void
178 {
179  /*
180  * Purge the buffer to eliminate expired entries
181  */
182  NS_LOG_DEBUG ("The passive buffer size " << m_passiveBuffer.size ());
183  IsExpired pred;
184  for (std::vector<DsrPassiveBuffEntry>::iterator i = m_passiveBuffer.begin (); i
185  != m_passiveBuffer.end (); ++i)
186  {
187  if (pred (*i))
188  {
189  NS_LOG_DEBUG ("Dropping Queue Packets");
190  Drop (*i, "Drop out-dated packet ");
191  }
192  }
193  m_passiveBuffer.erase (std::remove_if (m_passiveBuffer.begin (), m_passiveBuffer.end (), pred),
194  m_passiveBuffer.end ());
195 }
196 
197 void
199 {
200  NS_LOG_LOGIC (reason << en.GetPacket ()->GetUid () << " " << en.GetDestination ());
201 // en.GetErrorCallback () (en.GetPacket (), en.GetDestination (),
202 // Socket::ERROR_NOROUTETOHOST);
203  return;
204 }
205 
206 void
208 {
209  NS_LOG_LOGIC (reason << en.GetPacket ()->GetUid () << " " << en.GetSource () << " " << en.GetNextHop ());
210 // en.GetErrorCallback () (en.GetPacket (), en.GetDestination (),
211 // Socket::ERROR_NOROUTETOHOST);
212  return;
213 }
214 } // namespace dsr
215 } // namespace ns3
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
void DropLink(DsrPassiveBuffEntry en, std::string reason)
Notify that packet is dropped from queue by timeout.
uint64_t GetUid(void) const
Returns the packet's Uid.
Definition: packet.cc:368
DSR passive buffer.
uint32_t GetSize()
Number of entries.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
bool operator()(DsrErrorBuffEntry const &e) const
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:792
uint8_t GetSegsLeft() const
uint32_t m_maxLen
The maximum number of packets that we allow a routing protocol to buffer.
uint16_t GetFragmentOffset() const
void SetExpireTime(Time exp)
Ipv4Address GetSource() const
std::vector< DsrPassiveBuffEntry > m_passiveBuffer
The send buffer to cache unsent packet.
void Purge()
Remove all expired entries.
DSR Passive Buffer Entry.
bool Dequeue(Ipv4Address dst, DsrPassiveBuffEntry &entry)
Return first found (the earliest) entry for given destination.
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:252
bool AllEqual(DsrPassiveBuffEntry &entry)
Check if all the entries in passive buffer entry is all equal or not.
Ipv4Address GetDestination() const
bool Enqueue(DsrPassiveBuffEntry &entry)
Push entry in queue, if there is no entry with the same packet and destination address in queue...
Time m_passiveBufferTimeout
The maximum period of time that a routing protocol is allowed to buffer a packet for, seconds.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
uint16_t GetIdentification() const
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
Ipv4Address GetNextHop() const
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:236
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
void Drop(DsrPassiveBuffEntry en, std::string reason)
Notify that packet is dropped from queue by timeout.
A base class which provides memory management and object aggregation.
Definition: object.h:87
Time GetExpireTime() 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:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:904
Ptr< const Packet > GetPacket() const