35 #include "ns3/address-utils.h"
36 #include "ns3/ipv4-route.h"
38 #include "ns3/packet.h"
39 #include "ns3/simulator.h"
40 #include "ns3/socket.h"
41 #include "ns3/wifi-mac-header.h"
113 typedef std::list<DsrRouteCacheEntry>::value_type
route_pair;
116 : m_ackTimer(
Timer::CANCEL_ON_DESTROY),
121 m_blackListState(false),
157 m_maxEntriesEachDst(3),
158 m_isLinkCache(false),
159 m_ntimer(
Timer::CANCEL_ON_DESTROY),
189 std::map<Ipv4Address, std::list<DsrRouteCacheEntry>>::const_iterator i =
193 NS_LOG_LOGIC(
"Failed to find the route entry for the destination " << dst);
198 std::list<DsrRouteCacheEntry> rtVector = i->second;
201 rtVector.pop_front();
202 rtVector.push_back(successEntry);
208 std::pair<std::map<Ipv4Address, std::list<DsrRouteCacheEntry>>::iterator,
bool> result =
210 return result.second;
228 NS_LOG_LOGIC(
"Route to " <<
id <<
" not found; m_sortedRoutes is empty");
231 std::map<Ipv4Address, std::list<DsrRouteCacheEntry>>::const_iterator i =
236 for (std::map<
Ipv4Address, std::list<DsrRouteCacheEntry>>::const_iterator j =
241 std::list<DsrRouteCacheEntry> rtVector =
246 for (std::list<DsrRouteCacheEntry>::const_iterator
k = rtVector.begin();
254 for (DsrRouteCacheEntry::IP_VECTOR::iterator l =
routeVector.begin();
260 changeVector.push_back(*l);
264 changeVector.push_back(*l);
272 if ((changeVector.size() <
routeVector.size()) && (changeVector.size() > 1))
280 std::list<DsrRouteCacheEntry> newVector;
281 newVector.push_back(changeEntry);
285 NS_LOG_INFO(
"We have a sub-route to " <<
id <<
" add it in route cache");
290 NS_LOG_INFO(
"Here we check the route cache again after updated the sub routes");
291 std::map<Ipv4Address, std::list<DsrRouteCacheEntry>>::const_iterator
m =
301 std::list<DsrRouteCacheEntry> rtVector =
m->second;
302 rt = rtVector.front();
303 NS_LOG_LOGIC(
"Route to " <<
id <<
" with route size " << rtVector.size());
312 if (
type == std::string(
"LinkCache"))
316 else if (
type == std::string(
"PathCache"))
342 std::map<Ipv4Address, uint32_t> d;
344 std::map<Ipv4Address, Ipv4Address> pre;
349 if (i->second.find(source) != i->second.end())
351 d[i->first] = i->second[source];
352 pre[i->first] = source;
365 std::map<Ipv4Address, bool> s;
368 for (uint32_t i = 0; i <
m_netGraph.size(); i++)
371 for (std::map<Ipv4Address, uint32_t>::const_iterator j = d.begin(); j != d.end(); ++j)
374 if (s.find(ip) == s.end())
379 if (j->second <= temp)
389 for (std::map<Ipv4Address, uint32_t>::const_iterator
k =
m_netGraph[tempip].begin();
393 if (s.find(
k->first) == s.end() && d[
k->first] > d[tempip] +
k->second)
395 d[
k->first] = d[tempip] +
k->second;
396 pre[
k->first] = tempip;
405 else if (d[
k->first] == d[tempip] +
k->second)
407 std::map<Link, DsrLinkStab>::iterator oldlink =
409 std::map<Link, DsrLinkStab>::iterator newlink =
413 if (oldlink->second.GetLinkStability() < newlink->second.GetLinkStability())
415 NS_LOG_INFO(
"Select the link with longest expected lifetime");
416 d[
k->first] = d[tempip] +
k->second;
417 pre[
k->first] = tempip;
430 for (std::map<Ipv4Address, Ipv4Address>::iterator i = pre.begin(); i != pre.end(); ++i)
436 if (!i->second.IsBroadcast() && iptemp != source)
438 while (iptemp != source)
440 route.push_back(iptemp);
441 iptemp = pre[iptemp];
443 route.push_back(source);
446 for (DsrRouteCacheEntry::IP_VECTOR::reverse_iterator j = route.rbegin();
450 reverseroute.push_back(*j);
465 std::map<Ipv4Address, DsrRouteCacheEntry::IP_VECTOR>::const_iterator i =
474 if (i->second.size() < 2)
484 NS_LOG_INFO(
"Route to " <<
id <<
" found with the length " << i->second.size());
499 std::map<Link, DsrLinkStab>::iterator itmp = i;
500 if (i->second.GetLinkStability() <=
Seconds(0))
511 for (std::map<Ipv4Address, DsrNodeStab>::iterator i =
m_nodeCache.begin();
515 std::map<Ipv4Address, DsrNodeStab>::iterator itmp = i;
516 if (i->second.GetNodeStability() <=
Seconds(0))
538 m_netGraph[i->first.m_low][i->first.m_high] = weight;
539 m_netGraph[i->first.m_high][i->first.m_low] = weight;
547 std::map<Ipv4Address, DsrNodeStab>::const_iterator i =
m_nodeCache.find(node);
572 std::map<Ipv4Address, DsrNodeStab>::const_iterator i =
m_nodeCache.find(node);
599 for (uint32_t i = 0; i < nodelist.size() - 1; i++)
612 Link link(nodelist[i], nodelist[i + 1]);
653 for (DsrRouteCacheEntry::IP_VECTOR::iterator i = rt.begin(); i != rt.end() - 1; ++i)
655 Link link(*i, *(i + 1));
672 for (DsrRouteCacheEntry::IP_VECTOR::iterator i = rt.begin(); i != rt.end(); ++i)
683 NS_LOG_INFO(
"The node stability has already been increased");
694 std::list<DsrRouteCacheEntry> rtVector;
696 std::vector<Ipv4Address> route = rt.
GetVector();
699 std::map<Ipv4Address, std::list<DsrRouteCacheEntry>>::const_iterator i =
704 rtVector.push_back(rt);
709 std::pair<std::map<Ipv4Address, std::list<DsrRouteCacheEntry>>::iterator,
bool> result =
711 return result.second;
715 rtVector = i->second;
716 NS_LOG_DEBUG(
"The existing route size " << rtVector.size() <<
" for destination address "
730 "Find same vector, the FindSameRoute function will update the route expire time");
738 rtVector.push_back(rt);
743 <<
" The second time "
744 << rtVector.back().GetExpireTime().As(
Time::S));
745 NS_LOG_DEBUG(
"The first hop" << rtVector.front().GetVector().size()
746 <<
" The second hop "
747 << rtVector.back().GetVector().size());
752 std::pair<std::map<Ipv4Address, std::list<DsrRouteCacheEntry>>::iterator,
bool>
754 return result.second;
758 NS_LOG_INFO(
"The newly found route is already expired");
769 for (std::list<DsrRouteCacheEntry>::iterator i = rtVector.begin(); i != rtVector.end(); ++i)
777 NS_LOG_DEBUG(
"Found same routes in the route cache with the vector size "
780 <<
" the original expire time "
781 << i->GetExpireTime().As(
Time::S));
791 std::pair<std::map<Ipv4Address, std::list<DsrRouteCacheEntry>>::iterator,
bool> result =
793 return result.second;
806 NS_LOG_LOGIC(
"Route deletion to " << dst <<
" successful");
809 NS_LOG_LOGIC(
"Route deletion to " << dst <<
" not successful");
827 Link link1(errorSrc, unreachNode);
828 Link link2(unreachNode, errorSrc);
837 std::map<Ipv4Address, DsrNodeStab>::iterator i =
m_nodeCache.find(errorSrc);
840 NS_LOG_LOGIC(
"Update the node stability unsuccessfuly");
849 NS_LOG_LOGIC(
"Update the node stability unsuccessfuly");
872 for (std::map<
Ipv4Address, std::list<DsrRouteCacheEntry>>::iterator j =
876 std::map<Ipv4Address, std::list<DsrRouteCacheEntry>>::iterator jtmp = j;
878 std::list<DsrRouteCacheEntry> rtVector = j->second;
882 for (std::list<DsrRouteCacheEntry>::iterator
k = rtVector.begin();
k != rtVector.end();)
890 for (DsrRouteCacheEntry::IP_VECTOR::iterator i =
routeVector.begin();
896 changeVector.push_back(*i);
900 if (*(i + 1) == unreachNode)
902 changeVector.push_back(*i);
907 changeVector.push_back(*i);
916 NS_LOG_DEBUG(
"The route does not contain the broken link");
919 else if ((changeVector.size() <
routeVector.size()) && (changeVector.size() > 1))
924 Time expire =
k->GetExpireTime();
928 k = rtVector.erase(
k);
932 NS_LOG_DEBUG(
"The destination of the newly formed route "
933 << destination <<
" and the size of the route "
934 << changeVector.size());
938 rtVector.push_back(changeEntry);
946 k = rtVector.erase(
k);
951 NS_LOG_LOGIC(
"Cut route unsuccessful and erase the route");
955 k = rtVector.erase(
k);
963 if (!rtVector.empty())
993 for (std::vector<Ipv4Address>::const_iterator i = vec.begin(); i != vec.end(); ++i)
1004 for (std::list<DsrRouteCacheEntry>::iterator i = route.begin(); i != route.end(); i++)
1006 std::vector<Ipv4Address>
path = i->GetVector();
1026 std::map<Ipv4Address, std::list<DsrRouteCacheEntry>>::iterator itmp = i;
1031 std::list<DsrRouteCacheEntry> rtVector = i->second;
1032 NS_LOG_DEBUG(
"The route vector size of 1 " << dst <<
" " << rtVector.size());
1033 if (!rtVector.empty())
1035 for (std::list<DsrRouteCacheEntry>::iterator j = rtVector.begin(); j != rtVector.end();)
1037 NS_LOG_DEBUG(
"The expire time of every entry with expire time "
1038 << j->GetExpireTime());
1042 if (j->GetExpireTime() <=
Seconds(0))
1047 NS_LOG_DEBUG(
"Erase the expired route for " << dst <<
" with expire time "
1048 << j->GetExpireTime());
1049 j = rtVector.erase(j);
1056 NS_LOG_DEBUG(
"The route vector size of 2 " << dst <<
" " << rtVector.size());
1057 if (!rtVector.empty())
1085 os <<
"\nDSR Route Cache\n"
1086 <<
"Destination\tGateway\t\tInterface\tFlag\tExpire\tHops\n";
1104 std::map<Ipv4Address, uint16_t>::const_iterator i =
m_ackIdCache.find(nextHop);
1108 <<
" found and use id 1 for the first network ack id");
1115 NS_LOG_LOGIC(
"Ack id for " << nextHop <<
" found in the cache has value " << ackId);
1137 for (std::vector<Neighbor>::const_iterator i =
m_nb.begin(); i !=
m_nb.end(); ++i)
1139 if (i->m_neighborAddress == addr)
1152 for (std::vector<Neighbor>::const_iterator i =
m_nb.begin(); i !=
m_nb.end(); ++i)
1154 if (i->m_neighborAddress == addr)
1166 for (std::vector<Neighbor>::iterator i =
m_nb.begin(); i !=
m_nb.end(); ++i)
1168 for (std::vector<Ipv4Address>::iterator j = nodeList.begin(); j != nodeList.end(); ++j)
1170 if (i->m_neighborAddress == (*j))
1185 m_nb.push_back(neighbor);
1192 NS_LOG_LOGIC(
"Add neighbor number " << nodeList.size());
1193 for (std::vector<Ipv4Address>::iterator j = nodeList.begin(); j != nodeList.end();)
1196 if (addr == ownAddress)
1198 j = nodeList.erase(j);
1199 NS_LOG_DEBUG(
"The node list size " << nodeList.size());
1206 m_nb.push_back(neighbor);
1237 for (std::vector<Neighbor>::iterator j =
m_nb.begin(); j !=
m_nb.end(); ++j)
1241 NS_LOG_LOGIC(
"Close link to " << j->m_neighborAddress);
1292 for (std::vector<Neighbor>::iterator i =
m_nb.begin(); i !=
m_nb.end(); ++i)
1294 if (i->m_hardwareAddress == addr)
A record that that holds information about an ArpCache entry.
Address GetMacAddress() const
Ipv4 addresses are stored in host order in this class.
static Mac48Address ConvertFrom(const Address &address)
A base class which provides memory management and object aggregation.
Control the scheduling of simulation events.
static Time Now()
Return the current simulation virtual time.
Simulation virtual time values and global simulation resolution.
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
A simple virtual Timer class.
void SetDelay(const Time &delay)
void Cancel()
Cancel the currently-running event if there is one.
void Schedule()
Schedule a new event using the currently-configured delay, function, and arguments.
a unique identifier for an interface.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
DsrLinkStab class (DSR link stability)
Time GetLinkStability() const
get the link stability
DsrLinkStab(Time linkStab=Simulator::Now())
Constructor.
void Print() const
Print function.
void SetLinkStability(Time linkStab)
set the link stability
virtual ~DsrLinkStab()
Destructor.
DsrNodeStab class (DSR node stability)
void SetNodeStability(Time nodeStab)
Set node stability.
DsrNodeStab(Time nodeStab=Simulator::Now())
Constructor.
DsrRouteCacheEntry class for entries in the route cache.
IP_VECTOR GetVector() const
Get the IP vector.
void SetDestination(Ipv4Address d)
Set destination address.
Time m_expire
Expire time for queue entry.
DsrRouteCacheEntry(IP_VECTOR const &ip=IP_VECTOR(), Ipv4Address dst=Ipv4Address(), Time exp=Simulator::Now())
Constructor.
Ipv4Address m_dst
The destination Ip address.
uint8_t m_reqCount
Number of route requests.
Ipv4Address GetDestination() const
Get destination address.
virtual ~DsrRouteCacheEntry()
void Invalidate(Time badLinkLifetime)
Mark entry as "down" (i.e.
void Print(std::ostream &os) const
Print necessary fields.
std::vector< Ipv4Address > IP_VECTOR
Define the vector to hold Ip address.
Time GetExpireTime() const
Get expire time.
void SetExpireTime(Time exp)
Set expire time.
void SetVector(IP_VECTOR v)
Sets the IP vector.
DSR route request queue Since DSR is an on demand routing we queue requests while looking for route.
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...
uint32_t m_stabilityDecrFactor
stability decrease factor
std::list< DsrRouteCacheEntry::IP_VECTOR > routeVector
Define the vector of route entries.
static TypeId GetTypeId()
Get the type ID.
void PurgeLinkNode()
Purge from the cache if the stability time expired.
Callback< void, Ipv4Address, uint8_t > m_handleLinkFailure
The following code handles link-layer acks.
std::map< Link, DsrLinkStab > m_linkCache
The data structure to store link info.
void ScheduleTimer()
Schedule m_ntimer.
void RebuildBestRouteTable(Ipv4Address source)
Rebuild the best route table.
void SetCacheType(std::string type)
Dijsktra algorithm to get the best route from m_netGraph and update the m_bestRoutesTable_link when c...
routeEntryVector m_routeEntryVector
Define the route vector.
void AddArpCache(Ptr< ArpCache > a)
Add ARP cache to be used to allow layer 2 notifications processing.
std::map< Ipv4Address, DsrNodeStab > m_nodeCache
The data structure to store node info.
void Purge()
Delete all outdated entries and invalidate valid entry if Lifetime is expired.
Time m_initStability
initial stability
uint16_t CheckUniqueAckId(Ipv4Address nextHop)
Check for duplicate ids and save new entries if the id is not present in the table.
bool IsLinkCache()
is link cached
uint32_t m_stabilityIncrFactor
stability increase factor
Callback< void, const WifiMacHeader & > m_txErrorCallback
TX error callback.
bool LookupRoute(Ipv4Address id, DsrRouteCacheEntry &rt)
Lookup route cache entry with destination address dst.
uint16_t GetAckSize()
Get the ack table size.
std::map< Ipv4Address, uint16_t > m_ackIdCache
The id cache to ensure all the ids are unique.
void ProcessTxError(const WifiMacHeader &hdr)
Process layer 2 TX error notification.
Time RouteCacheTimeout
The maximum period of time that dsr is allowed to for an unused route.
bool AddRoute_Link(DsrRouteCacheEntry::IP_VECTOR nodelist, Ipv4Address node)
dd route link to cache
std::vector< Ptr< ArpCache > > m_arp
list of ARP cached to be used for layer 2 notifications processing
Mac48Address LookupMacAddress(Ipv4Address addr)
Find MAC address by IP using list of ARP caches.
void UseExtends(DsrRouteCacheEntry::IP_VECTOR rt)
When a link from the Route Cache is used in routing a packet originated or salvaged by that node,...
void PurgeMac()
Remove all expired mac entries.
bool FindSameRoute(DsrRouteCacheEntry &rt, std::list< DsrRouteCacheEntry > &rtVector)
Find the same route in the route cache.
~DsrRouteCache() override
std::map< Ipv4Address, routeEntryVector > m_sortedRoutes
Map the ipv4Address to route entry vector.
void PrintVector(std::vector< Ipv4Address > &vec)
Print the route vector elements.
bool m_isLinkCache
Check if the route is using path cache or link cache.
Time m_delay
This timeout deals with the passive ack.
bool DeleteRoute(Ipv4Address dst)
Delete the route with certain destination address.
bool IncStability(Ipv4Address node)
increase the stability of the node
void PrintRouteVector(std::list< DsrRouteCacheEntry > route)
Print all the route vector elements from the route list.
uint32_t m_maxEntriesEachDst
number of entries for each destination
Time GetExpireTime(Ipv4Address addr)
Return expire time for neighbor node with address addr, if exists, else return 0.
Time m_useExtends
use extend
std::vector< Neighbor > m_nb
vector of entries
Time m_minLifeTime
minimum lifetime
void DelArpCache(Ptr< ArpCache >)
Don't use the provided ARP cache any more (interface is down)
void Print(std::ostream &os)
Print route cache.
bool LookupRoute_Link(Ipv4Address id, DsrRouteCacheEntry &rt)
used by LookupRoute when LinkCache
bool UpdateRouteEntry(Ipv4Address dst)
Update route cache entry if it has been recently used and successfully delivered the data packet.
void UpdateNeighbor(std::vector< Ipv4Address > nodeList, Time expire)
Update expire time for entry with address addr, if it exists, else add new entry.
void RemoveLastEntry(std::list< DsrRouteCacheEntry > &rtVector)
Remove the aged route cache entries when the route cache is full.
Timer m_ntimer
Timer for neighbor's list. Schedule Purge().
bool m_subRoute
Check if save the sub route entries or not.
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 IsNeighbor(Ipv4Address addr)
Check that node with address addr is neighbor.
std::map< Ipv4Address, DsrRouteCacheEntry::IP_VECTOR > m_bestRoutesTable_link
for link route cache
bool DecStability(Ipv4Address node)
decrease the stability of the node
void UpdateNetGraph()
Update the Net Graph for the link and node cache has changed.
bool AddRoute(DsrRouteCacheEntry &rt)
Add route cache entry if it doesn't yet exist in route cache.
void AddNeighbor(std::vector< Ipv4Address > nodeList, Ipv4Address ownAddress, Time expire)
Add to the neighbor list.
#define MAXWEIGHT
The link cache to update all the link status, bi-link is two link for link is a struct when the weigh...
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Time Now()
create an ns3::Time instance which contains the current simulation time.
Time Seconds(double value)
Construct a Time in the indicated unit.
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
void(* Time)(Time oldValue, Time newValue)
TracedValue callback signature for Time.
bool CompareRoutesBoth(const DsrRouteCacheEntry &a, const DsrRouteCacheEntry &b)
bool CompareRoutesExpire(const DsrRouteCacheEntry &a, const DsrRouteCacheEntry &b)
bool CompareRoutesHops(const DsrRouteCacheEntry &a, const DsrRouteCacheEntry &b)
std::list< DsrRouteCacheEntry >::value_type route_pair
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
bool operator()(const DsrRouteCache::Neighbor &nb) const
Check if the entry is expired.
Structure to manage neighbor state.
Time m_expireTime
route expire time
bool close
is route active
The route cache structure.
void Print() const
Print function.
Ipv4Address m_low
low IP address
Ipv4Address m_high
high IP address