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