A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
dsr-rcache.h
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  * 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 
63 /*
64  * The route cache structure
65  \verbatim
66  +-+-+-+-+-+-+-+-+-+-+-+- +-+-+-+-+-+-+-+-+-+-+- +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
67  | Destination Address |---------| Route Cache Entry | ---------- | IP_VECTOR | dst | exp time |
68  +-+-+-+-+-+-+-+-+-+-+-+- Map +-+-+-+-+-+-+-+-+-+-+- Contains +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
69  +-+-+-+-+-+-+-+-+-+-+- +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
70  | Route Cache Entry | ---------- | IP_VECTOR | dst | exp time |
71  +-+-+-+-+-+-+-+-+-+-+- Contains +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
72  . .
73  . .
74  . .
75  . .
76  +-+-+-+-+-+-+-+-+-+-+- +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
77  | Route Cache Entry | ---------- | IP_VECTOR | dst | exp time |
78  +-+-+-+-+-+-+-+-+-+-+- Contains +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
79 
80  \endverbatim
81  */
86 struct Link
87 {
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 
133  void SetLinkStability (Time linkStab)
134  {
135  m_linkStability = linkStab + Simulator::Now ();
136  }
138  {
139  return m_linkStability - Simulator::Now ();
140  }
141 
142  void Print () const;
143 
144 private:
145  /*
146  * The link stability lifetime expected, when the time is due, the link expires the expiration happens
147  * when purge the node and link cache before update them when receiving new information
148  */
150 };
151 
152 class NodeStab
153 {
154 public:
158 // NodeStab ();
159  NodeStab (Time nodeStab = Simulator::Now ());
163  virtual ~NodeStab ();
164 
165  void SetNodeStability (Time nodeStab)
166  {
167  m_nodeStability = nodeStab + Simulator::Now ();
168  }
170  {
171  return m_nodeStability - Simulator::Now ();
172  }
173 private:
175 };
176 
178 {
179 public:
180  typedef std::vector<Ipv4Address> IP_VECTOR; // Define the vector to hold Ip address
181  typedef std::vector<Ipv4Address>::iterator Iterator; // Define the iterator
182  // / c-tor
186  RouteCacheEntry (IP_VECTOR const & ip = IP_VECTOR (), Ipv4Address dst = Ipv4Address (), Time exp = Simulator::Now ());
190  virtual ~RouteCacheEntry ();
191  // / Mark entry as "down" (i.e. disable it)
192  void Invalidate (Time badLinkLifetime);
193  // /\name Fields
194  // \{
195  void SetUnidirectional (bool u)
196  {
197  m_blackListState = u;
198  }
199  bool IsUnidirectional () const
200  {
201  return m_blackListState;
202  }
204  {
205  m_blackListTimeout = t;
206  }
208  {
209  return m_blackListTimeout;
210  }
212  {
213  return m_dst;
214  }
216  {
217  m_dst = d;
218  }
220  {
221  return m_path;
222  }
224  {
225  m_path = v;
226  }
227  void SetExpireTime (Time exp)
228  {
229  m_expire = exp + Simulator::Now ();
230  }
232  {
233  return m_expire - Simulator::Now ();
234  }
235  // \}
239  void Print (std::ostream & os) const;
244  bool operator== (RouteCacheEntry const & o) const
245  {
246  if (m_path.size () != o.m_path.size ())
247  {
248  NS_ASSERT (false);
249  return false;
250  }
251  IP_VECTOR::const_iterator j = o.m_path.begin ();
252  for (IP_VECTOR::const_iterator i = m_path.begin (); i
253  != m_path.end (); i++, j++)
254  {
255  /*
256  * Verify if neither the entry are not 0 and they equal to each other
257  */
258  if (((*i) == 0) || ((*j) == 0))
259  {
260  return false;
261  }
262  else if (!((*i) == (*j)) )
263  {
264  return false;
265  }
266  else
267  {
268  return true;
269  }
270  }
271  return false;
272  }
273  // \}
274 
275 private:
276  // / RREP_ACK timer
278  // / The destination Ip address
280  // / brief The IP address constructed route
282  // / Expire time for queue entry
284  // / Output interface address
286  // / Number of route requests
287  uint8_t m_reqCount;
288  // / Indicate if this entry is in "blacklist"
290  // / Time for which the node is put into the blacklist
292  // / The Ipv4 route
294  // / The Ipv4 layer 3
296 };
302 class RouteCache : public Object
303 {
304 public:
305  // / Default c-tor
310  static TypeId GetTypeId ();
314  RouteCache ();
318  virtual ~RouteCache ();
319  // / Remove the aged route cache entries when the route cache is full
320  void RemoveLastEntry (std::list<RouteCacheEntry> & rtVector);
321  // / Define the vector of route entries.
322  typedef std::list<RouteCacheEntry::IP_VECTOR> routeVector;
323  // / Get the destination address of the route.
324  Ipv4Address GetDestination (void) const;
325  // / Remove all packets with destination IP address dst
326  void DropPathWithDst (Ipv4Address dst);
327  // / To know if the two entries are the same
328  bool IsEqual (RouteCacheEntry ca);
329  // /\name Fields
330  // \{
331  bool GetSubRoute () const
332  {
333  return m_subRoute;
334  }
335  void SetSubRoute (bool subRoute)
336  {
337  m_subRoute = subRoute;
338  }
339  uint32_t GetMaxCacheLen () const
340  {
341  return m_maxCacheLen;
342  }
343  void SetMaxCacheLen (uint32_t len)
344  {
345  m_maxCacheLen = len;
346  }
348  {
349  return RouteCacheTimeout;
350  }
352  {
353  RouteCacheTimeout = t;
354  }
355  uint32_t GetMaxEntriesEachDst () const
356  {
357  return m_maxEntriesEachDst;
358  }
359  void SetMaxEntriesEachDst (uint32_t entries)
360  {
361  m_maxEntriesEachDst = entries;
362  }
364  {
365  return m_badLinkLifetime;
366  }
368  {
369  m_badLinkLifetime = t;
370  }
371  uint64_t GetStabilityDecrFactor () const
372  {
373  return m_stabilityDecrFactor;
374  }
375  void SetStabilityDecrFactor (uint64_t decrFactor)
376  {
377  m_stabilityDecrFactor = decrFactor;
378  }
379  uint64_t GetStabilityIncrFactor () const
380  {
381  return m_stabilityIncrFactor;
382  }
383  void SetStabilityIncrFactor (uint64_t incrFactor)
384  {
385  m_stabilityIncrFactor = incrFactor;
386  }
388  {
389  return m_initStability;
390  }
391  void SetInitStability (Time initStability)
392  {
393  m_initStability = initStability;
394  }
396  {
397  return m_minLifeTime;
398  }
399  void SetMinLifeTime (Time minLifeTime)
400  {
401  m_minLifeTime = minLifeTime;
402  }
404  {
405  return m_useExtends;
406  }
407  void SetUseExtends (Time useExtends)
408  {
409  m_useExtends = useExtends;
410  }
411  // \}
418  bool UpdateRouteEntry (Ipv4Address dst);
424  bool AddRoute (RouteCacheEntry & rt);
431  bool LookupRoute (Ipv4Address id, RouteCacheEntry & rt);
436  void PrintVector (std::vector<Ipv4Address>& vec);
441  void PrintRouteVector (std::list<RouteCacheEntry> route);
447  bool FindSameRoute (RouteCacheEntry & rt, std::list<RouteCacheEntry> & rtVector);
452  bool DeleteRoute (Ipv4Address dst);
453  /*
454  * Delete all the routes which includes the link from next hop address that has just been notified
455  * as unreachable
456  */
457  void DeleteAllRoutesIncludeLink (Ipv4Address errorSrc, Ipv4Address unreachNode, Ipv4Address node);
458  // / Delete all entries from routing table
459  void Clear ()
460  {
462  }
463  // / Delete all outdated entries and invalidate valid entry if Lifetime is expired
464  void Purge ();
465  // / Print route cache
466  void Print (std::ostream &os);
467 
468  //------------------------------------------------------------------------------------------
469  /*
470  * The following code deals with duplicate ack ids
471  */
472  // / Check for duplicate ids and save new entries if the id is not present in the table
473  uint16_t CheckUniqueAckId (Ipv4Address nextHop);
474  // / Get the ack table size
475  uint16_t GetAckSize ();
476 
477  // --------------------------------------------------------------------------------------------
478  /*
479  * The following code handles link-layer acks
480  */
481  // / Neighbor description
482  struct Neighbor
483  {
487  bool close;
488 
490  : m_neighborAddress (ip),
491  m_hardwareAddress (mac),
492  m_expireTime (t),
493  close (false)
494  {
495  }
496 
497  Neighbor () {} // For Python bindings
498  };
499  // / Return expire time for neighbor node with address addr, if exists, else return 0.
501  // / Check that node with address addr is neighbor
502  bool IsNeighbor (Ipv4Address addr);
503  // / Update expire time for entry with address addr, if it exists, else add new entry
504  void UpdateNeighbor (std::vector<Ipv4Address> nodeList, Time expire);
505  // / Add to the neighbor list
506  void AddNeighbor (std::vector<Ipv4Address> nodeList, Ipv4Address ownAddress, Time expire);
507  // / Remove all expired mac entries
508  void PurgeMac ();
509  // / Schedule m_ntimer.
510  void ScheduleTimer ();
511  // / Remove all entries
512  void ClearMac ()
513  {
514  m_nb.clear ();
515  }
516  // / Add ARP cache to be used to allow layer 2 notifications processing
517  void AddArpCache (Ptr<ArpCache>);
518  // / Don't use given ARP cache any more (interface is down)
519  void DelArpCache (Ptr<ArpCache>);
520  // / Get callback to ProcessTxError
521  /*
522  * This callback is trying to use the wifi mac tx error header to notify a link layer drop event, however,
523  * it is not fully supported yet
524  */
526  {
527  return m_txErrorCallback;
528  }
529  // /\name Handle link failure callback
530  // \{
532  {
533  m_handleLinkFailure = cb;
534  }
536  {
537  return m_handleLinkFailure;
538  }
539  // \}
540 
541 private:
542  RouteCache & operator= (RouteCache const &);
543  RouteCacheEntry::IP_VECTOR m_vector; // /< The route vector to save the ip addresses for intermediate nodes.
544  uint32_t m_maxCacheLen; // /< The maximum number of packets that we allow a routing protocol to buffer.
545  Time RouteCacheTimeout; // /< The maximum period of time that dsr is allowed to for an unused route.
546  Time m_badLinkLifetime; // /< The time for which the neighboring node is put into the blacklist.
547  /*
548  * Define the parameters for link cache type
549  */
555  /*
556  * Define the route cache data structure
557  */
558  typedef std::list<RouteCacheEntry> routeEntryVector;
559  // / Map the ipv4Address to route entry vector
560  std::map<Ipv4Address, routeEntryVector> m_sortedRoutes;
561  // Define the route vector
563  // / number of entries for each destination
565  // / The id cache to ensure all the ids are unique
566  std::map<Ipv4Address, uint16_t> m_ackIdCache;
567  // / Check if the route is using path cache or link cache
569  // / Check if save the sub route entries or not
575  #define MAXWEIGHT 0xFFFF;
576 
581  std::map<Ipv4Address, std::map<Ipv4Address, uint32_t> > m_netGraph;
582  // for link route cache
583  std::map<Ipv4Address, RouteCacheEntry::IP_VECTOR> m_bestRoutesTable_link;
584  std::map<Link, LinkStab> m_linkCache;
585  std::map<Ipv4Address, NodeStab> m_nodeCache;
586  // used by LookupRoute when LinkCache
588 
589  bool IncStability (Ipv4Address node);
590 
591  bool DecStability (Ipv4Address node);
592 
593 public:
598  void SetCacheType (std::string type);
599  bool IsLinkCache ();
604  void RebuildBestRouteTable (Ipv4Address source);
605  void PurgeLinkNode ();
616  void UpdateNetGraph ();
617  //---------------------------------------------------------------------------------------
621  // / link failure callback
623  // / TX error callback
625  // / Timer for neighbor's list. Schedule Purge().
627  // / vector of entries
628  std::vector<Neighbor> m_nb;
629  // / list of ARP cached to be used for layer 2 notifications processing
630  std::vector<Ptr<ArpCache> > m_arp;
631  // This timeout deals with the passive ack
633  // / Find MAC address by IP using list of ARP caches
635  // / Process layer 2 TX error notification
636  void ProcessTxError (WifiMacHeader const &);
637 };
638 } // namespace dsr
639 } // namespace ns3
640 #endif /* DSR_RCACHE_H */