A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
dsr-maintain-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-maintain-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 ("DsrMaintainBuffer");
40 
41 namespace ns3 {
42 namespace dsr {
43 
44 uint32_t
46 {
47  Purge ();
48  return m_maintainBuffer.size ();
49 }
50 
51 bool
53 {
54  Purge ();
55  for (std::vector<MaintainBuffEntry>::const_iterator i = m_maintainBuffer.begin (); i
56  != m_maintainBuffer.end (); ++i)
57  {
58 // NS_LOG_INFO ("nexthop " << i->GetNextHop () << " " << entry.GetNextHop () << " our add " << i->GetOurAdd () << " " << entry.GetOurAdd ()
59 // << " src " << i->GetSrc () << " " << entry.GetSrc () << " dst " << i->GetDst () << " " << entry.GetDst ()
60 // << " ackId " << i->GetAckId () << " " << entry.GetAckId () << " SegsLeft " << (uint32_t)i->GetSegsLeft () << " " << (uint32_t)entry.GetSegsLeft ()
61 // );
62 
63  if ((i->GetNextHop () == entry.GetNextHop ()) && (i->GetOurAdd () == entry.GetOurAdd ()) && (i->GetSrc () == entry.GetSrc ())
64  && (i->GetDst () == entry.GetDst ()) && (i->GetAckId () == entry.GetAckId ()) && (i->GetSegsLeft () == entry.GetSegsLeft ()))
65  {
66  NS_LOG_DEBUG ("Same maintenance entry found");
67  return false;
68  }
69  }
70 
72  if (m_maintainBuffer.size () >= m_maxLen)
73  {
74  NS_LOG_DEBUG ("Drop the most aged packet");
75  m_maintainBuffer.erase (m_maintainBuffer.begin ()); // Drop the most aged packet
76  }
77  m_maintainBuffer.push_back (entry);
78  return true;
79 }
80 
81 void
83 {
84  NS_LOG_FUNCTION (this << nextHop);
85  Purge ();
86  NS_LOG_INFO ("Drop Packet With next hop " << nextHop);
87  m_maintainBuffer.erase (std::remove_if (m_maintainBuffer.begin (), m_maintainBuffer.end (),
88  std::bind2nd (std::ptr_fun (MaintainBuffer::IsEqual), nextHop)), m_maintainBuffer.end ());
89 }
90 
91 bool
93 {
94  Purge ();
95  for (std::vector<MaintainBuffEntry>::iterator i = m_maintainBuffer.begin (); i != m_maintainBuffer.end (); ++i)
96  {
97  if (i->GetNextHop () == nextHop)
98  {
99  entry = *i;
100  m_maintainBuffer.erase (i);
101  NS_LOG_DEBUG ("Packet size while dequeuing " << entry.GetPacket ()->GetSize ());
102  return true;
103  }
104  }
105  return false;
106 }
107 
108 bool
110 {
111  for (std::vector<MaintainBuffEntry>::const_iterator i = m_maintainBuffer.begin (); i
112  != m_maintainBuffer.end (); ++i)
113  {
114  if (i->GetNextHop () == nextHop)
115  {
116  NS_LOG_DEBUG ("Found the packet in maintenance buffer");
117  return true;
118  }
119  }
120  return false;
121 }
122 
123 bool
125 {
126  for (std::vector<MaintainBuffEntry>::iterator i = m_maintainBuffer.begin (); i
127  != m_maintainBuffer.end (); ++i)
128  {
129 // NS_LOG_DEBUG ("nexthop " << i->GetNextHop () << " " << entry.GetNextHop () << " our address " << i->GetOurAdd () << " " << entry.GetOurAdd ()
130 // << " src " << i->GetSrc () << " " << entry.GetSrc () << " dst " << i->GetDst () << " " << entry.GetDst ()
131 // << " ackId " << i->GetAckId () << " " << entry.GetAckId ());
132 
133  if ((i->GetOurAdd () == entry.GetOurAdd ()) && (i->GetNextHop () == entry.GetNextHop ())
134  && (i->GetSrc () == entry.GetSrc ()) && (i->GetDst () == entry.GetDst ())
135  && (i->GetAckId () == entry.GetAckId ()) && (i->GetSegsLeft () == entry.GetSegsLeft ()))
136  {
137  m_maintainBuffer.erase (i); // Erase the same maintain buffer entry for the received packet
138  return true;
139  }
140  }
141  return false;
142 }
143 
144 bool
146 {
147  for (std::vector<MaintainBuffEntry>::iterator i = m_maintainBuffer.begin (); i
148  != m_maintainBuffer.end (); ++i)
149  {
150 // NS_LOG_DEBUG ("nexthop " << i->GetNextHop () << " " << entry.GetNextHop () << " our address " << i->GetOurAdd () << " " << entry.GetOurAdd ()
151 // << " src " << i->GetSrc () << " " << entry.GetSrc () << " dst " << i->GetDst () << " " << entry.GetDst ()
152 // << " ackId " << i->GetAckId () << " " << entry.GetAckId ());
153 
154  if ((i->GetOurAdd () == entry.GetOurAdd ()) && (i->GetNextHop () == entry.GetNextHop ())
155  && (i->GetSrc () == entry.GetSrc ()) && (i->GetDst () == entry.GetDst ())
156  && (i->GetAckId () == entry.GetAckId ()))
157  {
158  m_maintainBuffer.erase (i); // Erase the same maintain buffer entry for the received packet
159  return true;
160  }
161  }
162  return false;
163 }
164 
165 bool
167 {
168  NS_LOG_DEBUG ("The maintenance buffer size " << m_maintainBuffer.size ());
169  for (std::vector<MaintainBuffEntry>::iterator i = m_maintainBuffer.begin (); i
170  != m_maintainBuffer.end (); ++i)
171  {
172 // NS_LOG_DEBUG ("src " << i->GetSrc () << " " << entry.GetSrc () << " dst " << i->GetDst () << " " << entry.GetDst ()
173 // << " SegsLeft " << (uint32_t)i->GetSegsLeft () << " " << (uint32_t)entry.GetSegsLeft () << " ackId " << (uint32_t)i->GetAckId () << " "
174 // << (uint32_t)entry.GetAckId ()
175 // );
176 
177  if ((i->GetSrc () == entry.GetSrc ()) && (i->GetDst () == entry.GetDst ())
178  && (i->GetSegsLeft () == entry.GetSegsLeft ()) && (i->GetAckId () == entry.GetAckId ())
179  )
180  {
181  m_maintainBuffer.erase (i); // Erase the same maintain buffer entry for the promisc received packet
182  return true;
183  }
184  }
185  return false;
186 }
187 
188 bool
190 {
191  NS_LOG_DEBUG ("The maintenance buffer size " << m_maintainBuffer.size ());
192  for (std::vector<MaintainBuffEntry>::iterator i = m_maintainBuffer.begin (); i
193  != m_maintainBuffer.end (); ++i)
194  {
195 // NS_LOG_DEBUG ("src " << i->GetSrc () << " " << entry.GetSrc () << " dst " << i->GetDst () << " " << entry.GetDst ()
196 // << " OurAddress " << i->GetOurAdd () << " " << entry.GetOurAdd () << " next hop " << i->GetNextHop () << " "
197 // << entry.GetNextHop ()
198 // );
199 
200  if ((i->GetSrc () == entry.GetSrc ()) && (i->GetDst () == entry.GetDst ()) && (i->GetOurAdd () == entry.GetOurAdd ())
201  && (i->GetNextHop () == entry.GetNextHop ())
202  )
203  {
204  m_maintainBuffer.erase (i); // Erase the same maintain buffer entry for the promisc received packet
205  return true;
206  }
207  }
208  return false;
209 }
210 
211 struct IsExpired
212 {
213  bool
215  {
216  // NS_LOG_DEBUG("Expire time for packet in req queue: "<<e.GetExpireTime ());
217  return (e.GetExpireTime () < Seconds (0));
218  }
219 };
220 
221 void
223 {
224  NS_LOG_DEBUG ("Purging Maintenance Buffer");
225  IsExpired pred;
226  m_maintainBuffer.erase (std::remove_if (m_maintainBuffer.begin (), m_maintainBuffer.end (), pred),
227  m_maintainBuffer.end ());
228 }
229 
230 } // namespace dsr
231 } // namespace ns3
bool operator()(ErrorBuffEntry const &e) const
void SetExpireTime(Time exp)
bool NetworkEqual(MaintainBuffEntry &entry)
Verify if the maintain buffer entry is the same in every field for network ack.
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
static bool IsEqual(MaintainBuffEntry en, const Ipv4Address nextHop)
Verify if the maintain buffer is equal or not.
Time GetExpireTime() const
uint32_t GetSize(void) const
Definition: packet.h:650
#define NS_LOG_INFO(msg)
Definition: log.h:298
bool AllEqual(MaintainBuffEntry &entry)
Verify if all the elements in the maintainence buffer entry is the same.
void DropPacketWithNextHop(Ipv4Address nextHop)
Remove all packets with destination IP address dst.
Ipv4Address GetOurAdd() const
bool LinkEqual(MaintainBuffEntry &entry)
Verify if the maintain buffer entry is the same in every field for link ack.
uint32_t m_maxLen
The maximum number of packets that we allow a routing protocol to buffer.
bool Find(Ipv4Address nextHop)
Finds whether a packet with destination dst exists in the queue.
bool PromiscEqual(MaintainBuffEntry &entry)
Verify if the maintain buffer entry is the same in every field for promiscuous ack.
Ipv4Address GetSrc() const
uint16_t GetAckId() const
Time m_maintainBufferTimeout
The maximum period of time that a routing protocol is allowed to buffer a packet for, seconds.
NS_LOG_COMPONENT_DEFINE("DsrMaintainBuffer")
Ipv4Address GetDst() const
DSR Maintain Buffer Entry.
uint32_t GetSize()
Number of entries.
bool Enqueue(MaintainBuffEntry &entry)
Push entry in queue, if there is no entry with the same packet and destination address in queue...
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
#define NS_LOG_DEBUG(msg)
Definition: log.h:289
void Purge()
Remove all expired entries.
Ptr< const Packet > GetPacket() const
bool Dequeue(Ipv4Address dst, MaintainBuffEntry &entry)
Return first found (the earliest) entry for given destination.
uint8_t GetSegsLeft() const
std::vector< MaintainBuffEntry > m_maintainBuffer
The vector of maintain buffer entries.
Ipv4Address GetNextHop() const