A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
dsdv-rtable.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010 Hemanth Narra
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 * Author: Hemanth Narra <hemanth@ittc.ku.com>
18 *
19 * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director
20 * ResiliNets Research Group https://resilinets.org/
21 * Information and Telecommunication Technology Center (ITTC)
22 * and Department of Electrical Engineering and Computer Science
23 * The University of Kansas Lawrence, KS USA.
24 *
25 * Work supported in part by NSF FIND (Future Internet Design) Program
26 * under grant CNS-0626918 (Postmodern Internet Architecture),
27 * NSF grant CNS-1050226 (Multilayer Network Resilience Analysis and Experimentation on GENI),
28 * US Department of Defense (DoD), and ITTC at The University of Kansas.
29 */
30#include "dsdv-rtable.h"
31
32#include "ns3/log.h"
33#include "ns3/simulator.h"
34
35#include <iomanip>
36
37namespace ns3
38{
39
40NS_LOG_COMPONENT_DEFINE("DsdvRoutingTable");
41
42namespace dsdv
43{
45 Ipv4Address dst,
46 uint32_t seqNo,
48 uint32_t hops,
49 Ipv4Address nextHop,
50 Time lifetime,
51 Time settlingTime,
52 bool areChanged)
53 : m_seqNo(seqNo),
54 m_hops(hops),
55 m_lifeTime(lifetime),
56 m_iface(iface),
57 m_flag(VALID),
58 m_settlingTime(settlingTime),
59 m_entriesChanged(areChanged)
60{
61 m_ipv4Route = Create<Ipv4Route>();
62 m_ipv4Route->SetDestination(dst);
63 m_ipv4Route->SetGateway(nextHop);
64 m_ipv4Route->SetSource(m_iface.GetLocal());
65 m_ipv4Route->SetOutputDevice(dev);
66}
67
69{
70}
71
73{
74}
75
76bool
78{
79 if (m_ipv4AddressEntry.empty())
80 {
81 return false;
82 }
83 auto i = m_ipv4AddressEntry.find(id);
84 if (i == m_ipv4AddressEntry.end())
85 {
86 return false;
87 }
88 rt = i->second;
89 return true;
90}
91
92bool
94{
95 if (m_ipv4AddressEntry.empty())
96 {
97 return false;
98 }
99 auto i = m_ipv4AddressEntry.find(id);
100 if (i == m_ipv4AddressEntry.end())
101 {
102 return false;
103 }
104 if (forRouteInput && id == i->second.GetInterface().GetBroadcast())
105 {
106 return false;
107 }
108 rt = i->second;
109 return true;
110}
111
112bool
114{
115 return m_ipv4AddressEntry.erase(dst) != 0;
116}
117
120{
121 return m_ipv4AddressEntry.size();
122}
123
124bool
126{
127 auto result = m_ipv4AddressEntry.insert(std::make_pair(rt.GetDestination(), rt));
128 return result.second;
129}
130
131bool
133{
134 auto i = m_ipv4AddressEntry.find(rt.GetDestination());
135 if (i == m_ipv4AddressEntry.end())
136 {
137 return false;
138 }
139 i->second = rt;
140 return true;
141}
142
143void
145{
146 if (m_ipv4AddressEntry.empty())
147 {
148 return;
149 }
150 for (auto i = m_ipv4AddressEntry.begin(); i != m_ipv4AddressEntry.end();)
151 {
152 if (i->second.GetInterface() == iface)
153 {
154 auto tmp = i;
155 ++i;
156 m_ipv4AddressEntry.erase(tmp);
157 }
158 else
159 {
160 ++i;
161 }
162 }
163}
164
165void
166RoutingTable::GetListOfAllRoutes(std::map<Ipv4Address, RoutingTableEntry>& allRoutes)
167{
168 for (auto i = m_ipv4AddressEntry.begin(); i != m_ipv4AddressEntry.end(); ++i)
169 {
170 if (i->second.GetDestination() != Ipv4Address("127.0.0.1") && i->second.GetFlag() == VALID)
171 {
172 allRoutes.insert(std::make_pair(i->first, i->second));
173 }
174 }
175}
176
177void
179 std::map<Ipv4Address, RoutingTableEntry>& unreachable)
180{
181 unreachable.clear();
182 for (auto i = m_ipv4AddressEntry.begin(); i != m_ipv4AddressEntry.end(); ++i)
183 {
184 if (i->second.GetNextHop() == nextHop)
185 {
186 unreachable.insert(std::make_pair(i->first, i->second));
187 }
188 }
189}
190
191void
193{
194 std::ostream* os = stream->GetStream();
195 // Copy the current ostream state
196 std::ios oldState(nullptr);
197 oldState.copyfmt(*os);
198
199 *os << std::resetiosflags(std::ios::adjustfield) << std::setiosflags(std::ios::left);
200
201 std::ostringstream dest;
202 std::ostringstream gw;
203 std::ostringstream iface;
204 std::ostringstream ltime;
205 std::ostringstream stime;
206 dest << m_ipv4Route->GetDestination();
207 gw << m_ipv4Route->GetGateway();
208 iface << m_iface.GetLocal();
209 ltime << std::setprecision(3) << (Simulator::Now() - m_lifeTime).As(unit);
210 stime << m_settlingTime.As(unit);
211
212 *os << std::setw(16) << dest.str();
213 *os << std::setw(16) << gw.str();
214 *os << std::setw(16) << iface.str();
215 *os << std::setw(16) << m_hops;
216 *os << std::setw(16) << m_seqNo;
217 *os << std::setw(16) << ltime.str();
218 *os << stime.str() << std::endl;
219 // Restore the previous ostream state
220 (*os).copyfmt(oldState);
221}
222
223void
224RoutingTable::Purge(std::map<Ipv4Address, RoutingTableEntry>& removedAddresses)
225{
226 if (m_ipv4AddressEntry.empty())
227 {
228 return;
229 }
230 for (auto i = m_ipv4AddressEntry.begin(); i != m_ipv4AddressEntry.end();)
231 {
232 auto itmp = i;
233 if (i->second.GetLifeTime() > m_holddownTime && (i->second.GetHop() > 0))
234 {
235 for (auto j = m_ipv4AddressEntry.begin(); j != m_ipv4AddressEntry.end();)
236 {
237 if ((j->second.GetNextHop() == i->second.GetDestination()) &&
238 (i->second.GetHop() != j->second.GetHop()))
239 {
240 auto jtmp = j;
241 removedAddresses.insert(std::make_pair(j->first, j->second));
242 ++j;
243 m_ipv4AddressEntry.erase(jtmp);
244 }
245 else
246 {
247 ++j;
248 }
249 }
250 removedAddresses.insert(std::make_pair(i->first, i->second));
251 ++i;
252 m_ipv4AddressEntry.erase(itmp);
253 }
254 /** \todo Need to decide when to invalidate a route */
255 /* else if (i->second.GetLifeTime() > m_holddownTime)
256 {
257 ++i;
258 itmp->second.SetFlag(INVALID);
259 }*/
260 else
261 {
262 ++i;
263 }
264 }
265}
266
267void
269{
270 std::ostream* os = stream->GetStream();
271 // Copy the current ostream state
272 std::ios oldState(nullptr);
273 oldState.copyfmt(*os);
274
275 *os << std::resetiosflags(std::ios::adjustfield) << std::setiosflags(std::ios::left);
276
277 *os << "\nDSDV Routing table\n";
278 *os << std::setw(16) << "Destination";
279 *os << std::setw(16) << "Gateway";
280 *os << std::setw(16) << "Interface";
281 *os << std::setw(16) << "HopCount";
282 *os << std::setw(16) << "SeqNum";
283 *os << std::setw(16) << "LifeTime";
284 *os << "SettlingTime" << std::endl;
285 for (auto i = m_ipv4AddressEntry.begin(); i != m_ipv4AddressEntry.end(); ++i)
286 {
287 i->second.Print(stream, unit);
288 }
289 *os << std::endl;
290 // Restore the previous ostream state
291 (*os).copyfmt(oldState);
292}
293
294bool
296{
297 auto result = m_ipv4Events.insert(std::make_pair(address, id));
298 return result.second;
299}
300
301bool
303{
304 EventId event;
305 auto i = m_ipv4Events.find(address);
306 if (m_ipv4Events.empty())
307 {
308 return false;
309 }
310 if (i == m_ipv4Events.end())
311 {
312 return false;
313 }
314 event = i->second;
315 return event.IsRunning();
316}
317
318bool
320{
321 EventId event;
322 auto i = m_ipv4Events.find(address);
323 if (m_ipv4Events.empty() || i == m_ipv4Events.end())
324 {
325 return false;
326 }
327 event = i->second;
328 Simulator::Cancel(event);
329 m_ipv4Events.erase(address);
330 return true;
331}
332
333bool
335{
336 EventId event;
337 auto i = m_ipv4Events.find(address);
338 if (m_ipv4Events.empty() || i == m_ipv4Events.end())
339 {
340 return false;
341 }
342 event = i->second;
343 if (event.IsRunning())
344 {
345 return false;
346 }
347 if (event.IsExpired())
348 {
349 event.Cancel();
350 m_ipv4Events.erase(address);
351 return true;
352 }
353 else
354 {
355 m_ipv4Events.erase(address);
356 return true;
357 }
358}
359
362{
363 auto i = m_ipv4Events.find(address);
364 if (m_ipv4Events.empty() || i == m_ipv4Events.end())
365 {
366 return EventId();
367 }
368 else
369 {
370 return i->second;
371 }
372}
373} // namespace dsdv
374} // namespace ns3
An identifier for simulation events.
Definition: event-id.h:55
bool IsExpired() const
This method is syntactic sugar for the ns3::Simulator::IsExpired method.
Definition: event-id.cc:69
bool IsRunning() const
This method is syntactic sugar for !IsExpired().
Definition: event-id.cc:76
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
a class to store IPv4 address information on an interface
Ipv4Address GetLocal() const
Get the local address.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
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:285
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition: time.cc:415
Unit
The unit to use to interpret a number representing time.
Definition: nstime.h:111
Routing table entry.
Definition: dsdv-rtable.h:59
uint32_t m_seqNo
Destination Sequence Number.
Definition: dsdv-rtable.h:295
void Print(Ptr< OutputStreamWrapper > stream, Time::Unit unit=Time::S) const
Print routing table entry.
Definition: dsdv-rtable.cc:192
Time m_lifeTime
Expiration or deletion time of the route Lifetime field in the routing table plays dual role – for an...
Definition: dsdv-rtable.h:304
Ipv4Address GetDestination() const
Get destination IP address.
Definition: dsdv-rtable.h:90
~RoutingTableEntry()
Definition: dsdv-rtable.cc:68
Ipv4InterfaceAddress m_iface
Output interface address.
Definition: dsdv-rtable.h:313
RoutingTableEntry(Ptr< NetDevice > dev=nullptr, Ipv4Address dst=Ipv4Address(), uint32_t 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:44
Ptr< Ipv4Route > m_ipv4Route
Ip route, include.
Definition: dsdv-rtable.h:311
Time m_settlingTime
Time for which the node retains an update with changed metric before broadcasting it.
Definition: dsdv-rtable.h:318
uint32_t m_hops
Hop Count (number of hops needed to reach destination)
Definition: dsdv-rtable.h:297
bool LookupRoute(Ipv4Address dst, RoutingTableEntry &rt)
Lookup routing table entry with destination address dst.
Definition: dsdv-rtable.cc:77
Time m_holddownTime
hold down time of an expired route
Definition: dsdv-rtable.h:466
bool DeleteRoute(Ipv4Address dst)
Delete routing table entry with destination address dst, if it exists.
Definition: dsdv-rtable.cc:113
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:319
bool AddRoute(RoutingTableEntry &r)
Add routing table entry if it doesn't yet exist in routing table.
Definition: dsdv-rtable.cc:125
bool DeleteIpv4Event(Ipv4Address address)
Clear up the entry from the map after the event is completed.
Definition: dsdv-rtable.cc:334
bool Update(RoutingTableEntry &rt)
Updating the routing Table with routing table entry rt.
Definition: dsdv-rtable.cc:132
std::map< Ipv4Address, RoutingTableEntry > m_ipv4AddressEntry
an entry in the routing table.
Definition: dsdv-rtable.h:462
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:178
void Print(Ptr< OutputStreamWrapper > stream, Time::Unit unit=Time::S) const
Print routing table.
Definition: dsdv-rtable.cc:268
void DeleteAllRoutesFromInterface(Ipv4InterfaceAddress iface)
Delete all route from interface with address iface.
Definition: dsdv-rtable.cc:144
EventId GetEventId(Ipv4Address address)
Get the EventId associated with that address.
Definition: dsdv-rtable.cc:361
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:295
uint32_t RoutingTableSize()
Provides the number of routes present in that nodes routing table.
Definition: dsdv-rtable.cc:119
std::map< Ipv4Address, EventId > m_ipv4Events
an entry in the event table.
Definition: dsdv-rtable.h:464
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:302
void Purge(std::map< Ipv4Address, RoutingTableEntry > &removedAddresses)
Delete all outdated entries if Lifetime is expired.
Definition: dsdv-rtable.cc:224
void GetListOfAllRoutes(std::map< Ipv4Address, RoutingTableEntry > &allRoutes)
Lookup list of all addresses in the routing table.
Definition: dsdv-rtable.cc:166
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
Every class exported by the ns3 library is enclosed in the ns3 namespace.