1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2010 Hemanth Narra, Yufei Cheng
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  * Author: Hemanth Narra <hemanth@ittc.ku.com>
19  * Author: Yufei Cheng   <yfcheng@ittc.ku.edu>
20  *
21  * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director
22  * ResiliNets Research Group  http://wiki.ittc.ku.edu/resilinets
23  * Information and Telecommunication Technology Center (ITTC)
24  * and Department of Electrical Engineering and Computer Science
25  * The University of Kansas Lawrence, KS USA.
26  *
27  * Work supported in part by NSF FIND (Future Internet Design) Program
28  * under grant CNS-0626918 (Postmodern Internet Architecture),
29  * NSF grant CNS-1050226 (Multilayer Network Resilience Analysis and Experimentation on GENI),
30  * US Department of Defense (DoD), and ITTC at The University of Kansas.
31  */
32 
33 #ifndef DSDV_ROUTING_PROTOCOL_H
34 #define DSDV_ROUTING_PROTOCOL_H
35 
36 #include "dsdv-rtable.h"
37 #include "dsdv-packet-queue.h"
38 #include "dsdv-packet.h"
39 #include "ns3/node.h"
40 #include "ns3/random-variable-stream.h"
41 #include "ns3/ipv4-routing-protocol.h"
42 #include "ns3/ipv4-interface.h"
43 #include "ns3/ipv4-l3-protocol.h"
44 #include "ns3/output-stream-wrapper.h"
45 
46 namespace ns3 {
47 namespace dsdv {
48 
49 /**
50  * \ingroup dsdv
51  * \brief DSDV routing protocol.
52  */
53 class RoutingProtocol : public Ipv4RoutingProtocol
54 {
55 public:
56   /**
57    * \brief Get the type ID.
58    * \return the object TypeId
59    */
60   static TypeId GetTypeId (void);
61   static const uint32_t DSDV_PORT;
62 
63   /// c-tor
64   RoutingProtocol ();
65   virtual
66   ~RoutingProtocol ();
67   virtual void
68   DoDispose ();
69 
70   // From Ipv4RoutingProtocol
71   Ptr<Ipv4Route> RouteOutput (Ptr<Packet> p, const Ipv4Header &header, Ptr<NetDevice> oif, Socket::SocketErrno &sockerr);
72   /**
73    * Route input packet
74    * \param p The packet
75    * \param header The IPv4 header
76    * \param idev The device
77    * \param ucb The unicast forward callback
78    * \param mcb The multicast forward callback
79    * \param lcb The local deliver callback
80    * \param ecb The error callback
81    * \returns true if successful
82    */
83   bool RouteInput (Ptr<const Packet> p, const Ipv4Header &header, Ptr<const NetDevice> idev, UnicastForwardCallback ucb,
84                    MulticastForwardCallback mcb, LocalDeliverCallback lcb, ErrorCallback ecb);
85   virtual void PrintRoutingTable (Ptr<OutputStreamWrapper> stream, Time::Unit unit = Time::S) const;
86   virtual void NotifyInterfaceUp (uint32_t interface);
87   virtual void NotifyInterfaceDown (uint32_t interface);
88   virtual void NotifyAddAddress (uint32_t interface, Ipv4InterfaceAddress address);
89   virtual void NotifyRemoveAddress (uint32_t interface, Ipv4InterfaceAddress address);
90   virtual void SetIpv4 (Ptr<Ipv4> ipv4);
91 
92   // Methods to handle protocol parameters
93   /**
94    * Set enable buffer flag
95    * \param f The enable buffer flag
96    */
97   void SetEnableBufferFlag (bool f);
98   /**
99    * Get enable buffer flag
100    * \returns the enable buffer flag
101    */
102   bool GetEnableBufferFlag () const;
103   /**
104    * Set weighted settling time (WST) flag
105    * \param f the weighted settling time (WST) flag
106    */
107   void SetWSTFlag (bool f);
108   /**
109    * Get weighted settling time (WST) flag
110    * \returns the weighted settling time (WST) flag
111    */
112   bool GetWSTFlag () const;
113   /**
114    * Set enable route aggregation (RA) flag
115    * \param f the enable route aggregation (RA) flag
116    */
117   void SetEnableRAFlag (bool f);
118   /**
119    * Get enable route aggregation (RA) flag
120    * \returns the enable route aggregation (RA) flag
121    */
122   bool GetEnableRAFlag () const;
123 
124   /**
125    * Assign a fixed random variable stream number to the random variables
126    * used by this model.  Return the number of streams (possibly zero) that
127    * have been assigned.
128    *
129    * \param stream first stream index to use
130    * \return the number of stream indices assigned by this model
131    */
132   int64_t AssignStreams (int64_t stream);
133 
134 private:
135   // Protocol parameters.
136   /// Holdtimes is the multiplicative factor of PeriodicUpdateInterval for which the node waits since the last update
137   /// before flushing a route from the routing table. If PeriodicUpdateInterval is 8s and Holdtimes is 3, the node
138   /// waits for 24s since the last update to flush this route from its routing table.
139   uint32_t Holdtimes;
140   /// PeriodicUpdateInterval specifies the periodic time interval between which the a node broadcasts
141   /// its entire routing table.
142   Time m_periodicUpdateInterval;
143   /// SettlingTime specifies the time for which a node waits before propagating an update.
144   /// It waits for this time interval in hope of receiving an update with a better metric.
145   Time m_settlingTime;
146   /// Nodes IP address
147   Ipv4Address m_mainAddress;
148   /// IP protocol
149   Ptr<Ipv4> m_ipv4;
150   /// Raw socket per each IP interface, map socket -> iface address (IP + mask)
151   std::map<Ptr<Socket>, Ipv4InterfaceAddress> m_socketAddresses;
152   /// Loopback device used to defer route requests until a route is found
153   Ptr<NetDevice> m_lo;
154   /// Main Routing table for the node
155   RoutingTable m_routingTable;
156   /// Advertised Routing table for the node
157   RoutingTable m_advRoutingTable;
158   /// The maximum number of packets that we allow a routing protocol to buffer.
159   uint32_t m_maxQueueLen;
160   /// The maximum number of packets that we allow per destination to buffer.
161   uint32_t m_maxQueuedPacketsPerDst;
162   /// The maximum period of time that a routing protocol is allowed to buffer a packet for.
163   Time m_maxQueueTime;
164   /// A "drop front on full" queue used by the routing layer to buffer packets to which it does not have a route.
165   PacketQueue m_queue;
166   /// Flag that is used to enable or disable buffering
167   bool EnableBuffering;
168   /// Flag that is used to enable or disable Weighted Settling Time
169   bool EnableWST;
170   /// This is the wighted factor to determine the weighted settling time
171   double m_weightedFactor;
172   /// This is a flag to enable route aggregation. Route aggregation will aggregate all routes for
173   /// 'RouteAggregationTime' from the time an update is received by a node and sends them as a single update .
174   bool EnableRouteAggregation;
175   /// Parameter that holds the route aggregation time interval
176   Time m_routeAggregationTime;
177   /// Unicast callback for own packets
178   UnicastForwardCallback m_scb;
179   /// Error callback for own packets
180   ErrorCallback m_ecb;
181 
182 private:
183   /// Start protocol operation
184   void
185   Start ();
186   /**
187    * Queue packet until we find a route
188    * \param p the packet to route
189    * \param header the Ipv4Header
190    * \param ucb the UnicastForwardCallback function
191    * \param ecb the ErrorCallback function
192    */
193   void
194   DeferredRouteOutput (Ptr<const Packet> p, const Ipv4Header & header, UnicastForwardCallback ucb, ErrorCallback ecb);
195   /// Look for any queued packets to send them out
196   void
197   LookForQueuedPackets (void);
198   /**
199    * Send packet from queue
200    * \param dst - destination address to which we are sending the packet to
201    * \param route - route identified for this packet
202    */
203   void
204   SendPacketFromQueue (Ipv4Address dst, Ptr<Ipv4Route> route);
205   /**
206    * Find socket with local interface address iface
207    * \param iface the interface
208    * \returns the socket
209    */
210   Ptr<Socket>
211   FindSocketWithInterfaceAddress (Ipv4InterfaceAddress iface) const;
212 
213   // Receive dsdv control packets
214   /**
215    * Receive and process dsdv control packet
216    * \param socket the socket for receiving dsdv control packets
217    */
218   void
219   RecvDsdv (Ptr<Socket> socket);
220   /// Send packet
221   void
222   Send (Ptr<Ipv4Route>, Ptr<const Packet>, const Ipv4Header &);
223   /**
224    * Create loopback route for given header
225    *
226    * \param header the IP header
227    * \param oif the device
228    * \returns the route
229    */
230   Ptr<Ipv4Route>
231   LoopbackRoute (const Ipv4Header & header, Ptr<NetDevice> oif) const;
232   /**
233    * Get settlingTime for a destination
234    * \param dst - destination address
235    * \return settlingTime for the destination if found
236    */
237   Time
238   GetSettlingTime (Ipv4Address dst);
239   /// Sends trigger update from a node
240   void
241   SendTriggeredUpdate ();
242   /// Broadcasts the entire routing table for every PeriodicUpdateInterval
243   void
244   SendPeriodicUpdate ();
245   /// Merge periodic updates
246   void
247   MergeTriggerPeriodicUpdates ();
248   /// Notify that packet is dropped for some reason
249   void
250   Drop (Ptr<const Packet>, const Ipv4Header &, Socket::SocketErrno);
251   /// Timer to trigger periodic updates from a node
252   Timer m_periodicUpdateTimer;
253   /// Timer used by the trigger updates in case of Weighted Settling Time is used
254   Timer m_triggeredExpireTimer;
255 
256   /// Provides uniform random variables.
257   Ptr<UniformRandomVariable> m_uniformRandomVariable;
258 };
259 
260 }
261 }
262 
263 #endif /* DSDV_ROUTING_PROTOCOL_H */
264