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