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