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
39namespace ns3 {
40
41NS_LOG_COMPONENT_DEFINE ("DsrPassiveBuffer");
42
43namespace dsr {
44
45NS_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
67{
68 Purge ();
69 return m_passiveBuffer.size ();
70}
71
72bool
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
106bool
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
128bool
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
148bool
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
167struct IsExpired
168{
174 bool
176 {
177 // NS_LOG_DEBUG("Expire time for packet in req queue: "<<e.GetExpireTime ());
178 return (e.GetExpireTime () < Seconds (0));
179 }
180};
181
182void
184{
185 /*
186 * Purge the buffer to eliminate expired entries
187 */
188 NS_LOG_DEBUG ("The passive buffer size " << m_passiveBuffer.size ());
189 IsExpired pred;
190 for (std::vector<DsrPassiveBuffEntry>::iterator i = m_passiveBuffer.begin (); i
191 != m_passiveBuffer.end (); ++i)
192 {
193 if (pred (*i))
194 {
195 NS_LOG_DEBUG ("Dropping Queue Packets");
196 Drop (*i, "Drop out-dated packet ");
197 }
198 }
199 m_passiveBuffer.erase (std::remove_if (m_passiveBuffer.begin (), m_passiveBuffer.end (), pred),
200 m_passiveBuffer.end ());
201}
202
203void
205{
206 NS_LOG_LOGIC (reason << en.GetPacket ()->GetUid () << " " << en.GetDestination ());
207// en.GetErrorCallback () (en.GetPacket (), en.GetDestination (),
208// Socket::ERROR_NOROUTETOHOST);
209 return;
210}
211
212void
214{
215 NS_LOG_LOGIC (reason << en.GetPacket ()->GetUid () << " " << en.GetSource () << " " << en.GetNextHop ());
216// en.GetErrorCallback () (en.GetPacket (), en.GetDestination (),
217// Socket::ERROR_NOROUTETOHOST);
218 return;
219}
220} // namespace dsr
221} // namespace ns3
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:41
A base class which provides memory management and object aggregation.
Definition: object.h:88
uint64_t GetUid(void) const
Returns the packet's Uid.
Definition: packet.cc:390
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:856
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:922
DSR Passive Buffer Entry.
void SetExpireTime(Time exp)
Set expire time.
Ipv4Address GetDestination() const
Get destination address function.
Ptr< const Packet > GetPacket() const
Get packet function.
Ipv4Address GetNextHop() const
Get next hop address function.
Ipv4Address GetSource() const
Get source address function.
Time GetExpireTime() const
Get expire time.
uint8_t GetSegsLeft() const
Get segments left function.
uint16_t GetIdentification() const
Get identification function.
uint16_t GetFragmentOffset() const
Get fragment offset function.
bool Enqueue(DsrPassiveBuffEntry &entry)
Push entry in queue, if there is no entry with the same packet and destination address in queue.
uint32_t GetSize()
Number of entries.
bool Find(Ipv4Address dst)
Finds whether a packet with destination dst exists in the queue.
bool Dequeue(Ipv4Address dst, DsrPassiveBuffEntry &entry)
Return first found (the earliest) entry for given destination.
void DropLink(DsrPassiveBuffEntry en, std::string reason)
Notify that packet is dropped from queue by timeout.
uint32_t m_maxLen
The maximum number of packets that we allow a routing protocol to buffer.
Time m_passiveBufferTimeout
The maximum period of time that a routing protocol is allowed to buffer a packet for,...
void Purge()
Remove all expired entries.
std::vector< DsrPassiveBuffEntry > m_passiveBuffer
The send buffer to cache unsent packet.
void Drop(DsrPassiveBuffEntry en, std::string reason)
Notify that packet is dropped from queue by timeout.
static TypeId GetTypeId()
Get the type ID.
bool AllEqual(DsrPassiveBuffEntry &entry)
Check if all the entries in passive buffer entry is all equal or not.
#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_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:289
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
Every class exported by the ns3 library is enclosed in the ns3 namespace.
IsExpired structure.
bool operator()(DsrErrorBuffEntry const &e) const
comparison operator