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