A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
dsr-rcache.h
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  * Song Luan <lsuper@mail.ustc.edu.cn> (Implemented Link Cache using dijsktra algorithm to get the best route)
20  *
21  * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director
22  * ResiliNets Research Group http://wiki.ittc.ku.edu/resilinets
23  * Information and Telecommunication Technology Center (ITTC)
24  * and Department of Electrical Engineering and Computer Science
25  * The University of Kansas Lawrence, KS USA.
26  *
27  * Work supported in part by NSF FIND (Future Internet Design) Program
28  * under grant CNS-0626918 (Postmodern Internet Architecture),
29  * NSF grant CNS-1050226 (Multilayer Network Resilience Analysis and Experimentation on GENI),
30  * US Department of Defense (DoD), and ITTC at The University of Kansas.
31  */
32 
33 #ifndef DSR_RCACHE_H
34 #define DSR_RCACHE_H
35 
36 #include <map>
37 #include <stdint.h>
38 #include <cassert>
39 #include <sys/types.h>
40 #include <iostream>
41 #include <vector>
42 
43 #include "ns3/simulator.h"
44 #include "ns3/timer.h"
45 #include "ns3/simple-ref-count.h"
46 #include "ns3/header.h"
47 #include "ns3/enum.h"
48 #include "ns3/ipv4-address.h"
49 #include "ns3/nstime.h"
50 #include "ns3/ipv4.h"
51 #include "ns3/ipv4-route.h"
52 #include "ns3/net-device.h"
53 #include "ns3/ipv4-l3-protocol.h"
54 #include "ns3/callback.h"
55 #include "ns3/wifi-mac-header.h"
56 #include "ns3/arp-cache.h"
57 #include "dsr-option-header.h"
58 
59 namespace ns3 {
60 class Time;
61 namespace dsr {
62 
86 struct Link
87 {
88  Ipv4Address m_low;
89  Ipv4Address m_high;
90  Link (Ipv4Address ip1, Ipv4Address ip2)
91  {
92  if (ip1 < ip2)
93  {
94  m_low = ip1;
95  m_high = ip2;
96  }
97  else
98  {
99  m_low = ip2;
100  m_high = ip1;
101  }
102  }
103  bool operator < (Link const& L) const
104  {
105  if (m_low < L.m_low)
106  {
107  return true;
108  }
109  else if (m_low == L.m_low)
110  {
111  return (m_high < L.m_high);
112  }
113  else
114  {
115  return false;
116  }
117  }
118  void Print () const;
119 };
120 
121 class LinkStab
122 {
123 public:
127  LinkStab (Time linkStab = Simulator::Now ());
131  virtual ~LinkStab ();
132 
136  void SetLinkStability (Time linkStab)
137  {
138  m_linkStability = linkStab + Simulator::Now ();
139  }
140  Time GetLinkStability () const
141  {
142  return m_linkStability - Simulator::Now ();
143  }
144 
145  void Print () const;
146 
147 private:
153 };
154 
155 class NodeStab
156 {
157 public:
161 // NodeStab ();
162  NodeStab (Time nodeStab = Simulator::Now ());
166  virtual ~NodeStab ();
167 
168  void SetNodeStability (Time nodeStab)
169  {
170  m_nodeStability = nodeStab + Simulator::Now ();
171  }
172  Time GetNodeStability () const
173  {
174  return m_nodeStability - Simulator::Now ();
175  }
176 private:
177  Time m_nodeStability;
178 };
179 
181 {
182 public:
183  typedef std::vector<Ipv4Address> IP_VECTOR;
184  typedef std::vector<Ipv4Address>::iterator Iterator;
185  // / c-tor
189  RouteCacheEntry (IP_VECTOR const & ip = IP_VECTOR (), Ipv4Address dst = Ipv4Address (), Time exp = Simulator::Now ());
193  virtual ~RouteCacheEntry ();
194  // / Mark entry as "down" (i.e. disable it)
195  void Invalidate (Time badLinkLifetime);
196  // /\name Fields
197  // \{
198  void SetUnidirectional (bool u)
199  {
200  m_blackListState = u;
201  }
202  bool IsUnidirectional () const
203  {
204  return m_blackListState;
205  }
206  void SetBlacklistTimeout (Time t)
207  {
208  m_blackListTimeout = t;
209  }
210  Time GetBlacklistTimeout () const
211  {
212  return m_blackListTimeout;
213  }
214  Ipv4Address GetDestination () const
215  {
216  return m_dst;
217  }
218  void SetDestination (Ipv4Address d)
219  {
220  m_dst = d;
221  }
222  IP_VECTOR GetVector () const
223  {
224  return m_path;
225  }
226  void SetVector (IP_VECTOR v)
227  {
228  m_path = v;
229  }
230  void SetExpireTime (Time exp)
231  {
232  m_expire = exp + Simulator::Now ();
233  }
234  Time GetExpireTime () const
235  {
236  return m_expire - Simulator::Now ();
237  }
238  // \}
242  void Print (std::ostream & os) const;
247  bool operator== (RouteCacheEntry const & o) const
248  {
249  if (m_path.size () != o.m_path.size ())
250  {
251  NS_ASSERT (false);
252  return false;
253  }
254  IP_VECTOR::const_iterator j = o.m_path.begin ();
255  for (IP_VECTOR::const_iterator i = m_path.begin (); i
256  != m_path.end (); i++, j++)
257  {
258  /*
259  * Verify if neither the entry are not 0 and they equal to each other
260  */
261  if (((*i) == 0) || ((*j) == 0))
262  {
263  return false;
264  }
265  else if (!((*i) == (*j)) )
266  {
267  return false;
268  }
269  else
270  {
271  return true;
272  }
273  }
274  return false;
275  }
276  // \}
277 
278 private:
279 
281 
283 
285 
287 
289 
290  uint8_t m_reqCount;
291 
293 
295 
297 
299 };
305 class RouteCache : public Object
306 {
307 public:
308  // / Default c-tor
313  static TypeId GetTypeId ();
317  RouteCache ();
321  virtual ~RouteCache ();
325  void RemoveLastEntry (std::list<RouteCacheEntry> & rtVector);
329  typedef std::list<RouteCacheEntry::IP_VECTOR> routeVector;
333  Ipv4Address GetDestination (void) const;
337  void DropPathWithDst (Ipv4Address dst);
341  bool IsEqual (RouteCacheEntry ca);
342  // /\name Fields
343  // \{
344  bool GetSubRoute () const
345  {
346  return m_subRoute;
347  }
348  void SetSubRoute (bool subRoute)
349  {
350  m_subRoute = subRoute;
351  }
352  uint32_t GetMaxCacheLen () const
353  {
354  return m_maxCacheLen;
355  }
356  void SetMaxCacheLen (uint32_t len)
357  {
358  m_maxCacheLen = len;
359  }
360  Time GetCacheTimeout () const
361  {
362  return RouteCacheTimeout;
363  }
364  void SetCacheTimeout (Time t)
365  {
366  RouteCacheTimeout = t;
367  }
368  uint32_t GetMaxEntriesEachDst () const
369  {
370  return m_maxEntriesEachDst;
371  }
372  void SetMaxEntriesEachDst (uint32_t entries)
373  {
374  m_maxEntriesEachDst = entries;
375  }
376  Time GetBadLinkLifetime () const
377  {
378  return m_badLinkLifetime;
379  }
380  void SetBadLinkLifetime (Time t)
381  {
382  m_badLinkLifetime = t;
383  }
384  uint64_t GetStabilityDecrFactor () const
385  {
386  return m_stabilityDecrFactor;
387  }
388  void SetStabilityDecrFactor (uint64_t decrFactor)
389  {
390  m_stabilityDecrFactor = decrFactor;
391  }
392  uint64_t GetStabilityIncrFactor () const
393  {
394  return m_stabilityIncrFactor;
395  }
396  void SetStabilityIncrFactor (uint64_t incrFactor)
397  {
398  m_stabilityIncrFactor = incrFactor;
399  }
400  Time GetInitStability () const
401  {
402  return m_initStability;
403  }
404  void SetInitStability (Time initStability)
405  {
406  m_initStability = initStability;
407  }
408  Time GetMinLifeTime () const
409  {
410  return m_minLifeTime;
411  }
412  void SetMinLifeTime (Time minLifeTime)
413  {
414  m_minLifeTime = minLifeTime;
415  }
416  Time GetUseExtends () const
417  {
418  return m_useExtends;
419  }
420  void SetUseExtends (Time useExtends)
421  {
422  m_useExtends = useExtends;
423  }
424  // \}
431  bool UpdateRouteEntry (Ipv4Address dst);
437  bool AddRoute (RouteCacheEntry & rt);
444  bool LookupRoute (Ipv4Address id, RouteCacheEntry & rt);
449  void PrintVector (std::vector<Ipv4Address>& vec);
454  void PrintRouteVector (std::list<RouteCacheEntry> route);
460  bool FindSameRoute (RouteCacheEntry & rt, std::list<RouteCacheEntry> & rtVector);
465  bool DeleteRoute (Ipv4Address dst);
474  void DeleteAllRoutesIncludeLink (Ipv4Address errorSrc, Ipv4Address unreachNode, Ipv4Address node);
475  // / Delete all entries from routing table
476  void Clear ()
477  {
479  }
480  // / Delete all outdated entries and invalidate valid entry if Lifetime is expired
481  void Purge ();
482  // / Print route cache
483  void Print (std::ostream &os);
484 
485  //------------------------------------------------------------------------------------------
489  uint16_t CheckUniqueAckId (Ipv4Address nextHop);
493  uint16_t GetAckSize ();
494 
495  // --------------------------------------------------------------------------------------------
499  // / Neighbor description
500  struct Neighbor
501  {
502  Ipv4Address m_neighborAddress;
503  Mac48Address m_hardwareAddress;
504  Time m_expireTime;
505  bool close;
506 
508  : m_neighborAddress (ip),
509  m_hardwareAddress (mac),
510  m_expireTime (t),
511  close (false)
512  {
513  }
514 
515  Neighbor () {} // For Python bindings
516  };
524  bool IsNeighbor (Ipv4Address addr);
528  void UpdateNeighbor (std::vector<Ipv4Address> nodeList, Time expire);
532  void AddNeighbor (std::vector<Ipv4Address> nodeList, Ipv4Address ownAddress, Time expire);
536  void PurgeMac ();
540  void ScheduleTimer ();
544  void ClearMac ()
545  {
546  m_nb.clear ();
547  }
551  void AddArpCache (Ptr<ArpCache>);
555  void DelArpCache (Ptr<ArpCache>);
561  {
562  return m_txErrorCallback;
563  }
564  // /\name Handle link failure callback
565  // \{
566  void SetCallback (Callback<void, Ipv4Address, uint8_t > cb)
567  {
568  m_handleLinkFailure = cb;
569  }
570  Callback<void, Ipv4Address, uint8_t > GetCallback () const
571  {
572  return m_handleLinkFailure;
573  }
574  // \}
575 
576 private:
577  RouteCache & operator= (RouteCache const &);
579  uint32_t m_maxCacheLen;
582 
586  uint32_t m_stabilityIncrFactor;
587  Time m_initStability;
588  Time m_minLifeTime;
589  Time m_useExtends;
593  typedef std::list<RouteCacheEntry> routeEntryVector;
594 
595  std::map<Ipv4Address, routeEntryVector> m_sortedRoutes;
596 
598 
600 
601  std::map<Ipv4Address, uint16_t> m_ackIdCache;
602 
604 
605  bool m_subRoute;
606 
610  #define MAXWEIGHT 0xFFFF;
611 
616  std::map<Ipv4Address, std::map<Ipv4Address, uint32_t> > m_netGraph;
617 
618  std::map<Ipv4Address, RouteCacheEntry::IP_VECTOR> m_bestRoutesTable_link;
619  std::map<Link, LinkStab> m_linkCache;
620  std::map<Ipv4Address, NodeStab> m_nodeCache;
621 
631  bool IncStability (Ipv4Address node);
636  bool DecStability (Ipv4Address node);
637 
638 public:
644  void SetCacheType (std::string type);
645  bool IsLinkCache ();
651  void RebuildBestRouteTable (Ipv4Address source);
652  void PurgeLinkNode ();
663  void UpdateNetGraph ();
664  //---------------------------------------------------------------------------------------
669 
671 
673 
674  std::vector<Neighbor> m_nb;
675 
676  std::vector<Ptr<ArpCache> > m_arp;
677 
679 
681 
682  void ProcessTxError (WifiMacHeader const &);
683 };
684 } // namespace dsr
685 } // namespace ns3
686 #endif /* DSR_RCACHE_H */
bool DeleteRoute(Ipv4Address dst)
Delete the route with certain destination address.
Definition: dsr-rcache.cc:758
static TypeId GetTypeId()
The Route Cache used by DSR.
Definition: dsr-rcache.cc:136
keep track of time unit.
Definition: nstime.h:149
void PurgeMac()
Remove all expired mac entries.
Definition: dsr-rcache.cc:1167
Callback template class.
Definition: callback.h:369
IP_VECTOR m_path
brief The IP address constructed route
Definition: dsr-rcache.h:284
a simple Timer class
Definition: timer.h:45
bool IncStability(Ipv4Address node)
increase the stability of the node
Definition: dsr-rcache.cc:513
Ipv4Address m_dst
The destination Ip address.
Definition: dsr-rcache.h:282
Ipv4InterfaceAddress m_iface
Output interface address.
Definition: dsr-rcache.h:288
#define NS_ASSERT(condition)
Definition: assert.h:64
Time RouteCacheTimeout
The maximum period of time that dsr is allowed to for an unused route.
Definition: dsr-rcache.h:580
bool AddRoute(RouteCacheEntry &rt)
Add route cache entry if it doesn't yet exist in route cache.
Definition: dsr-rcache.cc:655
void RebuildBestRouteTable(Ipv4Address source)
USE MAXWEIGHT TO REPRESENT MAX; USE BROADCAST ADDRESS TO REPRESENT NULL PRECEEDING ADDRESS...
Definition: dsr-rcache.cc:315
NodeStab(Time nodeStab=Simulator::Now())
Constructor.
Definition: dsr-rcache.cc:80
Ipv4Address GetDestination(void) const
Get the destination address of the route.
void DropPathWithDst(Ipv4Address dst)
Remove all packets with destination IP address dst.
bool LookupRoute_Link(Ipv4Address id, RouteCacheEntry &rt)
used by LookupRoute when LinkCache
Definition: dsr-rcache.cc:431
std::map< Ipv4Address, std::map< Ipv4Address, uint32_t > > m_netGraph
Definition: dsr-rcache.h:616
uint32_t m_stabilityDecrFactor
Definition: dsr-rcache.h:585
std::map< Ipv4Address, uint16_t > m_ackIdCache
The id cache to ensure all the ids are unique.
Definition: dsr-rcache.h:601
Ptr< Ipv4 > m_ipv4
The Ipv4 layer 3.
Definition: dsr-rcache.h:298
bool operator==(RouteCacheEntry const &o) const
Compare the route cache entry.
Definition: dsr-rcache.h:247
void SetCacheType(std::string type)
dijsktra algorithm to get the best route from m_netGraph and update the m_bestRoutesTable_link curre...
Definition: dsr-rcache.cc:289
void UseExtends(RouteCacheEntry::IP_VECTOR rt)
Definition: dsr-rcache.cc:609
RouteCacheEntry::IP_VECTOR m_vector
The route vector to save the ip addresses for intermediate nodes.
Definition: dsr-rcache.h:578
uint16_t CheckUniqueAckId(Ipv4Address nextHop)
Check for duplicate ids and save new entries if the id is not present in the table.
Definition: dsr-rcache.cc:1049
std::list< RouteCacheEntry::IP_VECTOR > routeVector
Define the vector of route entries.
Definition: dsr-rcache.h:329
void AddNeighbor(std::vector< Ipv4Address > nodeList, Ipv4Address ownAddress, Time expire)
Add to the neighbor list.
Definition: dsr-rcache.cc:1141
void ClearMac()
Remove all entries.
Definition: dsr-rcache.h:544
void UpdateNeighbor(std::vector< Ipv4Address > nodeList, Time expire)
Update expire time for entry with address addr, if it exists, else add new entry. ...
Definition: dsr-rcache.cc:1113
void ScheduleTimer()
Schedule m_ntimer.
Definition: dsr-rcache.cc:1193
std::vector< Ipv4Address > IP_VECTOR
Define the vector to hold Ip address.
Definition: dsr-rcache.h:183
bool m_subRoute
Check if save the sub route entries or not.
Definition: dsr-rcache.h:605
bool UpdateRouteEntry(Ipv4Address dst)
Update route cache entry if it has been recently used and successfully delivered the data packet...
Definition: dsr-rcache.cc:176
void DelArpCache(Ptr< ArpCache >)
Don't use given ARP cache any more (interface is down)
Definition: dsr-rcache.cc:1206
RouteCacheEntry(IP_VECTOR const &ip=IP_VECTOR(), Ipv4Address dst=Ipv4Address(), Time exp=Simulator::Now())
Constructor.
Definition: dsr-rcache.cc:105
uint32_t m_maxCacheLen
The maximum number of packets that we allow a routing protocol to buffer.
Definition: dsr-rcache.h:579
Definition: dsr-rcache.h:180
void PrintRouteVector(std::list< RouteCacheEntry > route)
Print all the route vector elements from the route list.
Definition: dsr-rcache.cc:951
std::map< Link, LinkStab > m_linkCache
The data structure to store link info.
Definition: dsr-rcache.h:619
virtual ~RouteCacheEntry()
Destructor.
Definition: dsr-rcache.cc:116
Callback< void, WifiMacHeader const & > m_txErrorCallback
TX error callback.
Definition: dsr-rcache.h:670
void ProcessTxError(WifiMacHeader const &)
Process layer 2 TX error notification.
Definition: dsr-rcache.cc:1229
Mac48Address LookupMacAddress(Ipv4Address)
Find MAC address by IP using list of ARP caches.
Definition: dsr-rcache.cc:1212
uint8_t m_reqCount
Number of route requests.
Definition: dsr-rcache.h:290
bool AddRoute_Link(RouteCacheEntry::IP_VECTOR nodelist, Ipv4Address node)
Definition: dsr-rcache.cc:560
bool IsEqual(RouteCacheEntry ca)
To know if the two entries are the same.
std::list< RouteCacheEntry > routeEntryVector
Definition: dsr-rcache.h:593
void RemoveLastEntry(std::list< RouteCacheEntry > &rtVector)
Remove the aged route cache entries when the route cache is full.
Definition: dsr-rcache.cc:168
Ptr< Ipv4Route > m_ipv4Route
The Ipv4 route.
Definition: dsr-rcache.h:296
void PrintVector(std::vector< Ipv4Address > &vec)
Print the route vector elements.
Definition: dsr-rcache.cc:930
void Print(std::ostream &os) const
Print necessary fields.
Definition: dsr-rcache.cc:128
an EUI-48 address
Definition: mac48-address.h:41
DSR route request queue Since DSR is an on demand routing we queue requests while looking for route...
Definition: dsr-rcache.h:305
uint16_t GetAckSize()
Get the ack table size.
Definition: dsr-rcache.cc:1071
static Time Now(void)
Definition: simulator.cc:179
RouteCache()
Constructor.
Definition: dsr-rcache.cc:145
std::vector< Ptr< ArpCache > > m_arp
list of ARP cached to be used for layer 2 notifications processing
Definition: dsr-rcache.h:676
bool LookupRoute(Ipv4Address id, RouteCacheEntry &rt)
Lookup route cache entry with destination address dst.
Definition: dsr-rcache.cc:206
bool m_blackListState
Indicate if this entry is in "blacklist".
Definition: dsr-rcache.h:292
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
uint32_t m_maxEntriesEachDst
number of entries for each destination
Definition: dsr-rcache.h:599
Timer m_ntimer
Timer for neighbor's list. Schedule Purge().
Definition: dsr-rcache.h:672
virtual ~NodeStab()
Destructor.
Definition: dsr-rcache.cc:85
Callback< void, WifiMacHeader const & > GetTxErrorCallback() const
Get callback to ProcessTxError, this callback is trying to use the wifi mac tx error header to notify...
Definition: dsr-rcache.h:560
std::map< Ipv4Address, RouteCacheEntry::IP_VECTOR > m_bestRoutesTable_link
for link route cache
Definition: dsr-rcache.h:618
a class to store IPv4 address information on an interface
std::map< Ipv4Address, routeEntryVector > m_sortedRoutes
Map the ipv4Address to route entry vector.
Definition: dsr-rcache.h:595
Time m_expire
Expire time for queue entry.
Definition: dsr-rcache.h:286
Time m_badLinkLifetime
The time for which the neighboring node is put into the blacklist.
Definition: dsr-rcache.h:581
Timer m_ackTimer
RREP_ACK timer.
Definition: dsr-rcache.h:280
void UpdateNetGraph()
Update the Net Graph for the link and node cache has changed.
Definition: dsr-rcache.cc:498
Callback< void, Ipv4Address, uint8_t > m_handleLinkFailure
link failure callback
Definition: dsr-rcache.h:668
Time m_blackListTimeout
Time for which the node is put into the blacklist.
Definition: dsr-rcache.h:294
bool FindSameRoute(RouteCacheEntry &rt, std::list< RouteCacheEntry > &rtVector)
Find the same route in the route cache.
Definition: dsr-rcache.cc:725
a base class which provides memory management and object aggregation
Definition: object.h:63
void DeleteAllRoutesIncludeLink(Ipv4Address errorSrc, Ipv4Address unreachNode, Ipv4Address node)
Delete all the routes which includes the link from next hop address that has just been notified as un...
Definition: dsr-rcache.cc:772
bool DecStability(Ipv4Address node)
decrease the stability of the node
Definition: dsr-rcache.cc:537
std::vector< Neighbor > m_nb
vector of entries
Definition: dsr-rcache.h:674
routeEntryVector m_routeEntryVector
Define the route vector.
Definition: dsr-rcache.h:597
bool m_isLinkCache
Check if the route is using path cache or link cache.
Definition: dsr-rcache.h:603
a unique identifier for an interface.
Definition: type-id.h:44
bool IsNeighbor(Ipv4Address addr)
Check that node with address addr is neighbor.
Definition: dsr-rcache.cc:1081
Time GetExpireTime(Ipv4Address addr)
Return expire time for neighbor node with address addr, if exists, else return 0. ...
Definition: dsr-rcache.cc:1097
Time m_delay
This timeout deals with the passive ack.
Definition: dsr-rcache.h:678
void AddArpCache(Ptr< ArpCache >)
Add ARP cache to be used to allow layer 2 notifications processing.
Definition: dsr-rcache.cc:1200
std::vector< Ipv4Address >::iterator Iterator
Define the iterator.
Definition: dsr-rcache.h:184
std::map< Ipv4Address, NodeStab > m_nodeCache
The data structure to store node info.
Definition: dsr-rcache.h:620
virtual ~RouteCache()
Destructor.
Definition: dsr-rcache.cc:160