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;
330  // /\name Fields
331  // \{
332  bool GetSubRoute () const
333  {
334  return m_subRoute;
335  }
336  void SetSubRoute (bool subRoute)
337  {
338  m_subRoute = subRoute;
339  }
340  uint32_t GetMaxCacheLen () const
341  {
342  return m_maxCacheLen;
343  }
344  void SetMaxCacheLen (uint32_t len)
345  {
346  m_maxCacheLen = len;
347  }
349  {
350  return RouteCacheTimeout;
351  }
353  {
354  RouteCacheTimeout = t;
355  }
356  uint32_t GetMaxEntriesEachDst () const
357  {
358  return m_maxEntriesEachDst;
359  }
360  void SetMaxEntriesEachDst (uint32_t entries)
361  {
362  m_maxEntriesEachDst = entries;
363  }
365  {
366  return m_badLinkLifetime;
367  }
369  {
370  m_badLinkLifetime = t;
371  }
372  uint64_t GetStabilityDecrFactor () const
373  {
374  return m_stabilityDecrFactor;
375  }
376  void SetStabilityDecrFactor (uint64_t decrFactor)
377  {
378  m_stabilityDecrFactor = decrFactor;
379  }
380  uint64_t GetStabilityIncrFactor () const
381  {
382  return m_stabilityIncrFactor;
383  }
384  void SetStabilityIncrFactor (uint64_t incrFactor)
385  {
386  m_stabilityIncrFactor = incrFactor;
387  }
389  {
390  return m_initStability;
391  }
392  void SetInitStability (Time initStability)
393  {
394  m_initStability = initStability;
395  }
397  {
398  return m_minLifeTime;
399  }
400  void SetMinLifeTime (Time minLifeTime)
401  {
402  m_minLifeTime = minLifeTime;
403  }
405  {
406  return m_useExtends;
407  }
408  void SetUseExtends (Time useExtends)
409  {
410  m_useExtends = useExtends;
411  }
412  // \}
419  bool UpdateRouteEntry (Ipv4Address dst);
425  bool AddRoute (RouteCacheEntry & rt);
432  bool LookupRoute (Ipv4Address id, RouteCacheEntry & rt);
437  void PrintVector (std::vector<Ipv4Address>& vec);
442  void PrintRouteVector (std::list<RouteCacheEntry> route);
448  bool FindSameRoute (RouteCacheEntry & rt, std::list<RouteCacheEntry> & rtVector);
453  bool DeleteRoute (Ipv4Address dst);
462  void DeleteAllRoutesIncludeLink (Ipv4Address errorSrc, Ipv4Address unreachNode, Ipv4Address node);
463  // / Delete all entries from routing table
464  void Clear ()
465  {
467  }
468  // / Delete all outdated entries and invalidate valid entry if Lifetime is expired
469  void Purge ();
470  // / Print route cache
471  void Print (std::ostream &os);
472 
473  //------------------------------------------------------------------------------------------
477  uint16_t CheckUniqueAckId (Ipv4Address nextHop);
481  uint16_t GetAckSize ();
482 
483  // --------------------------------------------------------------------------------------------
487  // / Neighbor description
488  struct Neighbor
489  {
493  bool close;
494 
496  : m_neighborAddress (ip),
497  m_hardwareAddress (mac),
498  m_expireTime (t),
499  close (false)
500  {
501  }
502 
503  Neighbor () {} // For Python bindings
504  };
512  bool IsNeighbor (Ipv4Address addr);
516  void UpdateNeighbor (std::vector<Ipv4Address> nodeList, Time expire);
520  void AddNeighbor (std::vector<Ipv4Address> nodeList, Ipv4Address ownAddress, Time expire);
524  void PurgeMac ();
528  void ScheduleTimer ();
532  void ClearMac ()
533  {
534  m_nb.clear ();
535  }
539  void AddArpCache (Ptr<ArpCache>);
543  void DelArpCache (Ptr<ArpCache>);
549  {
550  return m_txErrorCallback;
551  }
552  // /\name Handle link failure callback
553  // \{
555  {
556  m_handleLinkFailure = cb;
557  }
559  {
560  return m_handleLinkFailure;
561  }
562  // \}
563 
564 private:
565  RouteCache & operator= (RouteCache const &);
567  uint32_t m_maxCacheLen;
570 
581  typedef std::list<RouteCacheEntry> routeEntryVector;
582 
583  std::map<Ipv4Address, routeEntryVector> m_sortedRoutes;
584 
586 
588 
589  std::map<Ipv4Address, uint16_t> m_ackIdCache;
590 
592 
593  bool m_subRoute;
594 
598  #define MAXWEIGHT 0xFFFF;
599 
604  std::map<Ipv4Address, std::map<Ipv4Address, uint32_t> > m_netGraph;
605 
606  std::map<Ipv4Address, RouteCacheEntry::IP_VECTOR> m_bestRoutesTable_link;
607  std::map<Link, LinkStab> m_linkCache;
608  std::map<Ipv4Address, NodeStab> m_nodeCache;
609 
619  bool IncStability (Ipv4Address node);
624  bool DecStability (Ipv4Address node);
625 
626 public:
632  void SetCacheType (std::string type);
633  bool IsLinkCache ();
639  void RebuildBestRouteTable (Ipv4Address source);
640  void PurgeLinkNode ();
651  void UpdateNetGraph ();
652  //---------------------------------------------------------------------------------------
657 
659 
661 
662  std::vector<Neighbor> m_nb;
663 
664  std::vector<Ptr<ArpCache> > m_arp;
665 
667 
669 
670  void ProcessTxError (WifiMacHeader const &);
671 };
672 } // namespace dsr
673 } // namespace ns3
674 #endif /* DSR_RCACHE_H */