8751
|
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 |
|
|
44 |
TypeId RreqTable::GetTypeId ()
|
|
45 |
{
|
|
46 |
static TypeId tid = TypeId ("ns3::dsr::RreqTable")
|
|
47 |
.SetParent<Object> ()
|
|
48 |
.AddConstructor<RreqTable> ()
|
|
49 |
;
|
|
50 |
return tid;
|
|
51 |
}
|
|
52 |
|
|
53 |
RreqTable::RreqTable ()
|
|
54 |
: m_linkStates (PROBABLE)
|
|
55 |
{
|
|
56 |
}
|
|
57 |
|
|
58 |
RreqTable::~RreqTable ()
|
|
59 |
{
|
|
60 |
NS_LOG_FUNCTION_NOARGS ();
|
|
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_LOGIC ("Find and update the route request entry for " << dst);
|
|
87 |
std::map<Ipv4Address, RreqTableEntry >::const_iterator i =
|
|
88 |
m_rreqDstMap.find (dst);
|
|
89 |
if (i == m_rreqDstMap.end ())
|
|
90 |
{
|
|
91 |
NS_LOG_DEBUG ("The request table entry 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_DEBUG ("The request table size after erase ");
|
|
99 |
}
|
|
100 |
RreqTableEntry rreqTableEntry;
|
|
101 |
rreqTableEntry.m_reqNo = 0;
|
|
102 |
rreqTableEntry.m_expire = Simulator::Now ();
|
|
103 |
m_rreqDstMap [dst] = rreqTableEntry;
|
|
104 |
}
|
|
105 |
else
|
|
106 |
{
|
|
107 |
NS_LOG_DEBUG ("Find the request table entry, increment the request count");
|
|
108 |
Ipv4Address dst = i->first;
|
|
109 |
RreqTableEntry rreqTableEntry = i->second;
|
|
110 |
NS_LOG_DEBUG ("The request count before incrementing " << rreqTableEntry.m_reqNo);
|
|
111 |
rreqTableEntry.m_reqNo = (rreqTableEntry.m_reqNo + 1);
|
|
112 |
rreqTableEntry.m_expire = Simulator::Now ();
|
|
113 |
m_rreqDstMap [dst] = rreqTableEntry;
|
|
114 |
}
|
|
115 |
}
|
|
116 |
|
|
117 |
void
|
|
118 |
RreqTable::RemoveRreqEntry (Ipv4Address dst)
|
|
119 |
{
|
|
120 |
NS_LOG_DEBUG ("Remove rreq entry with index dst");
|
|
121 |
std::map<Ipv4Address, RreqTableEntry >::const_iterator i =
|
|
122 |
m_rreqDstMap.find (dst);
|
|
123 |
if (i == m_rreqDstMap.end ())
|
|
124 |
{
|
|
125 |
NS_LOG_DEBUG ("The request table entry not found");
|
|
126 |
}
|
|
127 |
else
|
|
128 |
{
|
|
129 |
// erase the request entry
|
|
130 |
m_rreqDstMap.erase (dst);
|
|
131 |
}
|
|
132 |
}
|
|
133 |
|
|
134 |
uint16_t
|
|
135 |
RreqTable::GetRreqCnt (Ipv4Address dst)
|
|
136 |
{
|
|
137 |
NS_LOG_DEBUG ("Get the request count for a certain dst");
|
|
138 |
std::map<Ipv4Address, RreqTableEntry >::const_iterator i =
|
|
139 |
m_rreqDstMap.find (dst);
|
|
140 |
if (i == m_rreqDstMap.end ())
|
|
141 |
{
|
|
142 |
NS_LOG_DEBUG ("The request table entry not found");
|
|
143 |
FindAndUpdate (dst);
|
|
144 |
return 0;
|
|
145 |
}
|
|
146 |
else
|
|
147 |
{
|
|
148 |
RreqTableEntry rreqTableEntry = i->second;
|
|
149 |
NS_LOG_DEBUG ("Find the request count for " << dst << " " << rreqTableEntry.m_reqNo);
|
|
150 |
return rreqTableEntry.m_reqNo;
|
|
151 |
}
|
|
152 |
}
|
|
153 |
|
|
154 |
// ----------------------------------------------------------------------------------------------------------
|
|
155 |
/**
|
|
156 |
* This part takes care of the route request from a specific source
|
|
157 |
* need to ignore future route requests from same source for same destination with same identification
|
|
158 |
*/
|
|
159 |
bool
|
|
160 |
RreqTable::FindSrc (Ipv4Address source, Ipv4Address target, uint16_t id)
|
|
161 |
{
|
|
162 |
Purge ();
|
|
163 |
std::map<Ipv4Address, std::list<SourceRreqEntry> >::const_iterator i =
|
|
164 |
m_rreqMap.find (source);
|
|
165 |
if (i == m_rreqMap.end ())
|
|
166 |
{
|
|
167 |
NS_LOG_LOGIC ("No Request entry for " << source << " found");
|
|
168 |
SourceRreqEntry sourceRreqEntry;
|
|
169 |
sourceRreqEntry.m_dst = target;
|
|
170 |
sourceRreqEntry.m_identification = id;
|
|
171 |
sourceRreqEntry.m_expire = m_rreqEntryExpire + Simulator::Now ();
|
|
172 |
NS_LOG_DEBUG ("The src rreq expire time " << sourceRreqEntry.m_expire);
|
|
173 |
std::list<SourceRreqEntry> rqVector;
|
|
174 |
rqVector.push_back (sourceRreqEntry);
|
|
175 |
m_rreqMap[source] = rqVector;
|
|
176 |
return false;
|
|
177 |
}
|
|
178 |
else
|
|
179 |
{
|
|
180 |
NS_LOG_LOGIC ("Request entry for " << source << " found in the cache");
|
|
181 |
std::list<SourceRreqEntry> rqVector = i->second;
|
|
182 |
for (std::list<SourceRreqEntry>::iterator j = rqVector.begin (); j != rqVector.end (); ++j)
|
|
183 |
{
|
|
184 |
SourceRreqEntry rreqEntry = *j;
|
|
185 |
if ((rreqEntry.m_dst == target) && (rreqEntry.m_identification == id))
|
|
186 |
{
|
|
187 |
NS_LOG_DEBUG ("Found the request entry for source node with address " << source);
|
|
188 |
// j = rqVector.erase (j);
|
|
189 |
// rqVector.push_back(*j);
|
|
190 |
// m_rreqMap[source] = rqVector;
|
|
191 |
return true;
|
|
192 |
}
|
|
193 |
}
|
|
194 |
|
|
195 |
SourceRreqEntry rreqEntry;
|
|
196 |
rreqEntry.m_dst = target;
|
|
197 |
rreqEntry.m_identification = id;
|
|
198 |
rreqEntry.m_expire = m_rreqEntryExpire + Simulator::Now ();
|
|
199 |
if (rqVector.size () >= m_requestIdSize)
|
|
200 |
{
|
|
201 |
// erase the first element when the size is larger than the request id size (default: 16)
|
|
202 |
rqVector.pop_front ();
|
|
203 |
}
|
|
204 |
// May need to check the size of the entry
|
|
205 |
rqVector.push_back (rreqEntry);
|
|
206 |
m_rreqMap[source] = rqVector;
|
|
207 |
return false;
|
|
208 |
}
|
|
209 |
return false;
|
|
210 |
}
|
|
211 |
|
|
212 |
void
|
|
213 |
RreqTable::Purge ()
|
|
214 |
{
|
|
215 |
//Trying to purge the rreq table
|
|
216 |
if (m_rreqMap.empty ())
|
|
217 |
{
|
|
218 |
NS_LOG_DEBUG ("The rreq table is empty");
|
|
219 |
return;
|
|
220 |
}
|
|
221 |
|
|
222 |
for (std::map<Ipv4Address, std::list<SourceRreqEntry> >::iterator i =
|
|
223 |
m_rreqMap.begin (); i != m_rreqMap.end (); )
|
|
224 |
{
|
|
225 |
// Loop of rreq table entry with the source entries
|
|
226 |
std::map<Ipv4Address, std::list<SourceRreqEntry> >::iterator itmp = i;
|
|
227 |
/*
|
|
228 |
* The rreq table entries
|
|
229 |
*/
|
|
230 |
Ipv4Address dst = i->first;
|
|
231 |
std::list<SourceRreqEntry> rqVector = i->second;
|
|
232 |
NS_LOG_DEBUG ("The rqVector size for " << dst << " is " << rqVector.size ());
|
|
233 |
if (rqVector.size ())
|
|
234 |
{
|
|
235 |
for (std::list<SourceRreqEntry>::iterator j = rqVector.begin (); j != rqVector.end (); )
|
|
236 |
{
|
|
237 |
NS_LOG_DEBUG ("The expire time of every entry with expire time " << j->m_expire - Simulator::Now ());
|
|
238 |
/*
|
|
239 |
* First verify if the rreq table entry has expired or not
|
|
240 |
*/
|
|
241 |
if (j->m_expire - Simulator::Now () <= Seconds (0))
|
|
242 |
{
|
|
243 |
/*
|
|
244 |
* When the expire time has passed, erase the certain rreq table entry
|
|
245 |
*/
|
|
246 |
NS_LOG_DEBUG ("Erase the expired rreq table entry for " << dst << " with expire time " << j->m_expire - Simulator::Now ());
|
|
247 |
j = rqVector.erase (j);
|
|
248 |
}
|
|
249 |
else
|
|
250 |
{
|
|
251 |
++j;
|
|
252 |
}
|
|
253 |
}
|
|
254 |
NS_LOG_DEBUG ("The rreq table entry for " << dst << " " << rqVector.size ());
|
|
255 |
if (rqVector.size ())
|
|
256 |
{
|
|
257 |
++i;
|
|
258 |
m_rreqMap.erase (itmp); // erase the entry first
|
|
259 |
/*
|
|
260 |
* Save the new rreq table entry along with the destination address in map
|
|
261 |
*/
|
|
262 |
std::pair<std::map<Ipv4Address, std::list<SourceRreqEntry> >::iterator, bool> result =
|
|
263 |
m_rreqMap.insert (std::make_pair (dst, rqVector));
|
|
264 |
}
|
|
265 |
else
|
|
266 |
{
|
|
267 |
++i;
|
|
268 |
m_rreqMap.erase (itmp);
|
|
269 |
}
|
|
270 |
}
|
|
271 |
else
|
|
272 |
{
|
|
273 |
++i;
|
|
274 |
m_rreqMap.erase (itmp);
|
|
275 |
}
|
|
276 |
}
|
|
277 |
return;
|
|
278 |
}
|
|
279 |
|
|
280 |
// ----------------------------------------------------------------------------------------------------------
|
|
281 |
/**
|
|
282 |
* This part takes care of the route request ID initialized from a specific source to one destination
|
|
283 |
* Essentially a counter
|
|
284 |
*/
|
|
285 |
uint16_t
|
|
286 |
RreqTable::CheckUniqueRreqId (Ipv4Address dst)
|
|
287 |
{
|
|
288 |
NS_LOG_DEBUG ("The size of id cache " << m_rreqIdCache.size ());
|
|
289 |
std::map<Ipv4Address, uint16_t>::const_iterator i =
|
|
290 |
m_rreqIdCache.find (dst);
|
|
291 |
if (i == m_rreqIdCache.end ())
|
|
292 |
{
|
|
293 |
NS_LOG_LOGIC ("No Request id for " << dst << " found");
|
|
294 |
m_rreqIdCache[dst] = 0;
|
|
295 |
return 0;
|
|
296 |
}
|
|
297 |
else
|
|
298 |
{
|
|
299 |
NS_LOG_LOGIC ("Request id for " << dst << " found in the cache");
|
|
300 |
uint16_t rreqId = m_rreqIdCache[dst];
|
|
301 |
if (rreqId >= m_maxRreqId)
|
|
302 |
{
|
|
303 |
NS_LOG_DEBUG ("The request id increase past the max value, so reset it to 0");
|
|
304 |
rreqId = 0;
|
|
305 |
m_rreqIdCache[dst] = rreqId;
|
|
306 |
}
|
|
307 |
else
|
|
308 |
{
|
|
309 |
rreqId++;
|
|
310 |
m_rreqIdCache[dst] = rreqId;
|
|
311 |
}
|
|
312 |
NS_LOG_DEBUG ("The Request id for " << dst << " is " << rreqId);
|
|
313 |
return rreqId;
|
|
314 |
}
|
|
315 |
}
|
|
316 |
|
|
317 |
uint16_t
|
|
318 |
RreqTable::GetRreqSize ()
|
|
319 |
{
|
|
320 |
return m_rreqIdCache.size ();
|
|
321 |
}
|
|
322 |
|
|
323 |
// ----------------------------------------------------------------------------------------------------------
|
|
324 |
/**
|
|
325 |
* This part takes care of black list which can save unidirectional link information
|
|
326 |
*/
|
|
327 |
|
|
328 |
void
|
|
329 |
RreqTable::Invalidate ()
|
|
330 |
{
|
|
331 |
if (m_linkStates == QUESTIONABLE)
|
|
332 |
{
|
|
333 |
return;
|
|
334 |
}
|
|
335 |
m_linkStates = QUESTIONABLE;
|
|
336 |
}
|
|
337 |
|
|
338 |
BlackList*
|
|
339 |
RreqTable::FindUnidirectional (Ipv4Address neighbor)
|
|
340 |
{
|
|
341 |
PurgeNeighbor (); // purge the neighbor cache
|
|
342 |
for (std::vector<BlackList>::iterator i = m_blackList.begin ();
|
|
343 |
i != m_blackList.end (); ++i)
|
|
344 |
{
|
|
345 |
if (i->m_neighborAddress == neighbor)
|
|
346 |
{
|
|
347 |
return &(*i);
|
|
348 |
}
|
|
349 |
}
|
|
350 |
return NULL;
|
|
351 |
}
|
|
352 |
|
|
353 |
bool
|
|
354 |
RreqTable::MarkLinkAsUnidirectional (Ipv4Address neighbor, Time blacklistTimeout)
|
|
355 |
{
|
|
356 |
NS_LOG_LOGIC ("Add neighbor address in blacklist " << m_blackList.size ());
|
|
357 |
for (std::vector<BlackList>::iterator i = m_blackList.begin (); i != m_blackList.end (); ++i)
|
|
358 |
{
|
|
359 |
if (i->m_neighborAddress == neighbor)
|
|
360 |
{
|
|
361 |
NS_LOG_DEBUG ("Update the blacklist list timeout if found the blacklist entry");
|
|
362 |
i->m_expireTime = std::max (blacklistTimeout + Simulator::Now (), i->m_expireTime);
|
|
363 |
}
|
|
364 |
BlackList blackList (neighbor, blacklistTimeout + Simulator::Now ());
|
|
365 |
m_blackList.push_back (blackList);
|
|
366 |
PurgeNeighbor ();
|
|
367 |
return true;
|
|
368 |
}
|
|
369 |
return false;
|
|
370 |
}
|
|
371 |
|
|
372 |
void
|
|
373 |
RreqTable::PurgeNeighbor ()
|
|
374 |
{
|
|
375 |
/*
|
|
376 |
* Purge the expired blacklist entries
|
|
377 |
*/
|
|
378 |
m_blackList.erase (remove_if (m_blackList.begin (), m_blackList.end (),
|
|
379 |
IsExpired ()), m_blackList.end ());
|
|
380 |
}
|
|
381 |
|
|
382 |
} // namespace dsr
|
|
383 |
} // namespace ns3
|