A Discrete-Event Network Simulator
API
dsr-rreq-table.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-rreq-table.h"
33 #include "ns3/log.h"
34 #include <algorithm>
35 #include <iostream>
36 
37 namespace ns3 {
38 
39 NS_LOG_COMPONENT_DEFINE ("DsrRreqTable");
40 
41 namespace dsr {
42 
43 NS_OBJECT_ENSURE_REGISTERED (DsrRreqTable);
44 
46 {
47  static TypeId tid = TypeId ("ns3::dsr::DsrRreqTable")
48  .SetParent<Object> ()
49  .SetGroupName ("Dsr")
50  .AddConstructor<DsrRreqTable> ()
51  ;
52  return tid;
53 }
54 
56  : m_linkStates (PROBABLE)
57 {
58 }
59 
61 {
63 }
64 
65 void
67 {
68  NS_LOG_FUNCTION (this);
69  Ipv4Address firstExpire;
70  Time max = Seconds (0.0);
71  for (std::map<Ipv4Address, RreqTableEntry >::const_iterator i =
72  m_rreqDstMap.begin (); i != m_rreqDstMap.end (); ++i)
73  {
74  Ipv4Address dst = i->first;
75  RreqTableEntry rreqTableEntry = i->second;
76  if (rreqTableEntry.m_expire > max)
77  {
78  max = rreqTableEntry.m_expire;
79  firstExpire = dst;
80  }
81  }
82  m_rreqDstMap.erase (firstExpire);
83 }
84 
85 void
87 {
88  NS_LOG_FUNCTION (this << dst);
89  std::map<Ipv4Address, RreqTableEntry >::const_iterator i =
90  m_rreqDstMap.find (dst);
91  if (i == m_rreqDstMap.end ())
92  {
93  NS_LOG_LOGIC ("The request table entry for " << dst << " not found");
94  /*
95  * Drop the most aged packet when buffer reaches to max
96  */
97  if (m_rreqDstMap.size () >= m_requestTableSize)
98  {
100  NS_LOG_INFO ("The request table size after erase " << (uint32_t)m_rreqDstMap.size ());
101  }
102  RreqTableEntry rreqTableEntry;
103  rreqTableEntry.m_reqNo = 1;
104  rreqTableEntry.m_expire = Simulator::Now ();
105  m_rreqDstMap [dst] = rreqTableEntry;
106  }
107  else
108  {
109  NS_LOG_LOGIC ("Find the request table entry for " << dst << ", increment the request count");
110  Ipv4Address dst = i->first;
111  RreqTableEntry rreqTableEntry = i->second;
112  rreqTableEntry.m_reqNo = rreqTableEntry.m_reqNo + 1;
113  rreqTableEntry.m_expire = Simulator::Now ();
114  m_rreqDstMap [dst] = rreqTableEntry;
115  }
116 }
117 
118 void
120 {
121  NS_LOG_FUNCTION (this << dst);
122  std::map<Ipv4Address, RreqTableEntry >::const_iterator i =
123  m_rreqDstMap.find (dst);
124  if (i == m_rreqDstMap.end ())
125  {
126  NS_LOG_LOGIC ("The request table entry not found");
127  }
128  else
129  {
130  // erase the request entry
131  m_rreqDstMap.erase (dst);
132  }
133 }
134 
135 uint32_t
137 {
138  NS_LOG_FUNCTION (this << dst);
139  std::map<Ipv4Address, RreqTableEntry >::const_iterator i =
140  m_rreqDstMap.find (dst);
141  if (i == m_rreqDstMap.end ())
142  {
143  NS_LOG_LOGIC ("Request table entry not found");
144  return 0;
145  }
146  else
147  {
148  RreqTableEntry rreqTableEntry = i->second;
149  return rreqTableEntry.m_reqNo;
150  }
151 }
152 
153 // ----------------------------------------------------------------------------------------------------------
154 /*
155  * This part takes care of the route request ID initialized from a specific source to one destination
156  * Essentially a counter
157  */
158 uint32_t
160 {
161  NS_LOG_LOGIC ("The size of id cache " << m_rreqIdCache.size ());
162  std::map<Ipv4Address, uint32_t>::const_iterator i =
163  m_rreqIdCache.find (dst);
164  if (i == m_rreqIdCache.end ())
165  {
166  NS_LOG_LOGIC ("No Request id for " << dst << " found, initialize it to 0");
167  m_rreqIdCache[dst] = 0;
168  return 0;
169  }
170  else
171  {
172  NS_LOG_LOGIC ("Request id for " << dst << " found in the cache");
173  uint32_t rreqId = m_rreqIdCache[dst];
174  if (rreqId >= m_maxRreqId)
175  {
176  NS_LOG_DEBUG ("The request id increase past the max value, " << m_maxRreqId << " so reset it to 0");
177  rreqId = 0;
178  m_rreqIdCache[dst] = rreqId;
179  }
180  else
181  {
182  rreqId++;
183  m_rreqIdCache[dst] = rreqId;
184  }
185  NS_LOG_INFO ("The Request id for " << dst << " is " << rreqId);
186  return rreqId;
187  }
188 }
189 
190 uint32_t
192 {
193  return m_rreqIdCache.size ();
194 }
195 
196 // ----------------------------------------------------------------------------------------------------------
197 /*
198  * This part takes care of black list which can save unidirectional link information
199  */
200 
201 void
203 {
204  if (m_linkStates == QUESTIONABLE)
205  {
206  return;
207  }
209 }
210 
211 BlackList*
213 {
214  PurgeNeighbor (); // purge the neighbor cache
215  for (std::vector<BlackList>::iterator i = m_blackList.begin ();
216  i != m_blackList.end (); ++i)
217  {
218  if (i->m_neighborAddress == neighbor)
219  {
220  return &(*i);
221  }
222  }
223  return NULL;
224 }
225 
226 bool
228 {
229  NS_LOG_LOGIC ("Add neighbor address in blacklist " << m_blackList.size ());
230  for (std::vector<BlackList>::iterator i = m_blackList.begin (); i != m_blackList.end (); i++)
231  {
232  if (i->m_neighborAddress == neighbor)
233  {
234  NS_LOG_DEBUG ("Update the blacklist list timeout if found the blacklist entry");
235  i->m_expireTime = std::max (blacklistTimeout + Simulator::Now (), i->m_expireTime);
236  }
237  BlackList blackList (neighbor, blacklistTimeout + Simulator::Now ());
238  m_blackList.push_back (blackList);
239  PurgeNeighbor ();
240  return true;
241  }
242  return false;
243 }
244 
245 void
247 {
248  /*
249  * Purge the expired blacklist entries
250  */
251  m_blackList.erase (remove_if (m_blackList.begin (), m_blackList.end (),
252  IsExpired ()), m_blackList.end ());
253 }
254 
255 bool
257 {
258  NS_LOG_FUNCTION (this << src << dst << id);
259  DsrReceivedRreqEntry rreqEntry;
260  rreqEntry.SetDestination (dst);
261  rreqEntry.SetIdentification (id);
262  std::list<DsrReceivedRreqEntry> receivedRreqEntryList;
263  /*
264  * this function will return false if the entry is not found, true if duplicate entry find
265  */
266  std::map<Ipv4Address, std::list<DsrReceivedRreqEntry> >::const_iterator i = m_sourceRreqMap.find (src);
267  if (i == m_sourceRreqMap.end ())
268  {
269  NS_LOG_LOGIC ("The source request table entry for " << src << " not found");
270 
271  receivedRreqEntryList.clear ();
272  receivedRreqEntryList.push_back (rreqEntry);
273 
274  m_sourceRreqMap [src] = receivedRreqEntryList;
275  return false;
276  }
277  else
278  {
279  NS_LOG_LOGIC ("Find the request table entry for " << src << ", check if it is exact duplicate");
280  /*
281  * Drop the most aged packet when buffer reaches to max
282  */
283  receivedRreqEntryList = i->second;
284  if (receivedRreqEntryList.size () >= m_requestIdSize)
285  {
286  receivedRreqEntryList.pop_front ();
287  }
288  Ipv4Address src = i->first;
289  // We loop the receive rreq entry to find duplicate
290  for (std::list<DsrReceivedRreqEntry>::const_iterator j = receivedRreqEntryList.begin (); j != receivedRreqEntryList.end (); ++j)
291  {
292  if (*j == rreqEntry)
293  {
294  return true;
295  }
296  }
298  receivedRreqEntryList.push_back (rreqEntry);
299  m_sourceRreqMap [src] = receivedRreqEntryList;
300  return false;
301  }
302 }
303 
304 } // namespace dsr
305 } // namespace ns3
ns3::TypeId
a unique identifier for an interface.
Definition: type-id.h:59
ns3::dsr::PROBABLE
@ PROBABLE
PROBABLE.
Definition: dsr-rreq-table.h:51
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
ns3::dsr::DsrRreqTable::GetRreqCnt
uint32_t GetRreqCnt(Ipv4Address dst)
Get the request count number for one destination address.
Definition: dsr-rreq-table.cc:136
ns3::dsr::DsrReceivedRreqEntry::SetIdentification
void SetIdentification(uint16_t i)
Set identification.
Definition: dsr-rreq-table.h:161
ns3::dsr::DsrRreqTable::FindUnidirectional
BlackList * FindUnidirectional(Ipv4Address neighbor)
Verify if entry is unidirectional or not(e.g.
Definition: dsr-rreq-table.cc:212
NS_OBJECT_ENSURE_REGISTERED
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
ns3::dsr::RreqTableEntry::m_expire
Time m_expire
Expire time.
Definition: dsr-rreq-table.h:80
ns3::dsr::DsrRreqTable::GetTypeId
static TypeId GetTypeId()
Get the type ID.
Definition: dsr-rreq-table.cc:45
ns3::dsr::BlackList
BlackList description.
Definition: dsr-rreq-table.h:56
ns3::Simulator::Now
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:195
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::dsr::DsrRreqTable::m_rreqIdCache
std::map< Ipv4Address, uint32_t > m_rreqIdCache
The id cache to ensure all the ids are unique, it is used when sending out route request.
Definition: dsr-rreq-table.h:371
ns3::dsr::DsrRreqTable::m_blackList
std::vector< BlackList > m_blackList
The Black list.
Definition: dsr-rreq-table.h:378
ns3::dsr::DsrRreqTable::DsrRreqTable
DsrRreqTable()
Definition: dsr-rreq-table.cc:55
ns3::dsr::DsrRreqTable::GetRreqSize
uint32_t GetRreqSize()
Get the request id size.
Definition: dsr-rreq-table.cc:191
ns3::dsr::DsrRreqTable::FindSourceEntry
bool FindSourceEntry(Ipv4Address src, Ipv4Address dst, uint16_t id)
Find the source request entry in the route request queue, return false if not found.
Definition: dsr-rreq-table.cc:256
ns3::Ipv4Address
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:41
dsr-rreq-table.h
ns3::dsr::DsrRreqTable
maintain list of DsrRreqTable entry
Definition: dsr-rreq-table.h:200
ns3::dsr::DsrReceivedRreqEntry
The request entry for intermediate nodes to check if they have received this request or not This is u...
Definition: dsr-rreq-table.h:87
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923
ns3::dsr::DsrRreqTable::Invalidate
void Invalidate()
set the unidirectional entry as QUESTIONABLE state
Definition: dsr-rreq-table.cc:202
ns3::dsr::RreqTableEntry::m_reqNo
uint32_t m_reqNo
Route request number.
Definition: dsr-rreq-table.h:79
max
#define max(a, b)
Definition: 80211b.c:43
ns3::dsr::DsrRreqTable::~DsrRreqTable
virtual ~DsrRreqTable()
Definition: dsr-rreq-table.cc:60
ns3::dsr::DsrRreqTable::m_sourceRreqMap
std::map< Ipv4Address, std::list< DsrReceivedRreqEntry > > m_sourceRreqMap
The cache to ensure all the route request from unique source.
Definition: dsr-rreq-table.h:375
ns3::Object
A base class which provides memory management and object aggregation.
Definition: object.h:88
NS_LOG_INFO
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
ns3::dsr::DsrRreqTable::FindAndUpdate
void FindAndUpdate(Ipv4Address dst)
Find the entry in the route request queue to see if already exists.
Definition: dsr-rreq-table.cc:86
ns3::dsr::RreqTableEntry
The route request table entries.
Definition: dsr-rreq-table.h:78
ns3::dsr::DsrRreqTable::m_linkStates
LinkStates m_linkStates
The state of the unidirectional link.
Definition: dsr-rreq-table.h:367
ns3::Time
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:104
ns3::dsr::DsrRreqTable::IsExpired
Check if the entry is expired or not.
Definition: dsr-rreq-table.h:381
NS_LOG_LOGIC
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:289
ns3::dsr::DsrRreqTable::CheckUniqueRreqId
uint32_t CheckUniqueRreqId(Ipv4Address dst)
The following code generates new request id for each destination.
Definition: dsr-rreq-table.cc:159
ns3::dsr::DsrRreqTable::m_maxRreqId
uint32_t m_maxRreqId
The unique request id for any destination.
Definition: dsr-rreq-table.h:365
NS_LOG_FUNCTION_NOARGS
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Definition: log-macros-enabled.h:209
NS_LOG_DEBUG
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:273
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1289
ns3::dsr::DsrRreqTable::m_rreqDstMap
std::map< Ipv4Address, RreqTableEntry > m_rreqDstMap
The cache to save route request table entries indexed with destination address.
Definition: dsr-rreq-table.h:373
NS_LOG_FUNCTION
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Definition: log-macros-enabled.h:244
ns3::dsr::DsrRreqTable::MarkLinkAsUnidirectional
bool MarkLinkAsUnidirectional(Ipv4Address neighbor, Time blacklistTimeout)
Mark entry as unidirectional (e.g.
Definition: dsr-rreq-table.cc:227
ns3::dsr::DsrReceivedRreqEntry::SetDestination
void SetDestination(Ipv4Address d)
Set IPv4 address of the destination.
Definition: dsr-rreq-table.h:125
ns3::dsr::QUESTIONABLE
@ QUESTIONABLE
QUESTIONABLE.
Definition: dsr-rreq-table.h:52
ns3::dsr::DsrRreqTable::m_requestTableSize
uint32_t m_requestTableSize
The request table size.
Definition: dsr-rreq-table.h:361
ns3::max
double max(double x, double y)
Definition: cobalt-queue-disc.cc:137
ns3::dsr::DsrRreqTable::PurgeNeighbor
void PurgeNeighbor()
Remove all expired black list entries.
Definition: dsr-rreq-table.cc:246
ns3::dsr::DsrRreqTable::RemoveRreqEntry
void RemoveRreqEntry(Ipv4Address dst)
Remove route request entry for dst.
Definition: dsr-rreq-table.cc:119
ns3::dsr::DsrRreqTable::RemoveLeastExpire
void RemoveLeastExpire()
Remove the least used entry.
Definition: dsr-rreq-table.cc:66
ns3::dsr::DsrRreqTable::m_requestIdSize
uint32_t m_requestIdSize
The request source id size.
Definition: dsr-rreq-table.h:363