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 
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 
136  void SetLinkStability (Time linkStab)
137  {
138  m_linkStability = linkStab + Simulator::Now ();
139  }
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  }
173  {
174  return m_nodeStability - Simulator::Now ();
175  }
176 private:
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  }
207  {
208  m_blackListTimeout = t;
209  }
211  {
212  return m_blackListTimeout;
213  }
215  {
216  return m_dst;
217  }
219  {
220  m_dst = d;
221  }
223  {
224  return m_path;
225  }
227  {
228  m_path = v;
229  }
230  void SetExpireTime (Time exp)
231  {
232  m_expire = exp + Simulator::Now ();
233  }
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  }
361  {
362  return RouteCacheTimeout;
363  }
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  }
377  {
378  return m_badLinkLifetime;
379  }
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  }
401  {
402  return m_initStability;
403  }
404  void SetInitStability (Time initStability)
405  {
406  m_initStability = initStability;
407  }
409  {
410  return m_minLifeTime;
411  }
412  void SetMinLifeTime (Time minLifeTime)
413  {
414  m_minLifeTime = minLifeTime;
415  }
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  {
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  // \{
567  {
568  m_handleLinkFailure = cb;
569  }
571  {
572  return m_handleLinkFailure;
573  }
574  // \}
575 
576 private:
577  RouteCache & operator= (RouteCache const &);
579  uint32_t m_maxCacheLen;
582 
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 */