24#include "ns3/boolean.h"
27#include "ns3/net-device.h"
29#include "ns3/object.h"
30#include "ns3/packet.h"
31#include "ns3/simulator.h"
47 TypeId(
"ns3::Ipv4GlobalRouting")
49 .SetGroupName(
"Internet")
50 .AddAttribute(
"RandomEcmpRouting",
51 "Set to true if packets are randomly routed among ECMP; set to false for "
52 "using only one route consistently",
56 .AddAttribute(
"RespondToInterfaceEvents",
57 "Set to true if you want to dynamically recompute the global routes upon "
58 "Interface notification events (up/down, or add/remove address)",
66 : m_randomEcmpRouting(false),
67 m_respondToInterfaceEvents(false)
71 m_rand = CreateObject<UniformRandomVariable>();
103 NS_LOG_FUNCTION(
this << network << networkMask << nextHop << interface);
124 NS_LOG_FUNCTION(
this << network << networkMask << nextHop << interface);
134 NS_LOG_LOGIC(
"Looking for route for destination " << dest);
137 typedef std::vector<Ipv4RoutingTableEntry*> RouteVec_t;
138 RouteVec_t allRoutes;
144 if ((*i)->GetDest() == dest)
148 if (oif !=
m_ipv4->GetNetDevice((*i)->GetInterface()))
154 allRoutes.push_back(*i);
155 NS_LOG_LOGIC(allRoutes.size() <<
"Found global host route" << *i);
158 if (allRoutes.empty())
163 Ipv4Mask mask = (*j)->GetDestNetworkMask();
169 if (oif !=
m_ipv4->GetNetDevice((*j)->GetInterface()))
175 allRoutes.push_back(*j);
176 NS_LOG_LOGIC(allRoutes.size() <<
"Found global network route" << *j);
180 if (allRoutes.empty())
184 Ipv4Mask mask = (*k)->GetDestNetworkMask();
191 if (oif !=
m_ipv4->GetNetDevice((*k)->GetInterface()))
197 allRoutes.push_back(*k);
202 if (!allRoutes.empty())
218 rtentry = Create<Ipv4Route>();
219 rtentry->SetDestination(route->
GetDest());
224 rtentry->SetOutputDevice(
m_ipv4->GetNetDevice(interfaceIdx));
303 << index <<
"; host route remaining size = " <<
m_hostRoutes.size());
319 << index <<
"; network route remaining size = " <<
m_networkRoutes.size());
334 << index <<
"; network route remaining size = " <<
m_networkRoutes.size());
377 std::ostream* os = stream->GetStream();
379 std::ios oldState(
nullptr);
380 oldState.copyfmt(*os);
382 *os << std::resetiosflags(std::ios::adjustfield) << std::setiosflags(std::ios::left);
384 *os <<
"Node: " <<
m_ipv4->GetObject<
Node>()->GetId() <<
", Time: " <<
Now().
As(unit)
385 <<
", Local time: " <<
m_ipv4->GetObject<
Node>()->GetLocalTime().As(unit)
386 <<
", Ipv4GlobalRouting table" << std::endl;
390 *os <<
"Destination Gateway Genmask Flags Metric Ref Use Iface"
394 std::ostringstream dest;
395 std::ostringstream gw;
396 std::ostringstream mask;
397 std::ostringstream flags;
400 *os << std::setw(16) << dest.str();
402 *os << std::setw(16) << gw.str();
404 *os << std::setw(16) << mask.str();
414 *os << std::setw(6) << flags.str();
437 (*os).copyfmt(oldState);
453 NS_LOG_LOGIC(
"Multicast destination-- returning false");
507 if (!
m_ipv4->IsForwarding(iif))
514 NS_LOG_LOGIC(
"Unicast destination- looking up global route");
518 NS_LOG_LOGIC(
"Found unicast destination- calling unicast callback");
519 ucb(rtentry, p, header);
524 NS_LOG_LOGIC(
"Did not find unicast destination- returning false");
AttributeValue implementation for Boolean.
bool IsNull() const
Check for null implementation.
static void DeleteGlobalRoutes()
Delete all static routes on all nodes that have a GlobalRouterInterface.
static void InitializeRoutes()
Compute routes using a Dijkstra SPF computation and populate per-node forwarding tables.
static void BuildGlobalRoutingDatabase()
Build the routing database by gathering Link State Advertisements from each node exporting a GlobalRo...
Ipv4 addresses are stored in host order in this class.
void AddHostRouteTo(Ipv4Address dest, Ipv4Address nextHop, uint32_t interface)
Add a host route to the global routing table.
std::list< Ipv4RoutingTableEntry * >::iterator HostRoutesI
iterator of container of Ipv4RoutingTableEntry (routes to hosts)
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 an input packet (to be forwarded or locally delivered)
void AddASExternalRouteTo(Ipv4Address network, Ipv4Mask networkMask, Ipv4Address nextHop, uint32_t interface)
Add an external route to the global routing table.
Ipv4RoutingTableEntry * GetRoute(uint32_t i) const
Get a route from the global unicast routing table.
static TypeId GetTypeId()
Get the type ID.
void DoDispose() override
Destructor implementation.
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model.
Ptr< UniformRandomVariable > m_rand
A uniform random number generator for randomly routing packets among ECMP.
void RemoveRoute(uint32_t i)
Remove a route from the global unicast routing table.
void NotifyInterfaceDown(uint32_t interface) override
void NotifyInterfaceUp(uint32_t interface) override
void SetIpv4(Ptr< Ipv4 > ipv4) override
void PrintRoutingTable(Ptr< OutputStreamWrapper > stream, Time::Unit unit=Time::S) const override
Print the Routing Table entries.
uint32_t GetNRoutes() const
Get the number of individual unicast routes that have been added to the routing table.
std::list< Ipv4RoutingTableEntry * >::iterator ASExternalRoutesI
iterator of container of Ipv4RoutingTableEntry (routes to external AS)
Ipv4GlobalRouting()
Construct an empty Ipv4GlobalRouting routing protocol,.
std::list< Ipv4RoutingTableEntry * >::const_iterator NetworkRoutesCI
const iterator of container of Ipv4RoutingTableEntry (routes to networks)
~Ipv4GlobalRouting() override
Ptr< Ipv4 > m_ipv4
associated IPv4 instance
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.
void AddNetworkRouteTo(Ipv4Address network, Ipv4Mask networkMask, Ipv4Address nextHop, uint32_t interface)
Add a network route to the global routing table.
std::list< Ipv4RoutingTableEntry * >::const_iterator HostRoutesCI
const iterator of container of Ipv4RoutingTableEntry (routes to hosts)
void NotifyRemoveAddress(uint32_t interface, Ipv4InterfaceAddress address) override
std::list< Ipv4RoutingTableEntry * >::iterator NetworkRoutesI
iterator of container of Ipv4RoutingTableEntry (routes to networks)
ASExternalRoutes m_ASexternalRoutes
External routes imported.
HostRoutes m_hostRoutes
Routes to hosts.
bool m_respondToInterfaceEvents
Set to true if this interface should respond to interface events by globallly recomputing routes.
void NotifyAddAddress(uint32_t interface, Ipv4InterfaceAddress address) override
bool m_randomEcmpRouting
Set to true if packets are randomly routed among ECMP; set to false for using only one route consiste...
NetworkRoutes m_networkRoutes
Routes to networks.
std::list< Ipv4RoutingTableEntry * >::const_iterator ASExternalRoutesCI
const iterator of container of Ipv4RoutingTableEntry (routes to external AS)
Ptr< Ipv4Route > LookupGlobal(Ipv4Address dest, Ptr< NetDevice > oif=nullptr)
Lookup in the forwarding table for destination.
a class to store IPv4 address information on an interface
a class to represent an Ipv4 address mask
bool IsMatch(Ipv4Address a, Ipv4Address b) const
A record of an IPv4 routing table entry for Ipv4GlobalRouting and Ipv4StaticRouting.
Ipv4Address GetDest() const
Ipv4Address GetGateway() const
uint32_t GetInterface() const
static Ipv4RoutingTableEntry CreateNetworkRouteTo(Ipv4Address network, Ipv4Mask networkMask, Ipv4Address nextHop, uint32_t interface)
static Ipv4RoutingTableEntry CreateHostRouteTo(Ipv4Address dest, Ipv4Address nextHop, uint32_t interface)
Ipv4Mask GetDestNetworkMask() const
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,...
A base class which provides memory management and object aggregation.
virtual void DoDispose()
Destructor implementation.
Smart pointer class similar to boost::intrusive_ptr.
void SetStream(int64_t stream)
Specifies the stream number for the RngStream.
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 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 AttributeAccessor > MakeBooleanAccessor(T1 a1)
Ptr< const AttributeChecker > MakeBooleanChecker()
#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 ",...
#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.
Every class exported by the ns3 library is enclosed in the ns3 namespace.