43 #include "ns3/simulator.h"
44 #include "ns3/ipv4-route.h"
45 #include "ns3/socket.h"
47 #include "ns3/address-utils.h"
48 #include "ns3/packet.h"
106 : m_ackTimer (
Timer::CANCEL_ON_DESTROY),
111 m_blackListState (false),
141 .AddConstructor<RouteCache> ()
148 m_maxEntriesEachDst (3),
149 m_isLinkCache (false),
150 m_ntimer (
Timer::CANCEL_ON_DESTROY),
151 m_delay (MilliSeconds (100))
173 rtVector.pop_back ();
180 std::map<Ipv4Address, std::list<RouteCacheEntry> >::const_iterator i =
184 NS_LOG_LOGIC (
"Failed to find the route entry for the destination " << dst);
189 std::list<RouteCacheEntry> rtVector = i->second;
192 rtVector.pop_front ();
193 rtVector.push_back (successEntry);
199 std::pair<std::map<Ipv4Address, std::list<RouteCacheEntry> >::iterator,
bool> result =
201 return result.second;
219 NS_LOG_LOGIC (
"Route to " <<
id <<
" not found; m_sortedRoutes is empty");
222 std::map<Ipv4Address, std::list<RouteCacheEntry> >::const_iterator i =
m_sortedRoutes.find (
id);
226 for (std::map<
Ipv4Address, std::list<RouteCacheEntry> >::const_iterator j =
229 std::list<RouteCacheEntry> rtVector = j->second;
233 for (std::list<RouteCacheEntry>::const_iterator k = rtVector.begin (); k != rtVector.end (); ++k)
239 for (RouteCacheEntry::IP_VECTOR::iterator l = routeVector.begin (); l != routeVector.end (); ++l)
243 changeVector.push_back (*l);
247 changeVector.push_back (*l);
255 if ((changeVector.size () < routeVector.size ()) && (changeVector.size () > 1))
263 std::list<RouteCacheEntry> newVector;
264 newVector.push_back (changeEntry);
267 NS_LOG_INFO (
"We have a sub-route to " <<
id <<
" add it in route cache");
272 NS_LOG_INFO (
"Here we check the route cache again after updated the sub routes");
273 std::map<Ipv4Address, std::list<RouteCacheEntry> >::const_iterator m =
m_sortedRoutes.find (
id);
282 std::list<RouteCacheEntry> rtVector = m->second;
283 rt = rtVector.front ();
284 NS_LOG_LOGIC (
"Route to " <<
id <<
" with route size " << rtVector.size ());
293 if (type == std::string (
"LinkCache"))
297 else if (type == std::string (
"PathCache"))
323 std::map<Ipv4Address, uint32_t> d;
325 std::map<Ipv4Address, Ipv4Address> pre;
328 if (i->second.find (source) != i->second.end ())
330 d[i->first] = i->second[source];
331 pre[i->first] = source;
344 std::map<Ipv4Address, bool>
s;
347 for (uint32_t i = 0; i <
m_netGraph.size (); i++)
350 for (std::map<Ipv4Address,uint32_t>::const_iterator j = d.begin (); j != d.end (); ++j)
353 if (s.find (ip) == s.end ())
358 if (j->second <= temp)
368 for (std::map<Ipv4Address, uint32_t>::const_iterator k =
m_netGraph[tempip].begin (); k !=
m_netGraph[tempip].end (); ++k)
370 if (s.find (k->first) == s.end () && d[k->first] > d[tempip] + k->second)
372 d[k->first] = d[tempip] + k->second;
373 pre[k->first] = tempip;
381 else if (d[k->first] == d[tempip] + k->second)
383 std::map<Link, LinkStab>::iterator oldlink =
m_linkCache.find (
Link (k->first, pre[k->first]));
384 std::map<Link, LinkStab>::iterator newlink =
m_linkCache.find (
Link (k->first, tempip));
387 if (oldlink->second.GetLinkStability () < newlink->second.GetLinkStability ())
389 NS_LOG_INFO (
"Select the link with longest expected lifetime");
390 d[k->first] = d[tempip] + k->second;
391 pre[k->first] = tempip;
404 for (std::map<Ipv4Address, Ipv4Address>::iterator i = pre.begin (); i != pre.end (); ++i)
410 if (!i->second.IsBroadcast () && iptemp != source)
412 while (iptemp != source)
414 route.push_back (iptemp);
415 iptemp = pre[iptemp];
417 route.push_back (source);
420 for (RouteCacheEntry::IP_VECTOR::reverse_iterator j = route.rbegin (); j != route.rend (); ++j)
422 reverseroute.push_back (*j);
445 if (i->second.size () < 2)
455 NS_LOG_INFO (
"Route to " <<
id <<
" found with the length " << i->second.size ());
457 std::vector<Ipv4Address> path = rt.
GetVector ();
469 NS_LOG_DEBUG (
"The link stability " << i->second.GetLinkStability ().GetSeconds ());
470 std::map<Link, LinkStab>::iterator itmp = i;
471 if (i->second.GetLinkStability () <= Seconds (0))
484 NS_LOG_DEBUG (
"The node stability " << i->second.GetNodeStability ().GetSeconds ());
485 std::map<Ipv4Address, NodeStab>::iterator itmp = i;
486 if (i->second.GetNodeStability () <= Seconds (0))
508 m_netGraph[i->first.m_low][i->first.m_high] = weight;
509 m_netGraph[i->first.m_high][i->first.m_low] = weight;
517 std::map<Ipv4Address, NodeStab>::const_iterator i =
m_nodeCache.find (node);
528 NS_LOG_INFO (
"The node stability " << i->second.GetNodeStability ().GetSeconds ());
541 std::map<Ipv4Address, NodeStab>::const_iterator i =
m_nodeCache.find (node);
551 NS_LOG_INFO (
"The stability here " << i->second.GetNodeStability ().GetSeconds ());
567 for (uint32_t i = 0; i < nodelist.size () - 1; i++)
580 Link link (nodelist[i], nodelist[i + 1]);
620 for (RouteCacheEntry::IP_VECTOR::iterator i = rt.begin (); i != rt.end () - 1; ++i)
622 Link link (*i, *(i + 1));
638 for (RouteCacheEntry::IP_VECTOR::iterator i = rt.begin (); i != rt.end (); ++i)
649 NS_LOG_INFO (
"The node stability has already been increased");
660 std::list<RouteCacheEntry> rtVector;
662 std::vector<Ipv4Address> route = rt.
GetVector ();
665 std::map<Ipv4Address, std::list<RouteCacheEntry> >::const_iterator i =
670 rtVector.push_back (rt);
675 std::pair<std::map<Ipv4Address, std::list<RouteCacheEntry> >::iterator,
bool> result =
677 return result.second;
681 rtVector = i->second;
682 NS_LOG_DEBUG (
"The existing route size " << rtVector.size () <<
" for destination address " << dst);
693 NS_LOG_DEBUG (
"Find same vector, the FindSameRoute function will update the route expire time");
701 rtVector.push_back (rt);
705 NS_LOG_DEBUG (
"The first time" << rtVector.front ().GetExpireTime ().GetSeconds () <<
" The second time "
706 << rtVector.back ().GetExpireTime ().GetSeconds ());
707 NS_LOG_DEBUG (
"The first hop" << rtVector.front ().GetVector ().size () <<
" The second hop "
708 << rtVector.back ().GetVector ().size ());
713 std::pair<std::map<Ipv4Address, std::list<RouteCacheEntry> >::iterator,
bool> result =
715 return result.second;
719 NS_LOG_INFO (
"The newly found route is already expired");
729 for (std::list<RouteCacheEntry>::iterator i = rtVector.begin (); i != rtVector.end (); ++i)
735 if (routeVector == newVector)
737 NS_LOG_DEBUG (
"Found same routes in the route cache with the vector size "
740 <<
" the original expire time " << i->GetExpireTime ().GetSeconds ());
750 std::pair<std::map<Ipv4Address, std::list<RouteCacheEntry> >::iterator,
bool> result =
752 return result.second;
765 NS_LOG_LOGIC (
"Route deletion to " << dst <<
" successful");
768 NS_LOG_LOGIC (
"Route deletion to " << dst <<
" not successful");
784 Link link1 (errorSrc, unreachNode);
785 Link link2 (unreachNode, errorSrc);
794 std::map<Ipv4Address, NodeStab>::iterator i =
m_nodeCache.find (errorSrc);
797 NS_LOG_LOGIC (
"Update the node stability unsuccessfully");
806 NS_LOG_LOGIC (
"Update the node stability unsuccessfully");
829 for (std::map<
Ipv4Address, std::list<RouteCacheEntry> >::iterator j =
832 std::map<Ipv4Address, std::list<RouteCacheEntry> >::iterator jtmp = j;
834 std::list<RouteCacheEntry> rtVector = j->second;
838 for (std::list<RouteCacheEntry>::iterator k = rtVector.begin (); k != rtVector.end (); )
846 for (RouteCacheEntry::IP_VECTOR::iterator i = routeVector.begin (); i != routeVector.end (); ++i)
850 changeVector.push_back (*i);
854 if (*(i + 1) == unreachNode)
856 changeVector.push_back (*i);
861 changeVector.push_back (*i);
868 if (changeVector.size () == routeVector.size ())
870 NS_LOG_DEBUG (
"The route does not contain the broken link");
873 else if ((changeVector.size () < routeVector.size ()) && (changeVector.size () > 1))
878 Time expire = k->GetExpireTime ();
882 k = rtVector.erase (k);
886 NS_LOG_DEBUG (
"The destination of the newly formed route " << destination <<
" and the size of the route " << changeVector.size ());
889 rtVector.push_back (changeEntry);
890 NS_LOG_DEBUG (
"We have a sub-route to " << destination);
897 k = rtVector.erase (k);
902 NS_LOG_LOGIC (
"Cut route unsuccessful and erase the route");
906 k = rtVector.erase (k);
914 if (rtVector.size ())
924 NS_LOG_DEBUG (
"There is no route left for that destination " << address);
944 for (std::vector<Ipv4Address>::const_iterator i = vec.begin (); i != vec.end (); ++i)
955 for (std::list<RouteCacheEntry>::iterator i = route.begin (); i != route.end (); i++)
957 std::vector<Ipv4Address> path = i->GetVector ();
973 for (std::map<
Ipv4Address, std::list<RouteCacheEntry> >::iterator i =
977 std::map<Ipv4Address, std::list<RouteCacheEntry> >::iterator itmp = i;
982 std::list<RouteCacheEntry> rtVector = i->second;
983 NS_LOG_DEBUG (
"The route vector size of 1 " << dst <<
" " << rtVector.size ());
984 if (rtVector.size ())
986 for (std::list<RouteCacheEntry>::iterator j = rtVector.begin (); j != rtVector.end (); )
988 NS_LOG_DEBUG (
"The expire time of every entry with expire time " << j->GetExpireTime ());
992 if (j->GetExpireTime () <= Seconds (0))
997 NS_LOG_DEBUG (
"Erase the expired route for " << dst <<
" with expire time " << j->GetExpireTime ());
998 j = rtVector.erase (j);
1005 NS_LOG_DEBUG (
"The route vector size of 2 " << dst <<
" " << rtVector.size ());
1006 if (rtVector.size ())
1035 os <<
"\nDSR Route Cache\n"
1036 <<
"Destination\tGateway\t\tInterface\tFlag\tExpire\tHops\n";
1037 for (std::list<RouteCacheEntry>::const_iterator i =
1053 std::map<Ipv4Address, uint16_t>::const_iterator i =
1057 NS_LOG_LOGIC (
"No Ack id for " << nextHop <<
" found and use id 1 for the first network ack id");
1064 NS_LOG_LOGIC (
"Ack id for " << nextHop <<
" found in the cache has value " << ackId);
1086 for (std::vector<Neighbor>::const_iterator i =
m_nb.begin ();
1087 i !=
m_nb.end (); ++i)
1089 if (i->m_neighborAddress == addr)
1102 for (std::vector<Neighbor>::const_iterator i =
m_nb.begin (); i
1103 !=
m_nb.end (); ++i)
1105 if (i->m_neighborAddress == addr)
1117 for (std::vector<Neighbor>::iterator i =
m_nb.begin (); i !=
m_nb.end (); ++i)
1119 for (std::vector<Ipv4Address>::iterator j = nodeList.begin (); j != nodeList.end (); ++j)
1121 if (i->m_neighborAddress == (*j))
1137 m_nb.push_back (neighbor);
1144 NS_LOG_LOGIC (
"Add neighbor number " << nodeList.size ());
1145 for (std::vector<Ipv4Address>::iterator j = nodeList.begin (); j != nodeList.end ();)
1148 if (addr == ownAddress)
1150 j = nodeList.erase (j);
1151 NS_LOG_DEBUG (
"The node list size " << nodeList.size ());
1158 m_nb.push_back (neighbor);
1182 for (std::vector<Neighbor>::iterator j =
m_nb.begin (); j !=
m_nb.end (); ++j)
1186 NS_LOG_LOGIC (
"Close link to " << j->m_neighborAddress);
1192 m_nb.erase (std::remove_if (
m_nb.begin (),
m_nb.end (), pred),
m_nb.end ());
1207 m_arp.push_back (a);
1221 i !=
m_arp.end (); ++i)
1238 for (std::vector<Neighbor>::iterator i =
m_nb.begin (); i !=
m_nb.end (); ++i)
1240 if (i->m_hardwareAddress == addr)
bool CompareRoutesBoth(const RouteCacheEntry &a, const RouteCacheEntry &b)
bool DeleteRoute(Ipv4Address dst)
Delete the route with certain destination address.
keep track of time values and allow control of global simulation resolution
LinkStab(Time linkStab=Simulator::Now())
Constructor.
#define NS_LOG_FUNCTION(parameters)
void PurgeMac()
Remove all expired mac entries.
void Purge()
Delete all outdated entries and invalidate valid entry if Lifetime is expired.
Control the scheduling of simulation events.
void SetVector(IP_VECTOR v)
bool CompareRoutesHops(const RouteCacheEntry &a, const RouteCacheEntry &b)
bool IncStability(Ipv4Address node)
increase the stability of the node
Ipv4Address m_dst
The destination Ip address.
Ipv4Address GetDestination() const
void SetLinkStability(Time linkStab)
set/get the link stability
virtual ~LinkStab()
Destructor.
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
Time RouteCacheTimeout
The maximum period of time that dsr is allowed to for an unused route.
bool AddRoute(RouteCacheEntry &rt)
Add route cache entry if it doesn't yet exist in route cache.
void RebuildBestRouteTable(Ipv4Address source)
USE MAXWEIGHT TO REPRESENT MAX; USE BROADCAST ADDRESS TO REPRESENT NULL PRECEEDING ADDRESS...
NodeStab(Time nodeStab=Simulator::Now())
void Print(std::ostream &os)
Print route cache.
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
bool LookupRoute_Link(Ipv4Address id, RouteCacheEntry &rt)
used by LookupRoute when LinkCache
std::map< Ipv4Address, std::map< Ipv4Address, uint32_t > > m_netGraph
Current network graph state for this node, double is weight, which is calculated by the node informat...
bool CompareRoutesExpire(const RouteCacheEntry &a, const RouteCacheEntry &b)
uint32_t m_stabilityDecrFactor
Define the parameters for link cache type.
std::map< Ipv4Address, uint16_t > m_ackIdCache
The id cache to ensure all the ids are unique.
void SetCacheType(std::string type)
Dijsktra algorithm to get the best route from m_netGraph and update the m_bestRoutesTable_link when c...
double GetSeconds(void) const
void UseExtends(RouteCacheEntry::IP_VECTOR rt)
When a link from the Route Cache is used in routing a packet originated or salvaged by that node...
uint16_t CheckUniqueAckId(Ipv4Address nextHop)
Check for duplicate ids and save new entries if the id is not present in the table.
std::list< RouteCacheEntry::IP_VECTOR > routeVector
Define the vector of route entries.
void Schedule(void)
Schedule a new event using the currently-configured delay, function, and arguments.
void AddNeighbor(std::vector< Ipv4Address > nodeList, Ipv4Address ownAddress, Time expire)
Add to the neighbor list.
void UpdateNeighbor(std::vector< Ipv4Address > nodeList, Time expire)
Update expire time for entry with address addr, if it exists, else add new entry. ...
void ScheduleTimer()
Schedule m_ntimer.
std::vector< Ipv4Address > IP_VECTOR
Define the vector to hold Ip address.
bool m_subRoute
Check if save the sub route entries or not.
bool IsBroadcast(void) const
bool UpdateRouteEntry(Ipv4Address dst)
Update route cache entry if it has been recently used and successfully delivered the data packet...
The route cache structure.
bool operator()(const RouteCache::Neighbor &nb) const
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
void Invalidate(Time badLinkLifetime)
Mark entry as "down" (i.e. disable it)
void DelArpCache(Ptr< ArpCache >)
Don't use given ARP cache any more (interface is down)
bool IsExpired(void) const
RouteCacheEntry(IP_VECTOR const &ip=IP_VECTOR(), Ipv4Address dst=Ipv4Address(), Time exp=Simulator::Now())
void PrintRouteVector(std::list< RouteCacheEntry > route)
Print all the route vector elements from the route list.
std::map< Link, LinkStab > m_linkCache
The data structure to store link info.
void SetDelay(const Time &delay)
#define NS_LOG_LOGIC(msg)
Address GetMacAddress(void) const
virtual ~RouteCacheEntry()
Callback< void, WifiMacHeader const & > m_txErrorCallback
TX error callback.
void ProcessTxError(WifiMacHeader const &)
Process layer 2 TX error notification.
uint32_t m_stabilityIncrFactor
Mac48Address LookupMacAddress(Ipv4Address)
Find MAC address by IP using list of ARP caches.
uint8_t m_reqCount
Number of route requests.
bool AddRoute_Link(RouteCacheEntry::IP_VECTOR nodelist, Ipv4Address node)
static Mac48Address ConvertFrom(const Address &address)
void RemoveLastEntry(std::list< RouteCacheEntry > &rtVector)
Remove the aged route cache entries when the route cache is full.
void PrintVector(std::vector< Ipv4Address > &vec)
Print the route vector elements.
void Print(std::ostream &os) const
Print necessary fields.
Time GetLinkStability() const
static TypeId GetTypeId()
DSR route request queue Since DSR is an on demand routing we queue requests while looking for route...
A record that that holds information about an ArpCache entry.
The following code handles link-layer acks.
uint16_t GetAckSize()
Get the ack table size.
Time GetExpireTime() const
static Time Now(void)
Return the "current simulation time".
IP_VECTOR GetVector() const
std::vector< Ptr< ArpCache > > m_arp
list of ARP cached to be used for layer 2 notifications processing
std::list< RouteCacheEntry >::value_type route_pair
void SetExpireTime(Time exp)
bool LookupRoute(Ipv4Address id, RouteCacheEntry &rt)
Lookup route cache entry with destination address dst.
void SetNodeStability(Time nodeStab)
Ipv4 addresses are stored in host order in this class.
uint32_t m_maxEntriesEachDst
number of entries for each destination
Timer m_ntimer
Timer for neighbor's list. Schedule Purge().
std::map< Ipv4Address, RouteCacheEntry::IP_VECTOR > m_bestRoutesTable_link
for link route cache
#define MAXWEIGHT
The link cache to update all the link status, bi-link is two link for link is a struct when the weigh...
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
#define NS_LOG_DEBUG(msg)
void Cancel(void)
Cancel the currently-running event if there is one.
std::map< Ipv4Address, routeEntryVector > m_sortedRoutes
Map the ipv4Address to route entry vector.
Time m_expire
Expire time for queue entry.
void UpdateNetGraph()
Update the Net Graph for the link and node cache has changed.
Callback< void, Ipv4Address, uint8_t > m_handleLinkFailure
The following code handles link-layer acks.
bool FindSameRoute(RouteCacheEntry &rt, std::list< RouteCacheEntry > &rtVector)
Find the same route in the route cache.
a base class which provides memory management and object aggregation
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...
bool DecStability(Ipv4Address node)
decrease the stability of the node
std::vector< Neighbor > m_nb
vector of entries
routeEntryVector m_routeEntryVector
Define the route vector.
bool m_isLinkCache
Check if the route is using path cache or link cache.
a unique identifier for an interface.
bool IsNeighbor(Ipv4Address addr)
Check that node with address addr is neighbor.
Time GetExpireTime(Ipv4Address addr)
Return expire time for neighbor node with address addr, if exists, else return 0. ...
TypeId SetParent(TypeId tid)
NS_LOG_COMPONENT_DEFINE("RouteCache")
void SetDestination(Ipv4Address d)
Time m_delay
This timeout deals with the passive ack.
void AddArpCache(Ptr< ArpCache >)
Add ARP cache to be used to allow layer 2 notifications processing.
std::map< Ipv4Address, NodeStab > m_nodeCache
The data structure to store node info.