A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
dsdv-rtable.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2010 Hemanth Narra
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  *
20  * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director
21  * ResiliNets Research Group http://wiki.ittc.ku.edu/resilinets
22  * Information and Telecommunication Technology Center (ITTC)
23  * and Department of Electrical Engineering and Computer Science
24  * The University of Kansas Lawrence, KS USA.
25  *
26  * Work supported in part by NSF FIND (Future Internet Design) Program
27  * under grant CNS-0626918 (Postmodern Internet Architecture),
28  * NSF grant CNS-1050226 (Multilayer Network Resilience Analysis and Experimentation on GENI),
29  * US Department of Defense (DoD), and ITTC at The University of Kansas.
30  */
31 #include "dsdv-rtable.h"
32 #include "ns3/simulator.h"
33 #include <iomanip>
34 #include "ns3/log.h"
35 
36 NS_LOG_COMPONENT_DEFINE ("DsdvRoutingTable");
37 
38 namespace ns3 {
39 namespace dsdv {
41  Ipv4Address dst,
42  uint32_t seqNo,
44  uint32_t hops,
45  Ipv4Address nextHop,
46  Time lifetime,
47  Time SettlingTime,
48  bool areChanged)
49  : m_seqNo (seqNo),
50  m_hops (hops),
51  m_lifeTime (lifetime),
52  m_iface (iface),
53  m_flag (VALID),
54  m_settlingTime (SettlingTime),
55  m_entriesChanged (areChanged)
56 {
57  m_ipv4Route = Create<Ipv4Route> ();
59  m_ipv4Route->SetGateway (nextHop);
62 }
64 {
65 }
67 {
68 }
69 
70 bool
72  RoutingTableEntry & rt)
73 {
74  if (m_ipv4AddressEntry.empty ())
75  {
76  return false;
77  }
78  std::map<Ipv4Address, RoutingTableEntry>::const_iterator i = m_ipv4AddressEntry.find (id);
79  if (i == m_ipv4AddressEntry.end ())
80  {
81  return false;
82  }
83  rt = i->second;
84  return true;
85 }
86 
87 bool
89  RoutingTableEntry & rt,
90  bool forRouteInput)
91 {
92  if (m_ipv4AddressEntry.empty ())
93  {
94  return false;
95  }
96  std::map<Ipv4Address, RoutingTableEntry>::const_iterator i = m_ipv4AddressEntry.find (id);
97  if (i == m_ipv4AddressEntry.end ())
98  {
99  return false;
100  }
101  if (forRouteInput == true && id == i->second.GetInterface ().GetBroadcast ())
102  {
103  return false;
104  }
105  rt = i->second;
106  return true;
107 }
108 
109 bool
111 {
112  if (m_ipv4AddressEntry.erase (dst) != 0)
113  {
114  // NS_LOG_DEBUG("Route erased");
115  return true;
116  }
117  return false;
118 }
119 
120 uint32_t
122 {
123  return m_ipv4AddressEntry.size ();
124 }
125 
126 bool
128 {
129  std::pair<std::map<Ipv4Address, RoutingTableEntry>::iterator, bool> result = m_ipv4AddressEntry.insert (std::make_pair (
130  rt.GetDestination (),rt));
131  return result.second;
132 }
133 
134 bool
136 {
137  std::map<Ipv4Address, RoutingTableEntry>::iterator i = m_ipv4AddressEntry.find (rt.GetDestination ());
138  if (i == m_ipv4AddressEntry.end ())
139  {
140  return false;
141  }
142  i->second = rt;
143  return true;
144 }
145 
146 void
148 {
149  if (m_ipv4AddressEntry.empty ())
150  {
151  return;
152  }
153  for (std::map<Ipv4Address, RoutingTableEntry>::iterator i = m_ipv4AddressEntry.begin (); i != m_ipv4AddressEntry.end (); )
154  {
155  if (i->second.GetInterface () == iface)
156  {
157  std::map<Ipv4Address, RoutingTableEntry>::iterator tmp = i;
158  ++i;
159  m_ipv4AddressEntry.erase (tmp);
160  }
161  else
162  {
163  ++i;
164  }
165  }
166 }
167 
168 void
169 RoutingTable::GetListOfAllRoutes (std::map<Ipv4Address, RoutingTableEntry> & allRoutes)
170 {
171  for (std::map<Ipv4Address, RoutingTableEntry>::iterator i = m_ipv4AddressEntry.begin (); i != m_ipv4AddressEntry.end (); ++i)
172  {
173  if (i->second.GetDestination () != Ipv4Address ("127.0.0.1") && i->second.GetFlag () == VALID)
174  {
175  allRoutes.insert (
176  std::make_pair (i->first,i->second));
177  }
178  }
179 }
180 
181 void
183  std::map<Ipv4Address, RoutingTableEntry> & unreachable)
184 {
185  unreachable.clear ();
186  for (std::map<Ipv4Address, RoutingTableEntry>::const_iterator i = m_ipv4AddressEntry.begin (); i
187  != m_ipv4AddressEntry.end (); ++i)
188  {
189  if (i->second.GetNextHop () == nextHop)
190  {
191  unreachable.insert (std::make_pair (i->first,i->second));
192  }
193  }
194 }
195 
196 void
198 {
199  *stream->GetStream () << std::setiosflags (std::ios::fixed) << m_ipv4Route->GetDestination () << "\t\t" << m_ipv4Route->GetGateway () << "\t\t"
200  << m_iface.GetLocal () << "\t\t" << std::setiosflags (std::ios::left)
201  << std::setw (10) << m_hops << "\t" << std::setw (10) << m_seqNo << "\t"
202  << std::setprecision (3) << (Simulator::Now () - m_lifeTime).GetSeconds ()
203  << "s\t\t" << m_settlingTime.GetSeconds () << "s\n";
204 }
205 
206 void
207 RoutingTable::Purge (std::map<Ipv4Address, RoutingTableEntry> & removedAddresses)
208 {
209  if (m_ipv4AddressEntry.empty ())
210  {
211  return;
212  }
213  for (std::map<Ipv4Address, RoutingTableEntry>::iterator i = m_ipv4AddressEntry.begin (); i != m_ipv4AddressEntry.end (); )
214  {
215  std::map<Ipv4Address, RoutingTableEntry>::iterator itmp = i;
216  if (i->second.GetLifeTime () > m_holddownTime && (i->second.GetHop () > 0))
217  {
218  for (std::map<Ipv4Address, RoutingTableEntry>::iterator j = m_ipv4AddressEntry.begin (); j != m_ipv4AddressEntry.end (); )
219  {
220  if ((j->second.GetNextHop () == i->second.GetDestination ()) && (i->second.GetHop () != j->second.GetHop ()))
221  {
222  std::map<Ipv4Address, RoutingTableEntry>::iterator jtmp = j;
223  removedAddresses.insert (std::make_pair (j->first,j->second));
224  ++j;
225  m_ipv4AddressEntry.erase (jtmp);
226  }
227  else
228  {
229  ++j;
230  }
231  }
232  removedAddresses.insert (std::make_pair (i->first,i->second));
233  ++i;
234  m_ipv4AddressEntry.erase (itmp);
235  }
237  /* else if (i->second.GetLifeTime() > m_holddownTime)
238  {
239  ++i;
240  itmp->second.SetFlag(INVALID);
241  }*/
242  else
243  {
244  ++i;
245  }
246  }
247  return;
248 }
249 
250 void
252 {
253  *stream->GetStream () << "\nDSDV Routing table\n" << "Destination\t\tGateway\t\tInterface\t\tHopCount\t\tSeqNum\t\tLifeTime\t\tSettlingTime\n";
254  for (std::map<Ipv4Address, RoutingTableEntry>::const_iterator i = m_ipv4AddressEntry.begin (); i
255  != m_ipv4AddressEntry.end (); ++i)
256  {
257  i->second.Print (stream);
258  }
259  *stream->GetStream () << "\n";
260 }
261 
262 bool
264  EventId id)
265 {
266  std::pair<std::map<Ipv4Address, EventId>::iterator, bool> result = m_ipv4Events.insert (std::make_pair (address,id));
267  return result.second;
268 }
269 
270 bool
272 {
273  EventId event;
274  std::map<Ipv4Address, EventId>::const_iterator i = m_ipv4Events.find (address);
275  if (m_ipv4Events.empty ())
276  {
277  return false;
278  }
279  if (i == m_ipv4Events.end ())
280  {
281  return false;
282  }
283  event = i->second;
284  if (event.IsRunning ())
285  {
286  return true;
287  }
288  else
289  {
290  return false;
291  }
292 }
293 
294 bool
296 {
297  EventId event;
298  std::map<Ipv4Address, EventId>::const_iterator i = m_ipv4Events.find (address);
299  if (m_ipv4Events.empty () || i == m_ipv4Events.end ())
300  {
301  return false;
302  }
303  event = i->second;
304  Simulator::Cancel (event);
305  m_ipv4Events.erase (address);
306  return true;
307 }
308 
309 bool
311 {
312  EventId event;
313  std::map<Ipv4Address, EventId>::const_iterator i = m_ipv4Events.find (address);
314  if (m_ipv4Events.empty () || i == m_ipv4Events.end ())
315  {
316  return false;
317  }
318  event = i->second;
319  if (event.IsRunning ())
320  {
321  return false;
322  }
323  if (event.IsExpired ())
324  {
325  event.Cancel ();
326  m_ipv4Events.erase (address);
327  return true;
328  }
329  else
330  {
331  m_ipv4Events.erase (address);
332  return true;
333  }
334 }
335 
336 EventId
338 {
339  std::map <Ipv4Address, EventId>::const_iterator i = m_ipv4Events.find (address);
340  if (m_ipv4Events.empty () || i == m_ipv4Events.end ())
341  {
342  return EventId ();
343  }
344  else
345  {
346  return i->second;
347  }
348 }
349 }
350 }
Time m_holddownTime
an entry in the routing table.
Definition: dsdv-rtable.h:344
uint32_t m_hops
Hop Count (number of hops needed to reach destination)
Definition: dsdv-rtable.h:188
keep track of time values and allow control of global simulation resolution
Definition: nstime.h:81
Routing table entry.
Definition: dsdv-rtable.h:56
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
void Print(Ptr< OutputStreamWrapper > stream) const
Definition: dsdv-rtable.cc:197
bool AnyRunningEvent(Ipv4Address address)
Force delete an update waiting for settling time to complete as a better update to same destination w...
Definition: dsdv-rtable.cc:271
Ipv4Address GetLocal(void) const
Get the local address.
Time m_settlingTime
Time for which the node retains an update with changed metric before broadcasting it...
Definition: dsdv-rtable.h:209
EventId GetEventId(Ipv4Address address)
Get the EcentId associated with that address.
Definition: dsdv-rtable.cc:337
void GetListOfAllRoutes(std::map< Ipv4Address, RoutingTableEntry > &allRoutes)
Lookup list of all addresses in the routing table.
Definition: dsdv-rtable.cc:169
static void Cancel(const EventId &id)
Set the cancel bit on this event: the event's associated function will not be invoked when it expires...
Definition: simulator.cc:268
bool IsRunning(void) const
This method is syntactic sugar for the ns3::Simulator::isExpired method.
Definition: event-id.cc:59
bool Update(RoutingTableEntry &rt)
Updating the routing Table with routing table entry rt.
Definition: dsdv-rtable.cc:135
void SetSource(Ipv4Address src)
Definition: ipv4-route.cc:49
bool ForceDeleteIpv4Event(Ipv4Address address)
Force delete an update waiting for settling time to complete as a better update to same destination w...
Definition: dsdv-rtable.cc:295
double GetSeconds(void) const
Definition: nstime.h:274
NS_LOG_COMPONENT_DEFINE("DsdvRoutingTable")
void SetGateway(Ipv4Address gw)
Definition: ipv4-route.cc:63
uint32_t m_seqNo
Destination Sequence Number.
Definition: dsdv-rtable.h:186
~RoutingTableEntry()
Definition: dsdv-rtable.cc:63
void Purge(std::map< Ipv4Address, RoutingTableEntry > &removedAddresses)
Delete all outdated entries if Lifetime is expired.
Definition: dsdv-rtable.cc:207
Ipv4Address GetGateway(void) const
Definition: ipv4-route.cc:70
Ipv4InterfaceAddress m_iface
Output interface address.
Definition: dsdv-rtable.h:204
std::map< Ipv4Address, EventId > m_ipv4Events
an entry in the event table.
Definition: dsdv-rtable.h:342
void DeleteAllRoutesFromInterface(Ipv4InterfaceAddress iface)
Delete all route from interface with address iface.
Definition: dsdv-rtable.cc:147
bool AddRoute(RoutingTableEntry &r)
Add routing table entry if it doesn't yet exist in routing table.
Definition: dsdv-rtable.cc:127
uint32_t RoutingTableSize()
Provides the number of routes present in that nodes routing table.
Definition: dsdv-rtable.cc:121
static Time Now(void)
Return the "current simulation time".
Definition: simulator.cc:180
void SetOutputDevice(Ptr< NetDevice > outputDevice)
Equivalent in Linux to dst_entry.dev.
Definition: ipv4-route.cc:77
std::map< Ipv4Address, RoutingTableEntry > m_ipv4AddressEntry
an entry in the routing table.
Definition: dsdv-rtable.h:340
void GetListOfDestinationWithNextHop(Ipv4Address nxtHp, std::map< Ipv4Address, RoutingTableEntry > &dstList)
Lookup list of addresses for which nxtHp is the next Hop address.
Definition: dsdv-rtable.cc:182
bool DeleteIpv4Event(Ipv4Address address)
Clear up the entry from the map after the event is completed.
Definition: dsdv-rtable.cc:310
Ipv4Address GetDestination(void) const
Definition: ipv4-route.cc:42
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
bool DeleteRoute(Ipv4Address dst)
Delete routing table entry with destination address dst, if it exists.
Definition: dsdv-rtable.cc:110
a class to store IPv4 address information on an interface
an identifier for simulation events.
Definition: event-id.h:46
RoutingTableEntry(Ptr< NetDevice > dev=0, Ipv4Address dst=Ipv4Address(), uint32_t m_seqNo=0, Ipv4InterfaceAddress iface=Ipv4InterfaceAddress(), uint32_t hops=0, Ipv4Address nextHop=Ipv4Address(), Time lifetime=Simulator::Now(), Time SettlingTime=Simulator::Now(), bool changedEntries=false)
c-tor
Definition: dsdv-rtable.cc:40
Ipv4Address GetDestination() const
Definition: dsdv-rtable.h:66
Time m_lifeTime
Expiration or deletion time of the route Lifetime field in the routing table plays dual role – for a...
Definition: dsdv-rtable.h:195
bool AddIpv4Event(Ipv4Address address, EventId id)
Add an event for a destination address so that the update to for that destination is sent after the e...
Definition: dsdv-rtable.cc:263
void Print(Ptr< OutputStreamWrapper > stream) const
Print routing table.
Definition: dsdv-rtable.cc:251
tuple address
Definition: first.py:37
bool LookupRoute(Ipv4Address dst, RoutingTableEntry &rt)
Lookup routing table entry with destination address dst.
Definition: dsdv-rtable.cc:71
bool IsExpired(void) const
This method is syntactic sugar for the ns3::Simulator::isExpired method.
Definition: event-id.cc:53
Ptr< Ipv4Route > m_ipv4Route
Ip route, include.
Definition: dsdv-rtable.h:202
std::ostream * GetStream(void)
Return a pointer to an ostream previously set in the wrapper.
void SetDestination(Ipv4Address dest)
Definition: ipv4-route.cc:35