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