14#include "ns3/boolean.h"
17#include "ns3/net-device.h"
19#include "ns3/object.h"
20#include "ns3/packet.h"
21#include "ns3/simulator.h"
38 TypeId(
"ns3::Ipv4GlobalRouting")
40 .SetGroupName(
"Internet")
41 .template AddConstructor<GlobalRouting<T>>()
44 "Set to true if packets are randomly routed among ECMP; set to false for "
45 "using only one route consistently",
50 "RespondToInterfaceEvents",
51 "Set to true if you want to dynamically recompute the global routes upon "
52 "Interface notification events (up/down, or add/remove address)",
61 TypeId(
"ns3::Ipv6GlobalRouting")
63 .SetGroupName(
"Internet")
64 .template AddConstructor<GlobalRouting<T>>()
67 "Set to true if packets are randomly routed among ECMP; set to false for "
68 "using only one route consistently",
73 "RespondToInterfaceEvents",
74 "Set to true if you want to dynamically recompute the global routes upon "
75 "Interface notification events (up/down, or add/remove address)",
105 *route = IpRoutingTableEntry::CreateHostRouteTo(dest, nextHop, interface);
108 if (*routePointer == *route)
124 *route = IpRoutingTableEntry::CreateHostRouteTo(dest, interface);
127 if (*routePointer == *route)
144 NS_LOG_FUNCTION(
this << network << networkMask << nextHop << interface);
146 *route = IpRoutingTableEntry::CreateNetworkRouteTo(network, networkMask, nextHop, interface);
149 if (*routePointer == *route)
167 *route = IpRoutingTableEntry::CreateNetworkRouteTo(network, networkMask, interface);
170 if (*routePointer == *route)
187 NS_LOG_FUNCTION(
this << network << networkMask << nextHop << interface);
189 *route = IpRoutingTableEntry::CreateNetworkRouteTo(network, networkMask, nextHop, interface);
192 if (*routePointer == *route)
207 NS_LOG_LOGIC(
"Looking for route for destination " << dest);
210 typedef std::vector<IpRoutingTableEntry*> RouteVec_t;
211 RouteVec_t allRoutes;
217 if ((*i)->GetDest() == dest)
221 if (oif !=
m_ip->GetNetDevice((*i)->GetInterface()))
227 allRoutes.push_back(*i);
228 NS_LOG_LOGIC(allRoutes.size() <<
"Found global host route" << *i);
231 if (allRoutes.empty())
235 uint16_t longest_mask = 0;
241 mask = (*j)->GetDestNetworkMask();
245 mask = (*j)->GetDestNetworkPrefix();
247 uint16_t masklen = mask.GetPrefixLength();
249 IpAddress entry = (*j)->GetDestNetwork();
250 if (mask.IsMatch(dest, entry))
254 if (oif !=
m_ip->GetNetDevice((*j)->GetInterface()))
260 NS_LOG_LOGIC(allRoutes.size() <<
"Found global network route" << *j);
261 if (masklen < longest_mask)
266 else if (masklen == longest_mask)
268 NS_LOG_LOGIC(
"Equal mask length, adding this to the list");
269 allRoutes.push_back(*j);
273 NS_LOG_LOGIC(
"Longer mask length found, clearing the list and adding");
275 allRoutes.push_back(*j);
280 if (allRoutes.empty())
287 mask = (*k)->GetDestNetworkMask();
291 mask = (*k)->GetDestNetworkPrefix();
294 IpAddress entry = (*k)->GetDestNetwork();
295 if (mask.IsMatch(dest, entry))
300 if (oif !=
m_ip->GetNetDevice((*k)->GetInterface()))
306 allRoutes.push_back(*k);
311 if (!allRoutes.empty())
319 selectIndex =
m_rand->GetInteger(0, allRoutes.size() - 1);
328 rtentry->SetDestination(route->GetDest());
332 rtentry->SetSource(
m_ip->GetAddress(route->GetInterface(), 0).GetAddress());
336 rtentry->SetSource(
m_ip->GetAddress(route->GetInterface(), 1).GetAddress());
338 rtentry->SetGateway(route->GetGateway());
339 uint32_t interfaceIdx = route->GetInterface();
340 rtentry->SetOutputDevice(
m_ip->GetNetDevice(interfaceIdx));
422 << index <<
"; host route remaining size = " <<
m_hostRoutes.size());
438 << index <<
"; network route remaining size = " <<
m_networkRoutes.size());
453 << index <<
"; network route remaining size = " <<
m_networkRoutes.size());
466 m_rand->SetStream(stream);
489 IpRoutingProtocol::DoDispose();
498 std::ostream* os = stream->GetStream();
500 std::ios oldState(
nullptr);
501 oldState.copyfmt(*os);
503 *os << std::resetiosflags(std::ios::adjustfield) << std::setiosflags(std::ios::left);
515 *os <<
"Node: " <<
m_ip->template GetObject<Node>()->GetId() <<
", Time: " <<
Now().
As(unit)
516 <<
", Local time: " <<
m_ip->template GetObject<Node>()->GetLocalTime().As(unit) <<
", "
517 << name <<
"GlobalRouting table" << std::endl;
521 *os <<
"Destination Next Hop Flag Met Ref Use If"
525 std::ostringstream dest;
526 std::ostringstream gw;
527 std::ostringstream mask;
528 std::ostringstream flags;
532 dest << route.GetDest();
533 *os << std::setw(16) << dest.str();
534 gw << route.GetGateway();
535 *os << std::setw(16) << gw.str();
536 mask << route.GetDestNetworkMask();
537 *os << std::setw(16) << mask.str();
542 dest << route.GetDest() <<
"/"
543 << int(route.GetDestNetworkPrefix().GetPrefixLength());
544 *os << std::setw(31) << dest.str();
545 gw << route.GetGateway();
546 *os << std::setw(27) << gw.str();
553 else if (route.IsGateway())
557 *os << std::setw(6) << flags.str();
573 *os << route.GetInterface();
580 (*os).copyfmt(oldState);
595 if (header.GetDestination().IsMulticast())
597 NS_LOG_LOGIC(
"Multicast destination-- returning false");
626 NS_LOG_FUNCTION(
this << p << header << header.GetSource() << header.GetDestination() << idev
633 if (
m_ip->IsDestinationAddress(header.GetDestination(), iif))
637 NS_LOG_LOGIC(
"Local delivery to " << header.GetDestination());
658 if (!
m_ip->IsForwarding(iif))
665 NS_LOG_LOGIC(
"Unicast destination- looking up global route");
669 NS_LOG_LOGIC(
"Found unicast destination- calling unicast callback");
672 ucb(rtentry, p, header);
676 ucb(idev, rtentry, p, header);
682 NS_LOG_LOGIC(
"Did not find unicast destination- returning false");
AttributeValue implementation for Boolean.
bool IsNull() const
Check for null implementation.
static void BuildGlobalRoutingDatabase()
Build the routing database by gathering Link State Advertisements from each node exporting a GlobalRo...
static void InitializeRoutes()
Compute routes using a Dijkstra SPF computation and populate per-node forwarding tables.
static void DeleteGlobalRoutes()
Delete all static routes on all nodes that have a GlobalRouterInterface.
Global routing protocol for IPv4 stacks.
Ptr< IpRoute > LookupGlobal(IpAddress dest, Ptr< NetDevice > oif=nullptr)
Lookup in the forwarding table for destination.
void PrintRoutingTable(Ptr< OutputStreamWrapper > stream, Time::Unit unit=Time::S) const
Print the Routing Table entries.
void NotifyRemoveAddress(uint32_t interface, IpInterfaceAddress address)
static TypeId GetTypeId()
Get the type ID.
void AddHostRouteTo(IpAddress dest, IpAddress nextHop, uint32_t interface)
Add a host route to the global routing table.
virtual void SetIpv6(Ptr< Ip > ipv6)
Typically, invoked directly or indirectly from ns3::Ipv6::SetRoutingProtocol.
typename std::conditional_t< IsIpv4, Ipv4Address, Ipv6Address > IpAddress
Alias for Ipv4Address and Ipv6Address classes.
uint32_t GetNRoutes() const
Get the number of individual unicast routes that have been added to the routing table.
ASExternalRoutes m_ASexternalRoutes
External routes imported.
std::conditional_t< IsIpv4, MulticastForwardCallbackv4, MulticastForwardCallbackv6 > MulticastForwardCallback
Callback for multicast packets to be forwarded.
void NotifyInterfaceDown(uint32_t interface)
virtual void NotifyAddRoute(Ipv6Address dst, Ipv6Prefix mask, Ipv6Address nextHop, uint32_t interface, Ipv6Address prefixToUse=Ipv6Address::GetZero())
Notify a new route.
Callback< void, Ptr< const Packet >, const IpHeader &, uint32_t > LocalDeliverCallback
Callback for packets to be locally delivered.
void NotifyAddAddress(uint32_t interface, IpInterfaceAddress address)
typename std::conditional_t< IsIpv4, Ipv4Header, Ipv6Header > IpHeader
Alias for Ipv4Header and Ipv6Header classes.
void NotifyInterfaceUp(uint32_t interface)
HostRoutes m_hostRoutes
Routes to hosts.
Ptr< UniformRandomVariable > m_rand
A uniform random number generator for randomly routing packets among ECMP.
typename std::conditional_t< IsIpv4, Ipv4Mask, Ipv6Prefix > IpMaskOrPrefix
Alias for Ipv4Mask And Ipv6Prefix.
virtual void NotifyRemoveRoute(Ipv6Address dst, Ipv6Prefix mask, Ipv6Address nextHop, uint32_t interface, Ipv6Address prefixToUse=Ipv6Address::GetZero())
Notify route removing.
bool RouteInput(Ptr< const Packet > p, const IpHeader &header, Ptr< const NetDevice > idev, const UnicastForwardCallback &ucb, const MulticastForwardCallback &mcb, const LocalDeliverCallback &lcb, const ErrorCallback &ecb)
Route an input packet (to be forwarded or locally delivered).
static constexpr bool IsIpv4
Alias for determining whether the parent is Ipv4RoutingHelper or Ipv6RoutingHelper.
std::conditional_t< IsIpv4, UnicastForwardCallbackv4, UnicastForwardCallbackv6 > UnicastForwardCallback
Callback for unicast packets to be forwarded.
typename std::conditional_t< IsIpv4, Ipv4RoutingTableEntry, Ipv6RoutingTableEntry > IpRoutingTableEntry
Alias for Ipv4RoutingTableEntry and Ipv6RoutingTableEntry classes.
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model.
void RemoveRoute(uint32_t i)
Remove a route from the global unicast routing table.
typename std::conditional_t< IsIpv4, Ipv4InterfaceAddress, Ipv6InterfaceAddress > IpInterfaceAddress
Alias for Ipv4InterfaceAddress and Ipv6InterfaceAddress classes.
bool m_randomEcmpRouting
Set to true if packets are randomly routed among ECMP; set to false for using only one route consiste...
GlobalRouting()
Construct an empty Ipv4GlobalRouting routing protocol,.
Callback< void, Ptr< const Packet >, const IpHeader &, Socket::SocketErrno > ErrorCallback
Callback for routing errors (e.g., no route found).
Ptr< IpRoute > RouteOutput(Ptr< Packet > p, const IpHeader &header, Ptr< NetDevice > oif, Socket::SocketErrno &sockerr)
Query routing cache for an existing route, for an outbound packet.
void AddNetworkRouteTo(IpAddress network, IpMaskOrPrefix networkMask, IpAddress nextHop, uint32_t interface)
Add a network route to the global routing table.
virtual void SetIpv4(Ptr< Ip > ipv4)
Typically, invoked directly or indirectly from ns3::Ipv4::SetRoutingProtocol.
bool m_respondToInterfaceEvents
Set to true if this interface should respond to interface events by globally recomputing routes.
NetworkRoutes m_networkRoutes
Routes to networks.
Ptr< Ip > m_ip
associated IPv4 instance
void AddASExternalRouteTo(IpAddress network, IpMaskOrPrefix networkMask, IpAddress nextHop, uint32_t interface)
Add an external route to the global routing table.
IpRoutingTableEntry * GetRoute(uint32_t i) const
Get a route from the global unicast routing table.
Abstract base class for IPv4 routing protocols.
Describes an IPv6 address.
Describes an IPv6 prefix.
Abstract base class for IPv6 routing protocols.
static std::string FindName(Ptr< Object > object)
Given a pointer to an object, look to see if that object has a name associated with it and,...
Smart pointer class similar to boost::intrusive_ptr.
static Time Now()
Return the current simulation virtual time.
SocketErrno
Enumeration of the possible errors returned by a socket.
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.
a unique identifier for an interface.
TypeId AddAttribute(std::string name, std::string help, const AttributeValue &initialValue, Ptr< const AttributeAccessor > accessor, Ptr< const AttributeChecker > checker, SupportLevel supportLevel=SupportLevel::SUPPORTED, const std::string &supportMsg="")
Record in this TypeId the fact that a new attribute exists.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
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.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
#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_TEMPLATE_CLASS_DEFINE(type, param)
Explicitly instantiate a template class with one template parameter and register the resulting instan...
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.
Every class exported by the ns3 library is enclosed in the ns3 namespace.