23#include "ns3/boolean.h"
24#include "ns3/double.h"
25#include "ns3/inet-socket-address.h"
27#include "ns3/trace-source-accessor.h"
28#include "ns3/udp-socket-factory.h"
29#include "ns3/uinteger.h"
67 static TypeId tid =
TypeId(
"ns3::dsdv::DeferredRouteOutputTag")
94 void Print(std::ostream& os)
const override
96 os <<
"DeferredRouteOutputTag: output interface = " <<
oif;
104 TypeId(
"ns3::dsdv::RoutingProtocol")
106 .SetGroupName(
"Dsdv")
108 .AddAttribute(
"PeriodicUpdateInterval",
109 "Periodic interval between exchange of full routing tables among nodes.",
113 .AddAttribute(
"SettlingTime",
114 "Minimum time an update is to be stored in adv table before sending out "
115 "in case of change in metric (in seconds)",
119 .AddAttribute(
"MaxQueueLen",
120 "Maximum number of packets that we allow a routing protocol to buffer.",
124 .AddAttribute(
"MaxQueuedPacketsPerDst",
125 "Maximum number of packets that we allow per destination to buffer.",
129 .AddAttribute(
"MaxQueueTime",
130 "Maximum time packets can be queued (in seconds)",
136 "Enables buffering of data packets if no route to destination is available",
143 "Enables Weighted Settling Time for the updates before advertising",
147 .AddAttribute(
"Holdtimes",
148 "Times the forwarding Interval to purge the route.",
154 "WeightedFactor for the settling time if Weighted Settling Time is enabled",
158 .AddAttribute(
"EnableRouteAggregation",
159 "Enables Weighted Settling Time for the updates before advertising",
164 .AddAttribute(
"RouteAggregationTime",
165 "Time to aggregate updates before sending them out (in seconds)",
220 m_periodicUpdateTimer(
Timer::CANCEL_ON_DESTROY)
235 iter->first->Close();
245 <<
", Time: " <<
Now().
As(unit)
247 <<
", DSDV Routing table" << std::endl;
250 *stream->GetStream() << std::endl;
286 std::map<Ipv4Address, RoutingTableEntry> removedAddresses;
290 NS_LOG_DEBUG(
"Packet Size: " << p->GetSize() <<
", Packet id: " << p->GetUid()
291 <<
", Destination address in Packet: " << dst);
294 for (
auto rmItr = removedAddresses.begin(); rmItr != removedAddresses.end(); ++rmItr)
296 rmItr->second.SetEntriesChanged(
true);
297 rmItr->second.SetSeqNo(rmItr->second.GetSeqNo() + 1);
300 if (!removedAddresses.empty())
316 NS_LOG_DEBUG(
"A route exists from " << route->GetSource()
317 <<
" to neighboring destination "
318 << route->GetDestination());
319 if (oif && route->GetOutputDevice() != oif)
334 NS_LOG_DEBUG(
"A route exists from " << route->GetSource() <<
" to destination "
336 if (oif && route->GetOutputDevice() != oif)
351 if (!p->PeekPacketTag(tag))
353 p->AddPacketTag(tag);
371 NS_LOG_DEBUG(
"Added packet " << p->GetUid() <<
" to queue.");
385 << header.
GetSource() <<
" on interface " << idev->GetAddress()
410 if (p->PeekPacketTag(tag))
441 NS_LOG_ERROR(
"Unable to deliver packet locally due to null callback "
442 << p->GetUid() <<
" from " << origin);
452 ucb(route, packet, header);
456 NS_LOG_DEBUG(
"No route to forward. Drop packet " << p->GetUid());
473 NS_LOG_ERROR(
"Unable to deliver packet locally due to null callback "
474 << p->GetUid() <<
" from " << origin);
496 <<
" from " << header.
GetSource() <<
" via nexthop neighbor "
498 ucb(route, p, header);
502 NS_LOG_LOGIC(
"Drop packet " << p->GetUid() <<
" as there is no route to forward it.");
546 rt->SetSource(j->second.GetLocal());
550 rt->SetOutputDevice(
m_lo);
559 Ptr<Packet> packet = socket->RecvFrom(sourceAddress);
566 <<
" and packet id: " << packet->GetUid());
573 packet->RemoveHeader(dsdvHeader);
579 if (dsdvHeader.
GetDst() == interface.GetLocal())
583 NS_LOG_DEBUG(
"Sent Dsdv update back to the same Destination, "
584 "with infinite metric. Time left to send fwd update: "
590 NS_LOG_DEBUG(
"Received update for my address. Discarding this.");
600 << sender <<
" to " << receiver <<
". Details are: Destination: "
606 bool permanentTableVerifier =
608 if (!permanentTableVerifier)
631 NS_LOG_DEBUG(
"Discarding this update as this route is not present in "
632 "main routing table and received with infinite metric");
640 std::map<Ipv4Address, RoutingTableEntry> allRoutes;
642 for (
auto i = allRoutes.begin(); i != allRoutes.end(); ++i)
644 NS_LOG_DEBUG(
"ADV table routes are:" << i->second.GetDestination());
657 NS_LOG_DEBUG(
"Canceling the timer to update route with better seq number");
668 NS_LOG_DEBUG(
"Received update with better sequence number and changed "
669 "metric.Waiting for WST");
674 <<
" as there is no event running for this route");
694 NS_LOG_DEBUG(
"Route with better sequence number and same metric received. "
695 "Advertised without WST");
706 NS_LOG_DEBUG(
"Canceling any existing timer to update route with same "
708 "and better hop count");
720 <<
" as there is no current event running for this route");
747 NS_LOG_DEBUG(
"Received update with same seq number and "
748 "same/worst metric for, "
749 << dsdvHeader.
GetDst() <<
". Discarding the update.");
761 <<
" : Received update with old seq number. Discarding the update.");
767 <<
" from " << sender);
771 NS_LOG_DEBUG(
"Triggering an update for this unreachable route:");
772 std::map<Ipv4Address, RoutingTableEntry> dstsWithNextHopSrc;
779 for (
auto i = dstsWithNextHopSrc.begin(); i != dstsWithNextHopSrc.end(); ++i)
781 i->second.SetSeqNo(i->second.GetSeqNo() + 1);
782 i->second.SetEntriesChanged(
true);
794 "was received from a different neighbor "
795 "and I can reach the destination");
800 std::map<Ipv4Address, RoutingTableEntry> allRoutes;
818 std::map<Ipv4Address, RoutingTableEntry> allRoutes;
826 for (
auto i = allRoutes.begin(); i != allRoutes.end(); ++i)
828 NS_LOG_LOGIC(
"Destination: " << i->second.GetDestination()
829 <<
" SeqNo:" << i->second.GetSeqNo()
830 <<
" HopCount:" << i->second.GetHop() + 1);
832 if (i->second.GetEntriesChanged() &&
835 dsdvHeader.
SetDst(i->second.GetDestination());
845 packet->AddHeader(dsdvHeader);
847 NS_LOG_DEBUG(
"Deleted this route from the advertised table");
853 NS_LOG_DEBUG(
"EventID " << event.GetUid() <<
" associated with "
855 <<
" has not expired, waiting in adv table");
858 if (packet->GetSize() >= 12)
866 packet->AddHeader(dsdvHeader);
879 << dsdvHeader.
GetDst() <<
" with packet id : " << packet->GetUid()
880 <<
" and packet Size: " << packet->GetSize());
884 NS_LOG_FUNCTION(
"Update not sent as there are no updates to be triggered");
892 std::map<Ipv4Address, RoutingTableEntry> removedAddresses;
893 std::map<Ipv4Address, RoutingTableEntry> allRoutes;
897 if (allRoutes.empty())
907 for (
auto i = allRoutes.begin(); i != allRoutes.end(); ++i)
910 if (i->second.GetHop() == 0)
919 packet->AddHeader(dsdvHeader);
923 dsdvHeader.
SetDst(i->second.GetDestination());
926 packet->AddHeader(dsdvHeader);
932 <<
", LifeTime: " << i->second.GetLifeTime().As(
Time::S));
934 for (
auto rmItr = removedAddresses.begin(); rmItr != removedAddresses.end(); ++rmItr)
937 removedHeader.
SetDst(rmItr->second.GetDestination());
938 removedHeader.
SetDstSeqno(rmItr->second.GetSeqNo() + 1);
939 removedHeader.
SetHopCount(rmItr->second.GetHop() + 1);
940 packet->AddHeader(removedHeader);
941 NS_LOG_DEBUG(
"Update for removed record is: Destination: "
945 socket->Send(packet);
957 NS_LOG_FUNCTION(
"PeriodicUpdate Packet UID is : " << packet->GetUid());
1003 socket->BindToNetDevice(l3->GetNetDevice(i));
1005 socket->SetAllowBroadcast(
true);
1011 iface.GetBroadcast(),
1015 iface.GetBroadcast(),
1047 NS_LOG_FUNCTION(
this <<
" interface " << i <<
" address " << address);
1065 socket->BindToNetDevice(l3->GetNetDevice(i));
1067 socket->SetAllowBroadcast(
true);
1089 if (l3->GetNAddresses(i))
1099 socket->SetAllowBroadcast(
true);
1142 std::map<Ipv4Address, RoutingTableEntry> allRoutes;
1144 for (
auto i = allRoutes.begin(); i != allRoutes.end(); ++i)
1154 NS_LOG_LOGIC(
"A route exists from " << route->GetSource()
1155 <<
" to neighboring destination "
1156 << route->GetDestination());
1164 NS_LOG_LOGIC(
"A route exists from " << route->GetSource() <<
" to destination "
1165 << route->GetDestination() <<
" via "
1182 if (p->RemovePacketTag(tag))
1195 ucb(route, p, header);
1228 return weightedTime;
1238 "Merging advertised table changes with main table before sending out periodic update");
1239 std::map<Ipv4Address, RoutingTableEntry> allRoutes;
1241 if (!allRoutes.empty())
1243 for (
auto i = allRoutes.begin(); i != allRoutes.end(); ++i)
1255 <<
" with main routing Table");
1261 NS_LOG_DEBUG(
"Event currently running. Cannot Merge Routing Tables");
a polymophic address class
AttributeValue implementation for Boolean.
bool IsNull() const
Check for null implementation.
This class can be used to hold variables of floating point type such as 'double' or 'float'.
An identifier for simulation events.
Ipv4Address GetIpv4() const
static InetSocketAddress ConvertFrom(const Address &address)
Returns an InetSocketAddress which corresponds to the input Address.
Ipv4 addresses are stored in host order in this class.
static Ipv4Address GetLoopback()
static Ipv4Address GetAny()
virtual int32_t GetInterfaceForAddress(Ipv4Address address) const =0
Return the interface number of the interface that has been assigned the specified IP address.
virtual bool IsForwarding(uint32_t interface) const =0
virtual Ipv4InterfaceAddress GetAddress(uint32_t interface, uint32_t addressIndex) const =0
Because addresses can be removed, the addressIndex is not guaranteed to be static across calls to thi...
virtual Ptr< NetDevice > GetNetDevice(uint32_t interface)=0
virtual bool IsDestinationAddress(Ipv4Address address, uint32_t iif) const =0
Determine whether address and interface corresponding to received packet can be accepted for local de...
virtual int32_t GetInterfaceForDevice(Ptr< const NetDevice > device) const =0
virtual uint32_t GetNInterfaces() const =0
a class to store IPv4 address information on an interface
Ipv4Mask GetMask() const
Get the network mask.
Ipv4Address GetLocal() const
Get the local address.
Ipv4Address GetBroadcast() const
Get the broadcast address.
Implement the IPv4 layer.
a class to represent an Ipv4 address mask
static Ipv4Mask GetOnes()
Abstract base class for IPv4 routing protocols.
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
virtual void DoDispose()
Destructor implementation.
Smart pointer class similar to boost::intrusive_ptr.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
static Time Now()
Return the current simulation virtual time.
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
static Time GetMaximumSimulationTime()
Get the maximum representable simulation time.
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
This method wraps the creation of sockets that is performed on a given node by a SocketFactory specif...
SocketErrno
Enumeration of the possible errors returned by a socket.
TAG_BUFFER_INLINE uint32_t ReadU32()
TAG_BUFFER_INLINE void WriteU32(uint32_t v)
tag a set of bytes in a packet
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.
Unit
The unit to use to interpret a number representing time.
AttributeValue implementation for Time.
A simple virtual Timer class.
Time GetDelayLeft() const
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.
static TypeId GetTypeId()
Get the type ID.
Hold an unsigned integer type.
bool Find(Ipv4Address dst)
Finds whether a packet with destination dst exists in the queue.
bool Enqueue(QueueEntry &entry)
Push entry in queue, if there is no entry with the same packet and destination address in queue.
void SetQueueTimeout(Time t)
Set queue timeout.
bool Dequeue(Ipv4Address dst, QueueEntry &entry)
Return first found (the earliest) entry for given destination.
void SetMaxPacketsPerDst(uint32_t len)
Set maximum packets per destination.
uint32_t GetSize()
Get the number of entries.
void SetMaxQueueLen(uint32_t len)
Set maximum queue length.
Ptr< const Packet > GetPacket() const
Get packet.
Ipv4Header GetIpv4Header() const
Get IP header.
UnicastForwardCallback GetUnicastForwardCallback() const
Get unicast forward callback function.
void SetIpv4(Ptr< Ipv4 > ipv4) override
void Send(Ptr< Ipv4Route > route, Ptr< const Packet > packet, const Ipv4Header &header)
Send a packet.
bool GetWSTFlag() const
Get weighted settling time (WST) flag.
static const uint32_t DSDV_PORT
UDP Port for DSDV control traffic.
Time m_periodicUpdateInterval
PeriodicUpdateInterval specifies the periodic time interval between which the a node broadcasts its e...
void Start()
Start protocol operation.
bool EnableBuffering
Flag that is used to enable or disable buffering.
uint32_t m_maxQueuedPacketsPerDst
The maximum number of packets that we allow per destination to buffer.
void SetEnableRAFlag(bool f)
Set enable route aggregation (RA) flag.
Ptr< UniformRandomVariable > m_uniformRandomVariable
Provides uniform random variables.
UnicastForwardCallback m_scb
Unicast callback for own packets.
void NotifyInterfaceUp(uint32_t interface) override
void SendPeriodicUpdate()
Broadcasts the entire routing table for every PeriodicUpdateInterval.
void DoDispose() override
Destructor implementation.
PacketQueue m_queue
A "drop front on full" queue used by the routing layer to buffer packets to which it does not have a ...
void SetEnableBufferFlag(bool f)
Set enable buffer flag.
~RoutingProtocol() override
bool RouteInput(Ptr< const Packet > p, const Ipv4Header &header, Ptr< const NetDevice > idev, const UnicastForwardCallback &ucb, const MulticastForwardCallback &mcb, const LocalDeliverCallback &lcb, const ErrorCallback &ecb) override
Route input packet.
void NotifyAddAddress(uint32_t interface, Ipv4InterfaceAddress address) override
void Drop(Ptr< const Packet > packet, const Ipv4Header &header, Socket::SocketErrno err)
Notify that packet is dropped for some reason.
void DeferredRouteOutput(Ptr< const Packet > p, const Ipv4Header &header, UnicastForwardCallback ucb, ErrorCallback ecb)
Queue packet until we find a route.
Ptr< NetDevice > m_lo
Loopback device used to defer route requests until a route is found.
bool GetEnableBufferFlag() const
Get enable buffer flag.
void SetWSTFlag(bool f)
Set weighted settling time (WST) flag.
Time GetSettlingTime(Ipv4Address dst)
Get settlingTime for a destination.
Timer m_periodicUpdateTimer
Timer to trigger periodic updates from a node.
Time m_routeAggregationTime
Parameter that holds the route aggregation time interval.
bool EnableRouteAggregation
This is a flag to enable route aggregation.
void SendPacketFromQueue(Ipv4Address dst, Ptr< Ipv4Route > route)
Send packet from queue.
Ptr< Ipv4 > m_ipv4
IP protocol.
void NotifyRemoveAddress(uint32_t interface, Ipv4InterfaceAddress address) override
ErrorCallback m_ecb
Error callback for own packets.
Time m_maxQueueTime
The maximum period of time that a routing protocol is allowed to buffer a packet for.
std::map< Ptr< Socket >, Ipv4InterfaceAddress > m_socketAddresses
Raw socket per each IP interface, map socket -> iface address (IP + mask)
Ptr< Ipv4Route > LoopbackRoute(const Ipv4Header &header, Ptr< NetDevice > oif) const
Create loopback route for given header.
void NotifyInterfaceDown(uint32_t interface) override
static TypeId GetTypeId()
Get the type ID.
bool GetEnableRAFlag() const
Get enable route aggregation (RA) flag.
bool EnableWST
Flag that is used to enable or disable Weighted Settling Time.
Time m_settlingTime
SettlingTime specifies the time for which a node waits before propagating an update.
Ptr< Socket > FindSocketWithInterfaceAddress(Ipv4InterfaceAddress iface) const
Find socket with local interface address iface.
RoutingTable m_routingTable
Main Routing table for the node.
uint32_t Holdtimes
Holdtimes is the multiplicative factor of PeriodicUpdateInterval for which the node waits since the l...
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model.
void RecvDsdv(Ptr< Socket > socket)
Receive and process dsdv control packet.
void LookForQueuedPackets()
Look for any queued packets to send them out.
void SendTriggeredUpdate()
Sends trigger update from a node.
RoutingTable m_advRoutingTable
Advertised Routing table for the node.
Ptr< Ipv4Route > RouteOutput(Ptr< Packet > p, const Ipv4Header &header, Ptr< NetDevice > oif, Socket::SocketErrno &sockerr) override
Query routing cache for an existing route, for an outbound packet.
uint32_t m_maxQueueLen
The maximum number of packets that we allow a routing protocol to buffer.
Ipv4Address m_mainAddress
Nodes IP address.
void PrintRoutingTable(Ptr< OutputStreamWrapper > stream, Time::Unit unit=Time::S) const override
Print the Routing Table entries.
void MergeTriggerPeriodicUpdates()
Merge periodic updates.
double m_weightedFactor
This is the weighted factor to determine the weighted settling time.
void SetHop(uint32_t hopCount)
Set hop.
void SetLifeTime(Time lifeTime)
Set lifetime.
bool GetEntriesChanged() const
Get entries changed.
void SetEntriesChanged(bool entriesChanged)
Set entries changed indicator.
Time GetSettlingTime() const
Get settling time.
Ipv4Address GetDestination() const
Get destination IP address.
void SetSettlingTime(Time settlingTime)
Set settling time.
Ptr< Ipv4Route > GetRoute() const
Get route.
void SetNextHop(Ipv4Address nextHop)
Set next hop.
uint32_t GetSeqNo() const
Get sequence number.
Time GetLifeTime() const
Get lifetime.
void SetFlag(RouteFlags flag)
Set route flags.
uint32_t GetHop() const
Get hop.
Ipv4Address GetNextHop() const
Get next hop.
void SetSeqNo(uint32_t sequenceNumber)
Set sequence number.
bool LookupRoute(Ipv4Address dst, RoutingTableEntry &rt)
Lookup routing table entry with destination address dst.
bool DeleteRoute(Ipv4Address dst)
Delete routing table entry with destination address dst, if it exists.
bool ForceDeleteIpv4Event(Ipv4Address address)
Force delete an update waiting for settling time to complete as a better update to same destination w...
void Clear()
Delete all entries from routing table.
bool AddRoute(RoutingTableEntry &r)
Add routing table entry if it doesn't yet exist in routing table.
void Setholddowntime(Time t)
Set hold down time (time until an invalid route may be deleted)
bool DeleteIpv4Event(Ipv4Address address)
Clear up the entry from the map after the event is completed.
bool Update(RoutingTableEntry &rt)
Updating the routing Table with routing table entry rt.
void GetListOfDestinationWithNextHop(Ipv4Address nxtHp, std::map< Ipv4Address, RoutingTableEntry > &dstList)
Lookup list of addresses for which nxtHp is the next Hop address.
void Print(Ptr< OutputStreamWrapper > stream, Time::Unit unit=Time::S) const
Print routing table.
void DeleteAllRoutesFromInterface(Ipv4InterfaceAddress iface)
Delete all route from interface with address iface.
EventId GetEventId(Ipv4Address address)
Get the EventId associated with that address.
bool AddIpv4Event(Ipv4Address address, EventId id)
Add an event for a destination address so that the update to for that destination is sent after the e...
bool AnyRunningEvent(Ipv4Address address)
Force delete an update waiting for settling time to complete as a better update to same destination w...
void Purge(std::map< Ipv4Address, RoutingTableEntry > &removedAddresses)
Delete all outdated entries if Lifetime is expired.
void GetListOfAllRoutes(std::map< Ipv4Address, RoutingTableEntry > &allRoutes)
Lookup list of all addresses in the routing table.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Ptr< const AttributeChecker > MakeBooleanChecker()
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Ptr< const AttributeChecker > MakeDoubleChecker()
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Ptr< const AttributeChecker > MakeTimeChecker()
Helper to make an unbounded Time checker.
Ptr< const AttributeChecker > MakeUintegerChecker()
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
#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(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Time Now()
create an ns3::Time instance which contains the current simulation time.
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Time Seconds(double value)
Construct a Time in the indicated unit.
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
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...
Ptr< T1 > ConstCast(const Ptr< T2 > &p)
Cast a Ptr.
Tag used by DSDV implementation.
uint32_t GetSerializedSize() const override
void Deserialize(TagBuffer i) override
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
void Serialize(TagBuffer i) const override
void Print(std::ostream &os) const override
int32_t oif
Positive if output device is fixed in RouteOutput.
static TypeId GetTypeId()
Get the type ID.
DeferredRouteOutputTag(int32_t o=-1)
Constructor.
static const uint32_t packetSize
Packet size generated at the AP.