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),
140 .AddConstructor<RouteCache> ()
147 m_maxEntriesEachDst (3),
148 m_isLinkCache (false),
149 m_ntimer (
Timer::CANCEL_ON_DESTROY),
172 rtVector.pop_back ();
179 std::map<Ipv4Address, std::list<RouteCacheEntry> >::const_iterator i =
183 NS_LOG_DEBUG (
"Failed to find the route entry for the destination " << dst);
188 std::list<RouteCacheEntry> rtVector = i->second;
191 rtVector.pop_front ();
192 rtVector.push_back (successEntry);
198 std::pair<std::map<Ipv4Address, std::list<RouteCacheEntry> >::iterator,
bool> result =
200 return result.second;
218 NS_LOG_LOGIC (
"Route to " <<
id <<
" not found; m_sortedRoutes is empty");
221 std::map<Ipv4Address, std::list<RouteCacheEntry> >::const_iterator i =
m_sortedRoutes.find (
id);
225 for (std::map<
Ipv4Address, std::list<RouteCacheEntry> >::const_iterator j =
228 std::list<RouteCacheEntry> rtVector = j->second;
232 for (std::list<RouteCacheEntry>::const_iterator k = rtVector.begin (); k != rtVector.end (); ++k)
238 for (RouteCacheEntry::IP_VECTOR::iterator l = routeVector.begin (); l != routeVector.end (); ++l)
242 changeVector.push_back (*l);
246 changeVector.push_back (*l);
254 if ((changeVector.size () < routeVector.size ()) && (changeVector.size () > 1))
262 std::list<RouteCacheEntry> newVector;
263 newVector.push_back (changeEntry);
266 NS_LOG_INFO (
"We have a sub-route to " <<
id <<
" add it in route cache");
271 NS_LOG_INFO (
"Here we check the route cache again after updated the sub routes");
272 std::map<Ipv4Address, std::list<RouteCacheEntry> >::const_iterator m =
m_sortedRoutes.find (
id);
281 std::list<RouteCacheEntry> rtVector = m->second;
282 rt = rtVector.front ();
283 NS_LOG_DEBUG (
"Route to " <<
id <<
" with route size " << rtVector.size ());
292 if (type == std::string (
"LinkCache"))
296 else if (type == std::string (
"PathCache"))
322 std::map<Ipv4Address, uint32_t> d;
324 std::map<Ipv4Address, Ipv4Address> pre;
327 if (i->second.find (source) != i->second.end ())
329 d[i->first] = i->second[source];
330 pre[i->first] = source;
343 std::map<Ipv4Address, bool> s;
346 for (uint32_t i = 0; i <
m_netGraph.size (); i++)
349 for (std::map<Ipv4Address,uint32_t>::const_iterator j = d.begin (); j != d.end (); ++j)
352 if (s.find (ip) == s.end ())
357 if (j->second <= temp)
367 for (std::map<Ipv4Address, uint32_t>::const_iterator k =
m_netGraph[tempip].begin (); k !=
m_netGraph[tempip].end (); ++k)
369 if (s.find (k->first) == s.end () && d[k->first] > d[tempip] + k->second)
371 d[k->first] = d[tempip] + k->second;
372 pre[k->first] = tempip;
380 else if (d[k->first] == d[tempip] + k->second)
382 std::map<Link, LinkStab>::iterator oldlink =
m_linkCache.find (
Link (k->first, pre[k->first]));
383 std::map<Link, LinkStab>::iterator newlink =
m_linkCache.find (
Link (k->first, tempip));
386 if (oldlink->second.GetLinkStability () < newlink->second.GetLinkStability ())
388 NS_LOG_INFO (
"Select the link with longest expected lifetime");
389 d[k->first] = d[tempip] + k->second;
390 pre[k->first] = tempip;
403 for (std::map<Ipv4Address, Ipv4Address>::iterator i = pre.begin (); i != pre.end (); ++i)
409 if (!i->second.IsBroadcast () && iptemp != source)
411 while (iptemp != source)
413 route.push_back (iptemp);
414 iptemp = pre[iptemp];
416 route.push_back (source);
421 for (RouteCacheEntry::IP_VECTOR::reverse_iterator j = route.rbegin (); j != route.rend (); ++j)
423 reverseroute.push_back (*j);
444 if (i->second.size () < 2)
454 NS_LOG_INFO (
"Route to " <<
id <<
" found with the route length " << i->second.size ());
456 std::vector<Ipv4Address> path = rt.
GetVector ();
469 NS_LOG_DEBUG (
"The link stability " << i->second.GetLinkStability ());
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 ());
485 std::map<Ipv4Address, NodeStab>::iterator itmp = i;
486 if (i->second.GetNodeStability () <=
Seconds (0))
507 m_netGraph[i->first.m_low][i->first.m_high] = weight;
508 m_netGraph[i->first.m_high][i->first.m_low] = weight;
516 std::map<Ipv4Address, NodeStab>::const_iterator i =
m_nodeCache.find (node);
537 std::map<Ipv4Address, NodeStab>::const_iterator i =
m_nodeCache.find (node);
558 for (uint32_t i = 0; i < nodelist.size () - 1; i++)
571 Link link (nodelist[i], nodelist[i + 1]);
607 for (RouteCacheEntry::IP_VECTOR::iterator i = rt.begin (); i != rt.end () - 1; ++i)
609 Link link (*i, *(i + 1));
624 for (RouteCacheEntry::IP_VECTOR::iterator i = rt.begin (); i != rt.end (); ++i)
635 NS_LOG_INFO (
"The node stability has already been increased");
646 std::list<RouteCacheEntry> rtVector;
648 std::vector<Ipv4Address> route = rt.
GetVector ();
651 std::map<Ipv4Address, std::list<RouteCacheEntry> >::const_iterator i =
656 rtVector.push_back (rt);
661 std::pair<std::map<Ipv4Address, std::list<RouteCacheEntry> >::iterator,
bool> result =
663 return result.second;
667 rtVector = i->second;
668 NS_LOG_DEBUG (
"The existing route size " << rtVector.size () <<
" for destination address " << dst);
679 NS_LOG_DEBUG (
"Find same vector, the FindSameRoute function will update the route expire time");
687 rtVector.push_back (rt);
691 NS_LOG_DEBUG (
"The first time" << rtVector.front ().GetExpireTime ().GetSeconds () <<
" The second time "
692 << rtVector.back ().GetExpireTime ().GetSeconds ());
693 NS_LOG_DEBUG (
"The first hop" << rtVector.front ().GetVector ().size () <<
" The second hop "
694 << rtVector.back ().GetVector ().size ());
699 std::pair<std::map<Ipv4Address, std::list<RouteCacheEntry> >::iterator,
bool> result =
701 return result.second;
705 NS_LOG_INFO (
"The newly found route is already expired");
715 for (std::list<RouteCacheEntry>::iterator i = rtVector.begin (); i != rtVector.end (); ++i)
721 if (routeVector == newVector)
723 NS_LOG_DEBUG (
"Found same routes in the route cache with the vector size "
726 <<
" the original expire time " << i->GetExpireTime ().GetSeconds ());
736 std::pair<std::map<Ipv4Address, std::list<RouteCacheEntry> >::iterator,
bool> result =
738 return result.second;
751 NS_LOG_LOGIC (
"Route deletion to " << dst <<
" successful");
754 NS_LOG_LOGIC (
"Route deletion to " << dst <<
" not successful");
768 Link link1 (errorSrc, unreachNode);
769 Link link2 (unreachNode, errorSrc);
775 std::map<Ipv4Address, NodeStab>::iterator i =
m_nodeCache.find (errorSrc);
778 NS_LOG_LOGIC (
"Update the node stability unsuccessfully");
787 NS_LOG_LOGIC (
"Update the node stability unsuccessfully");
811 for (std::map<
Ipv4Address, std::list<RouteCacheEntry> >::iterator j =
814 std::map<Ipv4Address, std::list<RouteCacheEntry> >::iterator jtmp = j;
816 std::list<RouteCacheEntry> rtVector = j->second;
820 for (std::list<RouteCacheEntry>::iterator k = rtVector.begin (); k != rtVector.end (); )
828 for (RouteCacheEntry::IP_VECTOR::iterator i = routeVector.begin (); i != routeVector.end (); ++i)
832 changeVector.push_back (*i);
836 if (*(i + 1) == unreachNode)
838 changeVector.push_back (*i);
843 changeVector.push_back (*i);
850 if (changeVector.size () == routeVector.size ())
852 NS_LOG_DEBUG (
"The route does not contain the broken link");
855 else if ((changeVector.size () < routeVector.size ()) && (changeVector.size () > 1))
860 Time expire = k->GetExpireTime ();
864 k = rtVector.erase (k);
868 NS_LOG_DEBUG (
"The destination of the newly formed route " << destination <<
" and the size of the route " << changeVector.size ());
871 rtVector.push_back (changeEntry);
872 NS_LOG_DEBUG (
"We have a sub-route to " << destination);
879 k = rtVector.erase (k);
884 NS_LOG_LOGIC (
"Cut route unsuccessful and erase the route");
888 k = rtVector.erase (k);
896 if (rtVector.size ())
906 NS_LOG_DEBUG (
"There is no route left for that destination " << address);
926 for (std::vector<Ipv4Address>::const_iterator i = vec.begin (); i != vec.end (); ++i)
937 for (std::list<RouteCacheEntry>::iterator i = route.begin (); i != route.end (); i++)
939 std::vector<Ipv4Address> path = i->GetVector ();
955 for (std::map<
Ipv4Address, std::list<RouteCacheEntry> >::iterator i =
959 std::map<Ipv4Address, std::list<RouteCacheEntry> >::iterator itmp = i;
964 std::list<RouteCacheEntry> rtVector = i->second;
965 NS_LOG_DEBUG (
"The route vector size of 1 " << dst <<
" " << rtVector.size ());
966 if (rtVector.size ())
968 for (std::list<RouteCacheEntry>::iterator j = rtVector.begin (); j != rtVector.end (); )
970 NS_LOG_DEBUG (
"The expire time of every entry with expire time " << j->GetExpireTime ());
974 if (j->GetExpireTime () <=
Seconds (0))
979 NS_LOG_DEBUG (
"Erase the expired route for " << dst <<
" with expire time " << j->GetExpireTime ());
980 j = rtVector.erase (j);
987 NS_LOG_DEBUG (
"The route vector size of 2 " << dst <<
" " << rtVector.size ());
988 if (rtVector.size ())
1017 os <<
"\nDSR Route Cache\n"
1018 <<
"Destination\tGateway\t\tInterface\tFlag\tExpire\tHops\n";
1019 for (std::list<RouteCacheEntry>::const_iterator i =
1035 std::map<Ipv4Address, uint16_t>::const_iterator i =
1039 NS_LOG_LOGIC (
"No Ack id for " << nextHop <<
" found and use id 1 for the first network ack id");
1046 NS_LOG_LOGIC (
"Ack id for " << nextHop <<
" found in the cache has value " << ackId);
1068 for (std::vector<Neighbor>::const_iterator i =
m_nb.begin ();
1069 i !=
m_nb.end (); ++i)
1071 if (i->m_neighborAddress == addr)
1084 for (std::vector<Neighbor>::const_iterator i =
m_nb.begin (); i
1085 !=
m_nb.end (); ++i)
1087 if (i->m_neighborAddress == addr)
1099 for (std::vector<Neighbor>::iterator i =
m_nb.begin (); i !=
m_nb.end (); ++i)
1101 for (std::vector<Ipv4Address>::iterator j = nodeList.begin (); j != nodeList.end (); ++j)
1103 if (i->m_neighborAddress == (*j))
1119 m_nb.push_back (neighbor);
1126 NS_LOG_LOGIC (
"Add neighbor number " << nodeList.size ());
1127 for (std::vector<Ipv4Address>::iterator j = nodeList.begin (); j != nodeList.end (); ++j)
1130 if (addr == ownAddress)
1133 NS_LOG_DEBUG (
"The node list size " << nodeList.size ());
1136 m_nb.push_back (neighbor);
1160 for (std::vector<Neighbor>::iterator j =
m_nb.begin (); j !=
m_nb.end (); ++j)
1164 NS_LOG_LOGIC (
"Close link to " << j->m_neighborAddress);
1170 m_nb.erase (std::remove_if (
m_nb.begin (),
m_nb.end (), pred),
m_nb.end ());
1185 m_arp.push_back (a);
1199 i !=
m_arp.end (); ++i)
1216 for (std::vector<Neighbor>::iterator i =
m_nb.begin (); i !=
m_nb.end (); ++i)
1218 if (i->m_hardwareAddress == addr)