A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ipv4-global-routing.h
Go to the documentation of this file.
1//
2// Copyright (c) 2008 University of Washington
3//
4// SPDX-License-Identifier: GPL-2.0-only
5//
6
7#ifndef IPV4_GLOBAL_ROUTING_H
8#define IPV4_GLOBAL_ROUTING_H
9
10#include "ipv4-header.h"
12#include "ipv4.h"
13
14#include "ns3/ipv4-address.h"
15#include "ns3/ptr.h"
16#include "ns3/random-variable-stream.h"
17
18#include <list>
19#include <stdint.h>
20
21namespace ns3
22{
23
24class Packet;
25class NetDevice;
26class Ipv4Interface;
27class Ipv4Address;
28class Ipv4Header;
31class Node;
33
34/**
35 * @ingroup ipv4
36 *
37 * @brief Global routing protocol for IPv4 stacks.
38 *
39 * In ns-3 we have the concept of a pluggable routing protocol. Routing
40 * protocols are added to a list maintained by the Ipv4L3Protocol. Every
41 * stack gets one routing protocol for free -- the Ipv4StaticRouting routing
42 * protocol is added in the constructor of the Ipv4L3Protocol (this is the
43 * piece of code that implements the functionality of the IP layer).
44 *
45 * As an option to running a dynamic routing protocol, a GlobalRouteManager
46 * object has been created to allow users to build routes for all participating
47 * nodes. One can think of this object as a "routing oracle"; it has
48 * an omniscient view of the topology, and can construct shortest path
49 * routes between all pairs of nodes. These routes must be stored
50 * somewhere in the node, so therefore this class Ipv4GlobalRouting
51 * is used as one of the pluggable routing protocols. It is kept distinct
52 * from Ipv4StaticRouting because these routes may be dynamically cleared
53 * and rebuilt in the middle of the simulation, while manually entered
54 * routes into the Ipv4StaticRouting may need to be kept distinct.
55 *
56 * This class deals with Ipv4 unicast routes only.
57 *
58 * @see Ipv4RoutingProtocol
59 * @see GlobalRouteManager
60 */
62{
64
65 public:
66 /**
67 * @brief Get the type ID.
68 * @return the object TypeId
69 */
70 static TypeId GetTypeId();
71 /**
72 * @brief Construct an empty Ipv4GlobalRouting routing protocol,
73 *
74 * The Ipv4GlobalRouting class supports host and network unicast routes.
75 * This method initializes the lists containing these routes to empty.
76 *
77 * @see Ipv4GlobalRouting
78 */
80 ~Ipv4GlobalRouting() override;
81
82 // These methods inherited from base class
84 const Ipv4Header& header,
86 Socket::SocketErrno& sockerr) override;
87
89 const Ipv4Header& header,
91 const UnicastForwardCallback& ucb,
92 const MulticastForwardCallback& mcb,
93 const LocalDeliverCallback& lcb,
94 const ErrorCallback& ecb) override;
95 void NotifyInterfaceUp(uint32_t interface) override;
96 void NotifyInterfaceDown(uint32_t interface) override;
97 void NotifyAddAddress(uint32_t interface, Ipv4InterfaceAddress address) override;
98 void NotifyRemoveAddress(uint32_t interface, Ipv4InterfaceAddress address) override;
99 void SetIpv4(Ptr<Ipv4> ipv4) override;
101 Time::Unit unit = Time::S) const override;
102
103 /**
104 * @brief Add a host route to the global routing table.
105 *
106 * @param dest The Ipv4Address destination for this route.
107 * @param nextHop The Ipv4Address of the next hop in the route.
108 * @param interface The network interface index used to send packets to the
109 * destination.
110 *
111 * @see Ipv4Address
112 */
113 void AddHostRouteTo(Ipv4Address dest, Ipv4Address nextHop, uint32_t interface);
114 /**
115 * @brief Add a host route to the global routing table.
116 *
117 * @param dest The Ipv4Address destination for this route.
118 * @param interface The network interface index used to send packets to the
119 * destination.
120 *
121 * @see Ipv4Address
122 */
123 void AddHostRouteTo(Ipv4Address dest, uint32_t interface);
124
125 /**
126 * @brief Add a network route to the global routing table.
127 *
128 * @param network The Ipv4Address network for this route.
129 * @param networkMask The Ipv4Mask to extract the network.
130 * @param nextHop The next hop in the route to the destination network.
131 * @param interface The network interface index used to send packets to the
132 * destination.
133 *
134 * @see Ipv4Address
135 */
136 void AddNetworkRouteTo(Ipv4Address network,
137 Ipv4Mask networkMask,
138 Ipv4Address nextHop,
139 uint32_t interface);
140
141 /**
142 * @brief Add a network route to the global routing table.
143 *
144 * @param network The Ipv4Address network for this route.
145 * @param networkMask The Ipv4Mask to extract the network.
146 * @param interface The network interface index used to send packets to the
147 * destination.
148 *
149 * @see Ipv4Address
150 */
151 void AddNetworkRouteTo(Ipv4Address network, Ipv4Mask networkMask, uint32_t interface);
152
153 /**
154 * @brief Add an external route to the global routing table.
155 *
156 * @param network The Ipv4Address network for this route.
157 * @param networkMask The Ipv4Mask to extract the network.
158 * @param nextHop The next hop Ipv4Address
159 * @param interface The network interface index used to send packets to the
160 * destination.
161 */
163 Ipv4Mask networkMask,
164 Ipv4Address nextHop,
165 uint32_t interface);
166
167 /**
168 * @brief Get the number of individual unicast routes that have been added
169 * to the routing table.
170 *
171 * @warning The default route counts as one of the routes.
172 * @returns the number of routes
173 */
174 uint32_t GetNRoutes() const;
175
176 /**
177 * @brief Get a route from the global unicast routing table.
178 *
179 * Externally, the unicast global routing table appears simply as a table with
180 * n entries. The one subtlety of note is that if a default route has been set
181 * it will appear as the zeroth entry in the table. This means that if you
182 * add only a default route, the table will have one entry that can be accessed
183 * either by explicitly calling GetDefaultRoute () or by calling GetRoute (0).
184 *
185 * Similarly, if the default route has been set, calling RemoveRoute (0) will
186 * remove the default route.
187 *
188 * @param i The index (into the routing table) of the route to retrieve. If
189 * the default route has been set, it will occupy index zero.
190 * @return If route is set, a pointer to that Ipv4RoutingTableEntry is returned, otherwise
191 * a zero pointer is returned.
192 *
193 * @see Ipv4RoutingTableEntry
194 * @see Ipv4GlobalRouting::RemoveRoute
195 */
197
198 /**
199 * @brief Remove a route from the global unicast routing table.
200 *
201 * Externally, the unicast global routing table appears simply as a table with
202 * n entries. The one subtlety of note is that if a default route has been set
203 * it will appear as the zeroth entry in the table. This means that if the
204 * default route has been set, calling RemoveRoute (0) will remove the
205 * default route.
206 *
207 * @param i The index (into the routing table) of the route to remove. If
208 * the default route has been set, it will occupy index zero.
209 *
210 * @see Ipv4RoutingTableEntry
211 * @see Ipv4GlobalRouting::GetRoute
212 * @see Ipv4GlobalRouting::AddRoute
213 */
214 void RemoveRoute(uint32_t i);
215
216 /**
217 * Assign a fixed random variable stream number to the random variables
218 * used by this model. Return the number of streams (possibly zero) that
219 * have been assigned.
220 *
221 * @param stream first stream index to use
222 * @return the number of stream indices assigned by this model
223 */
224 int64_t AssignStreams(int64_t stream);
225
226 protected:
227 void DoDispose() override;
228
229 private:
230 /// Set to true if packets are randomly routed among ECMP; set to false for using only one route
231 /// consistently
233 /// Set to true if this interface should respond to interface events by globally recomputing
234 /// routes
236 /// A uniform random number generator for randomly routing packets among ECMP
238
239 /// container of Ipv4RoutingTableEntry (routes to hosts)
240 typedef std::list<Ipv4RoutingTableEntry*> HostRoutes;
241 /// const iterator of container of Ipv4RoutingTableEntry (routes to hosts)
242 typedef std::list<Ipv4RoutingTableEntry*>::const_iterator HostRoutesCI;
243 /// iterator of container of Ipv4RoutingTableEntry (routes to hosts)
244 typedef std::list<Ipv4RoutingTableEntry*>::iterator HostRoutesI;
245
246 /// container of Ipv4RoutingTableEntry (routes to networks)
247 typedef std::list<Ipv4RoutingTableEntry*> NetworkRoutes;
248 /// const iterator of container of Ipv4RoutingTableEntry (routes to networks)
249 typedef std::list<Ipv4RoutingTableEntry*>::const_iterator NetworkRoutesCI;
250 /// iterator of container of Ipv4RoutingTableEntry (routes to networks)
251 typedef std::list<Ipv4RoutingTableEntry*>::iterator NetworkRoutesI;
252
253 /// container of Ipv4RoutingTableEntry (routes to external AS)
254 typedef std::list<Ipv4RoutingTableEntry*> ASExternalRoutes;
255 /// const iterator of container of Ipv4RoutingTableEntry (routes to external AS)
256 typedef std::list<Ipv4RoutingTableEntry*>::const_iterator ASExternalRoutesCI;
257 /// iterator of container of Ipv4RoutingTableEntry (routes to external AS)
258 typedef std::list<Ipv4RoutingTableEntry*>::iterator ASExternalRoutesI;
259
260 /**
261 * @brief Lookup in the forwarding table for destination.
262 * @param dest destination address
263 * @param oif output interface if any (put 0 otherwise)
264 * @return Ipv4Route to route the packet to reach dest address
265 */
267
268 HostRoutes m_hostRoutes; //!< Routes to hosts
269 NetworkRoutes m_networkRoutes; //!< Routes to networks
270 ASExternalRoutes m_ASexternalRoutes; //!< External routes imported
271
272 Ptr<Ipv4> m_ipv4; //!< associated IPv4 instance
273};
274
275} // Namespace ns3
276
277#endif /* IPV4_GLOBAL_ROUTING_H */
A global router implementation.
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
std::list< Ipv4RoutingTableEntry * > HostRoutes
container of Ipv4RoutingTableEntry (routes to hosts)
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.
std::list< Ipv4RoutingTableEntry * > ASExternalRoutes
container of Ipv4RoutingTableEntry (routes to external AS)
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)
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 * > NetworkRoutes
container of Ipv4RoutingTableEntry (routes to networks)
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 globally 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.
Packet header for IPv4.
Definition ipv4-header.h:23
a class to store IPv4 address information on an interface
The IPv4 representation of a network interface.
a class to represent an Ipv4 address mask
A record of an IPv4 multicast route for Ipv4GlobalRouting and Ipv4StaticRouting.
Abstract base class for IPv4 routing protocols.
Callback< void, Ptr< Ipv4MulticastRoute >, Ptr< const Packet >, const Ipv4Header & > MulticastForwardCallback
Callback for multicast packets to be forwarded.
Callback< void, Ptr< const Packet >, const Ipv4Header &, uint32_t > LocalDeliverCallback
Callback for packets to be locally delivered.
Callback< void, Ptr< Ipv4Route >, Ptr< const Packet >, const Ipv4Header & > UnicastForwardCallback
Callback for unicast packets to be forwarded.
Callback< void, Ptr< const Packet >, const Ipv4Header &, Socket::SocketErrno > ErrorCallback
Callback for routing errors (e.g., no route found).
A record of an IPv4 routing table entry for Ipv4GlobalRouting and Ipv4StaticRouting.
Network layer to device interface.
Definition net-device.h:87
A network Node.
Definition node.h:46
network packets
Definition packet.h:228
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
SocketErrno
Enumeration of the possible errors returned by a socket.
Definition socket.h:73
Unit
The unit to use to interpret a number representing time.
Definition nstime.h:102
@ S
second
Definition nstime.h:107
a unique identifier for an interface.
Definition type-id.h:49
Every class exported by the ns3 library is enclosed in the ns3 namespace.