A Discrete-Event Network Simulator
API
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 namespace ns3 {
40 
41 NS_LOG_COMPONENT_DEFINE ("DsrMaintainBuffer");
42 
43 namespace dsr {
44 
45 uint32_t
47 {
48  Purge ();
49  return m_maintainBuffer.size ();
50 }
51 
52 bool
54 {
55  Purge ();
56  for (std::vector<DsrMaintainBuffEntry>::const_iterator i = m_maintainBuffer.begin (); i
57  != m_maintainBuffer.end (); ++i)
58  {
59 // NS_LOG_INFO ("nexthop " << i->GetNextHop () << " " << entry.GetNextHop () << " our add " << i->GetOurAdd () << " " << entry.GetOurAdd ()
60 // << " src " << i->GetSrc () << " " << entry.GetSrc () << " dst " << i->GetDst () << " " << entry.GetDst ()
61 // << " ackId " << i->GetAckId () << " " << entry.GetAckId () << " SegsLeft " << (uint32_t)i->GetSegsLeft () << " " << (uint32_t)entry.GetSegsLeft ()
62 // );
63 
64  if ((i->GetNextHop () == entry.GetNextHop ()) && (i->GetOurAdd () == entry.GetOurAdd ()) && (i->GetSrc () == entry.GetSrc ())
65  && (i->GetDst () == entry.GetDst ()) && (i->GetAckId () == entry.GetAckId ()) && (i->GetSegsLeft () == entry.GetSegsLeft ()))
66  {
67  NS_LOG_DEBUG ("Same maintenance entry found");
68  return false;
69  }
70  }
71 
73  if (m_maintainBuffer.size () >= m_maxLen)
74  {
75  NS_LOG_DEBUG ("Drop the most aged packet");
76  m_maintainBuffer.erase (m_maintainBuffer.begin ()); // Drop the most aged packet
77  }
78  m_maintainBuffer.push_back (entry);
79  return true;
80 }
81 
82 void
84 {
85  NS_LOG_FUNCTION (this << nextHop);
86  Purge ();
87  NS_LOG_INFO ("Drop Packet With next hop " << nextHop);
88  m_maintainBuffer.erase (std::remove_if (m_maintainBuffer.begin (), m_maintainBuffer.end (),
89  std::bind2nd (std::ptr_fun (DsrMaintainBuffer::IsEqual), nextHop)), m_maintainBuffer.end ());
90 }
91 
92 bool
94 {
95  Purge ();
96  for (std::vector<DsrMaintainBuffEntry>::iterator i = m_maintainBuffer.begin (); i != m_maintainBuffer.end (); ++i)
97  {
98  if (i->GetNextHop () == nextHop)
99  {
100  entry = *i;
101  i = m_maintainBuffer.erase (i);
102  NS_LOG_DEBUG ("Packet size while dequeuing " << entry.GetPacket ()->GetSize ());
103  return true;
104  }
105  }
106  return false;
107 }
108 
109 bool
111 {
112  for (std::vector<DsrMaintainBuffEntry>::const_iterator i = m_maintainBuffer.begin (); i
113  != m_maintainBuffer.end (); ++i)
114  {
115  if (i->GetNextHop () == nextHop)
116  {
117  NS_LOG_DEBUG ("Found the packet in maintenance buffer");
118  return true;
119  }
120  }
121  return false;
122 }
123 
124 bool
126 {
127  for (std::vector<DsrMaintainBuffEntry>::iterator i = m_maintainBuffer.begin (); i
128  != m_maintainBuffer.end (); ++i)
129  {
130 // NS_LOG_DEBUG ("nexthop " << i->GetNextHop () << " " << entry.GetNextHop () << " our address " << i->GetOurAdd () << " " << entry.GetOurAdd ()
131 // << " src " << i->GetSrc () << " " << entry.GetSrc () << " dst " << i->GetDst () << " " << entry.GetDst ()
132 // << " ackId " << i->GetAckId () << " " << entry.GetAckId ());
133 
134  if ((i->GetOurAdd () == entry.GetOurAdd ()) && (i->GetNextHop () == entry.GetNextHop ())
135  && (i->GetSrc () == entry.GetSrc ()) && (i->GetDst () == entry.GetDst ())
136  && (i->GetAckId () == entry.GetAckId ()) && (i->GetSegsLeft () == entry.GetSegsLeft ()))
137  {
138  i = m_maintainBuffer.erase (i); // Erase the same maintain buffer entry for the received packet
139  return true;
140  }
141  }
142  return false;
143 }
144 
145 bool
147 {
148  for (std::vector<DsrMaintainBuffEntry>::iterator i = m_maintainBuffer.begin (); i
149  != m_maintainBuffer.end (); ++i)
150  {
151 // NS_LOG_DEBUG ("nexthop " << i->GetNextHop () << " " << entry.GetNextHop () << " our address " << i->GetOurAdd () << " " << entry.GetOurAdd ()
152 // << " src " << i->GetSrc () << " " << entry.GetSrc () << " dst " << i->GetDst () << " " << entry.GetDst ()
153 // << " ackId " << i->GetAckId () << " " << entry.GetAckId ());
154 
155  if ((i->GetOurAdd () == entry.GetOurAdd ()) && (i->GetNextHop () == entry.GetNextHop ())
156  && (i->GetSrc () == entry.GetSrc ()) && (i->GetDst () == entry.GetDst ())
157  && (i->GetAckId () == entry.GetAckId ()))
158  {
159  i = m_maintainBuffer.erase (i); // Erase the same maintain buffer entry for the received packet
160  return true;
161  }
162  }
163  return false;
164 }
165 
166 bool
168 {
169  NS_LOG_DEBUG ("The maintenance buffer size " << m_maintainBuffer.size ());
170  for (std::vector<DsrMaintainBuffEntry>::iterator i = m_maintainBuffer.begin (); i
171  != m_maintainBuffer.end (); ++i)
172  {
173 // NS_LOG_DEBUG ("src " << i->GetSrc () << " " << entry.GetSrc () << " dst " << i->GetDst () << " " << entry.GetDst ()
174 // << " SegsLeft " << (uint32_t)i->GetSegsLeft () << " " << (uint32_t)entry.GetSegsLeft () << " ackId " << (uint32_t)i->GetAckId () << " "
175 // << (uint32_t)entry.GetAckId ()
176 // );
177 
178  if ((i->GetSrc () == entry.GetSrc ()) && (i->GetDst () == entry.GetDst ())
179  && (i->GetSegsLeft () == entry.GetSegsLeft ()) && (i->GetAckId () == entry.GetAckId ())
180  )
181  {
182  i = m_maintainBuffer.erase (i); // Erase the same maintain buffer entry for the promisc received packet
183  return true;
184  }
185  }
186  return false;
187 }
188 
189 bool
191 {
192  NS_LOG_DEBUG ("The maintenance buffer size " << m_maintainBuffer.size ());
193  for (std::vector<DsrMaintainBuffEntry>::iterator i = m_maintainBuffer.begin (); i
194  != m_maintainBuffer.end (); ++i)
195  {
196 // NS_LOG_DEBUG ("src " << i->GetSrc () << " " << entry.GetSrc () << " dst " << i->GetDst () << " " << entry.GetDst ()
197 // << " OurAddress " << i->GetOurAdd () << " " << entry.GetOurAdd () << " next hop " << i->GetNextHop () << " "
198 // << entry.GetNextHop ()
199 // );
200 
201  if ((i->GetSrc () == entry.GetSrc ()) && (i->GetDst () == entry.GetDst ()) && (i->GetOurAdd () == entry.GetOurAdd ())
202  && (i->GetNextHop () == entry.GetNextHop ())
203  )
204  {
205  i = m_maintainBuffer.erase (i); // Erase the same maintain buffer entry for the promisc received packet
206  return true;
207  }
208  }
209  return false;
210 }
211 
213 struct IsExpired
214 {
220  bool
222  {
223  // NS_LOG_DEBUG("Expire time for packet in req queue: "<<e.GetExpireTime ());
224  return (e.GetExpireTime () < Seconds (0));
225  }
226 };
227 
228 void
230 {
231  NS_LOG_DEBUG ("Purging Maintenance Buffer");
232  IsExpired pred;
233  m_maintainBuffer.erase (std::remove_if (m_maintainBuffer.begin (), m_maintainBuffer.end (), pred),
234  m_maintainBuffer.end ());
235 }
236 
237 } // namespace dsr
238 } // namespace ns3
uint8_t GetSegsLeft() const
Get segments left.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:831
bool NetworkEqual(DsrMaintainBuffEntry &entry)
Verify if the maintain buffer entry is the same in every field for network ack.
DSR Maintain Buffer Entry.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
void DropPacketWithNextHop(Ipv4Address nextHop)
Remove all packets with next hop IP address dst.
Ipv4Address GetDst() const
Get destination address.
bool AllEqual(DsrMaintainBuffEntry &entry)
Verify if all the elements in the maintenance buffer entry is the same.
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:278
uint32_t GetSize()
Number of entries.
bool Find(Ipv4Address nextHop)
Finds whether a packet with next hop dst exists in the queue.
Ptr< const Packet > GetPacket() const
Get packet.
Ipv4Address GetSrc() const
Get source address.
bool PromiscEqual(DsrMaintainBuffEntry &entry)
Verify if the maintain buffer entry is the same in every field for promiscuous ack.
void SetExpireTime(Time exp)
Set expiration time.
uint32_t m_maxLen
The maximum number of packets that we allow a routing protocol to buffer.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Time m_maintainBufferTimeout
The maximum period of time that a routing protocol is allowed to buffer a packet for, seconds.
static bool IsEqual(DsrMaintainBuffEntry en, const Ipv4Address nextHop)
Verify if the maintain buffer is equal or not.
Time GetExpireTime() const
Get expiration time.
void Purge()
Remove all expired entries.
std::vector< DsrMaintainBuffEntry > m_maintainBuffer
The vector of maintain buffer entries.
Ipv4Address GetOurAdd() const
Get local address of entry.
bool Dequeue(Ipv4Address dst, DsrMaintainBuffEntry &entry)
Return first found (the earliest) entry for given destination.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
Ipv4Address GetNextHop() const
Get next hop of entry.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:270
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1014
bool Enqueue(DsrMaintainBuffEntry &entry)
Push entry in queue, if there is no entry with the same packet and destination address in queue...
bool LinkEqual(DsrMaintainBuffEntry &entry)
Verify if the maintain buffer entry is the same in every field for link ack.
bool operator()(DsrErrorBuffEntry const &e) const
comparison operator
IsExpired structure.
uint16_t GetAckId() const
Get acknowledge ID.