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