author | Tom Henderson <tomh@tomh.org> |
Fri, 03 Aug 2007 09:49:02 -0700 | |
changeset 1121 | 541bfe1308b2 |
parent 1113 | src/routing/global/global-route-manager-impl.cc@5b63b39161e7 |
child 1200 | ae6244482a59 |
permissions | -rw-r--r-- |
1111 | 1 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
2 |
/* |
|
3 |
* This program is free software; you can redistribute it and/or modify |
|
4 |
* it under the terms of the GNU General Public License version 2 as |
|
5 |
* published by the Free Software Foundation; |
|
6 |
* |
|
7 |
* This program is distributed in the hope that it will be useful, |
|
8 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
9 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
10 |
* GNU General Public License for more details. |
|
11 |
* |
|
12 |
* You should have received a copy of the GNU General Public License |
|
13 |
* along with this program; if not, write to the Free Software |
|
14 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
15 |
*/ |
|
16 |
||
17 |
#include <utility> |
|
18 |
#include <vector> |
|
19 |
#include <queue> |
|
20 |
#include "ns3/assert.h" |
|
21 |
#include "ns3/fatal-error.h" |
|
22 |
#include "ns3/debug.h" |
|
23 |
#include "ns3/node-list.h" |
|
24 |
#include "ns3/ipv4.h" |
|
25 |
#include "global-router-interface.h" |
|
26 |
#include "global-route-manager-impl.h" |
|
27 |
#include "candidate-queue.h" |
|
28 |
||
29 |
NS_DEBUG_COMPONENT_DEFINE ("GlobalRouteManager"); |
|
30 |
||
31 |
namespace ns3 { |
|
32 |
||
33 |
// --------------------------------------------------------------------------- |
|
34 |
// |
|
35 |
// SPFVertex Implementation |
|
36 |
// |
|
37 |
// --------------------------------------------------------------------------- |
|
38 |
||
39 |
SPFVertex::SPFVertex () : |
|
40 |
m_vertexType (VertexUnknown), |
|
41 |
m_vertexId ("255.255.255.255"), |
|
42 |
m_lsa (0), |
|
43 |
m_distanceFromRoot (SPF_INFINITY), |
|
44 |
m_rootOif (SPF_INFINITY), |
|
45 |
m_nextHop ("0.0.0.0"), |
|
46 |
m_parent (0), |
|
47 |
m_children () |
|
48 |
{ |
|
49 |
} |
|
50 |
||
51 |
SPFVertex::SPFVertex (GlobalRouterLSA* lsa) : |
|
52 |
m_vertexType (VertexRouter), |
|
53 |
m_vertexId (lsa->GetLinkStateId ()), |
|
54 |
m_lsa (lsa), |
|
55 |
m_distanceFromRoot (SPF_INFINITY), |
|
56 |
m_rootOif (SPF_INFINITY), |
|
57 |
m_nextHop ("0.0.0.0"), |
|
58 |
m_parent (0), |
|
59 |
m_children () |
|
60 |
{ |
|
61 |
} |
|
62 |
||
63 |
SPFVertex::~SPFVertex () |
|
64 |
{ |
|
65 |
for ( ListOfSPFVertex_t::iterator i = m_children.begin (); |
|
66 |
i != m_children.end (); |
|
67 |
i++) |
|
68 |
{ |
|
69 |
SPFVertex *p = *i; |
|
70 |
delete p; |
|
71 |
p = 0; |
|
72 |
*i = 0; |
|
73 |
} |
|
74 |
m_children.clear (); |
|
75 |
} |
|
76 |
||
77 |
void |
|
78 |
SPFVertex::SetVertexType (SPFVertex::VertexType type) |
|
79 |
{ |
|
80 |
m_vertexType = type; |
|
81 |
} |
|
82 |
||
83 |
SPFVertex::VertexType |
|
84 |
SPFVertex::GetVertexType (void) const |
|
85 |
{ |
|
86 |
return m_vertexType; |
|
87 |
} |
|
88 |
||
89 |
void |
|
90 |
SPFVertex::SetVertexId (Ipv4Address id) |
|
91 |
{ |
|
92 |
m_vertexId = id; |
|
93 |
} |
|
94 |
||
95 |
Ipv4Address |
|
96 |
SPFVertex::GetVertexId (void) const |
|
97 |
{ |
|
98 |
return m_vertexId; |
|
99 |
} |
|
100 |
||
101 |
void |
|
102 |
SPFVertex::SetLSA (GlobalRouterLSA* lsa) |
|
103 |
{ |
|
104 |
m_lsa = lsa; |
|
105 |
} |
|
106 |
||
107 |
GlobalRouterLSA* |
|
108 |
SPFVertex::GetLSA (void) const |
|
109 |
{ |
|
110 |
return m_lsa; |
|
111 |
} |
|
112 |
||
113 |
void |
|
114 |
SPFVertex::SetDistanceFromRoot (uint32_t distance) |
|
115 |
{ |
|
116 |
m_distanceFromRoot = distance; |
|
117 |
} |
|
118 |
||
119 |
uint32_t |
|
120 |
SPFVertex::GetDistanceFromRoot (void) const |
|
121 |
{ |
|
122 |
return m_distanceFromRoot; |
|
123 |
} |
|
124 |
||
125 |
void |
|
126 |
SPFVertex::SetOutgoingInterfaceId (uint32_t id) |
|
127 |
{ |
|
128 |
m_rootOif = id; |
|
129 |
} |
|
130 |
||
131 |
uint32_t |
|
132 |
SPFVertex::GetOutgoingInterfaceId (void) const |
|
133 |
{ |
|
134 |
return m_rootOif; |
|
135 |
} |
|
136 |
||
137 |
void |
|
138 |
SPFVertex::SetNextHop (Ipv4Address nextHop) |
|
139 |
{ |
|
140 |
m_nextHop = nextHop; |
|
141 |
} |
|
142 |
||
143 |
Ipv4Address |
|
144 |
SPFVertex::GetNextHop (void) const |
|
145 |
{ |
|
146 |
return m_nextHop; |
|
147 |
} |
|
148 |
||
149 |
void |
|
150 |
SPFVertex::SetParent (SPFVertex* parent) |
|
151 |
{ |
|
152 |
m_parent = parent; |
|
153 |
} |
|
154 |
||
155 |
SPFVertex* |
|
156 |
SPFVertex::GetParent (void) const |
|
157 |
{ |
|
158 |
return m_parent; |
|
159 |
} |
|
160 |
||
161 |
uint32_t |
|
162 |
SPFVertex::GetNChildren (void) const |
|
163 |
{ |
|
164 |
return m_children.size (); |
|
165 |
} |
|
166 |
||
167 |
SPFVertex* |
|
168 |
SPFVertex::GetChild (uint32_t n) const |
|
169 |
{ |
|
170 |
uint32_t j = 0; |
|
171 |
||
172 |
for ( ListOfSPFVertex_t::const_iterator i = m_children.begin (); |
|
173 |
i != m_children.end (); |
|
174 |
i++, j++) |
|
175 |
{ |
|
176 |
if (j == n) |
|
177 |
{ |
|
178 |
return *i; |
|
179 |
} |
|
180 |
} |
|
181 |
NS_ASSERT_MSG(false, "Index <n> out of range."); |
|
182 |
return 0; |
|
183 |
} |
|
184 |
||
185 |
uint32_t |
|
186 |
SPFVertex::AddChild (SPFVertex* child) |
|
187 |
{ |
|
188 |
m_children.push_back (child); |
|
189 |
return m_children.size (); |
|
190 |
} |
|
191 |
||
192 |
// --------------------------------------------------------------------------- |
|
193 |
// |
|
194 |
// GlobalRouteManagerLSDB Implementation |
|
195 |
// |
|
196 |
// --------------------------------------------------------------------------- |
|
197 |
||
198 |
GlobalRouteManagerLSDB::GlobalRouteManagerLSDB () |
|
199 |
: |
|
200 |
m_database () |
|
201 |
{ |
|
202 |
NS_DEBUG ("GlobalRouteManagerLSDB::GlobalRouteManagerLSDB ()"); |
|
203 |
} |
|
204 |
||
205 |
GlobalRouteManagerLSDB::~GlobalRouteManagerLSDB () |
|
206 |
{ |
|
207 |
NS_DEBUG ("GlobalRouteManagerLSDB::~GlobalRouteManagerLSDB ()"); |
|
208 |
||
209 |
LSDBMap_t::iterator i; |
|
210 |
for (i= m_database.begin (); i!= m_database.end (); i++) |
|
211 |
{ |
|
212 |
NS_DEBUG ("GlobalRouteManagerLSDB::~GlobalRouteManagerLSDB ():free LSA"); |
|
213 |
GlobalRouterLSA* temp = i->second; |
|
214 |
delete temp; |
|
215 |
} |
|
216 |
NS_DEBUG ("GlobalRouteManagerLSDB::~GlobalRouteManagerLSDB (): clear map"); |
|
217 |
m_database.clear (); |
|
218 |
} |
|
219 |
||
220 |
void |
|
221 |
GlobalRouteManagerLSDB::Initialize () |
|
222 |
{ |
|
223 |
NS_DEBUG ("GlobalRouteManagerLSDB::Initialize ()"); |
|
224 |
||
225 |
LSDBMap_t::iterator i; |
|
226 |
for (i= m_database.begin (); i!= m_database.end (); i++) |
|
227 |
{ |
|
228 |
GlobalRouterLSA* temp = i->second; |
|
229 |
temp->SetStatus (GlobalRouterLSA::LSA_SPF_NOT_EXPLORED); |
|
230 |
} |
|
231 |
} |
|
232 |
||
233 |
void |
|
234 |
GlobalRouteManagerLSDB::Insert (Ipv4Address addr, GlobalRouterLSA* lsa) |
|
235 |
{ |
|
236 |
NS_DEBUG ("GlobalRouteManagerLSDB::Insert ()"); |
|
237 |
m_database.insert (LSDBPair_t (addr, lsa)); |
|
238 |
} |
|
239 |
||
240 |
GlobalRouterLSA* |
|
241 |
GlobalRouteManagerLSDB::GetLSA (Ipv4Address addr) const |
|
242 |
{ |
|
243 |
NS_DEBUG ("GlobalRouteManagerLSDB::GetLSA ()"); |
|
244 |
// |
|
245 |
// Look up an LSA by its address. |
|
246 |
// |
|
247 |
LSDBMap_t::const_iterator i; |
|
248 |
for (i= m_database.begin (); i!= m_database.end (); i++) |
|
249 |
{ |
|
250 |
if (i->first == addr) |
|
251 |
{ |
|
252 |
return i->second; |
|
253 |
} |
|
254 |
} |
|
255 |
return 0; |
|
256 |
} |
|
257 |
||
258 |
// --------------------------------------------------------------------------- |
|
259 |
// |
|
260 |
// GlobalRouteManagerImpl Implementation |
|
261 |
// |
|
262 |
// --------------------------------------------------------------------------- |
|
263 |
||
264 |
GlobalRouteManagerImpl::GlobalRouteManagerImpl () |
|
265 |
: |
|
266 |
m_spfroot (0) |
|
267 |
{ |
|
268 |
NS_DEBUG ("GlobalRouteManagerImpl::GlobalRoutemanagerImpl ()"); |
|
269 |
m_lsdb = new GlobalRouteManagerLSDB (); |
|
270 |
} |
|
271 |
||
272 |
GlobalRouteManagerImpl::~GlobalRouteManagerImpl () |
|
273 |
{ |
|
274 |
NS_DEBUG ("GlobalRouteManagerImpl::~GlobalRouteManagerImpl ()"); |
|
275 |
||
276 |
if (m_lsdb) |
|
277 |
{ |
|
278 |
delete m_lsdb; |
|
279 |
} |
|
280 |
} |
|
281 |
||
282 |
void |
|
283 |
GlobalRouteManagerImpl::DebugUseLsdb (GlobalRouteManagerLSDB* lsdb) |
|
284 |
{ |
|
285 |
NS_DEBUG ("GlobalRouteManagerImpl::DebugUseLsdb ()"); |
|
286 |
||
287 |
if (m_lsdb) |
|
288 |
{ |
|
289 |
delete m_lsdb; |
|
290 |
} |
|
291 |
m_lsdb = lsdb; |
|
292 |
} |
|
293 |
||
294 |
// |
|
1113
5b63b39161e7
remove routing environment, move router interface creation to global-route-manager
Craig Dowell <craigdo@ee.washington.edu>
parents:
1111
diff
changeset
|
295 |
// In order to build the routing database, we need at least one of the nodes |
5b63b39161e7
remove routing environment, move router interface creation to global-route-manager
Craig Dowell <craigdo@ee.washington.edu>
parents:
1111
diff
changeset
|
296 |
// to participate as a router. Eventually we expect to provide a mechanism |
5b63b39161e7
remove routing environment, move router interface creation to global-route-manager
Craig Dowell <craigdo@ee.washington.edu>
parents:
1111
diff
changeset
|
297 |
// for selecting a subset of the nodes to participate; for now, we just make |
5b63b39161e7
remove routing environment, move router interface creation to global-route-manager
Craig Dowell <craigdo@ee.washington.edu>
parents:
1111
diff
changeset
|
298 |
// all nodes routers. We do this by walking the list of nodes in the system |
5b63b39161e7
remove routing environment, move router interface creation to global-route-manager
Craig Dowell <craigdo@ee.washington.edu>
parents:
1111
diff
changeset
|
299 |
// and aggregating a Global Router Interface to each of the nodes. |
5b63b39161e7
remove routing environment, move router interface creation to global-route-manager
Craig Dowell <craigdo@ee.washington.edu>
parents:
1111
diff
changeset
|
300 |
// |
5b63b39161e7
remove routing environment, move router interface creation to global-route-manager
Craig Dowell <craigdo@ee.washington.edu>
parents:
1111
diff
changeset
|
301 |
void |
5b63b39161e7
remove routing environment, move router interface creation to global-route-manager
Craig Dowell <craigdo@ee.washington.edu>
parents:
1111
diff
changeset
|
302 |
GlobalRouteManagerImpl::SelectRouterNodes () |
5b63b39161e7
remove routing environment, move router interface creation to global-route-manager
Craig Dowell <craigdo@ee.washington.edu>
parents:
1111
diff
changeset
|
303 |
{ |
5b63b39161e7
remove routing environment, move router interface creation to global-route-manager
Craig Dowell <craigdo@ee.washington.edu>
parents:
1111
diff
changeset
|
304 |
NS_DEBUG ("GlobalRouteManagerImpl::SelectRouterNodes ()"); |
5b63b39161e7
remove routing environment, move router interface creation to global-route-manager
Craig Dowell <craigdo@ee.washington.edu>
parents:
1111
diff
changeset
|
305 |
|
5b63b39161e7
remove routing environment, move router interface creation to global-route-manager
Craig Dowell <craigdo@ee.washington.edu>
parents:
1111
diff
changeset
|
306 |
typedef std::vector < Ptr<Node> >::iterator Iterator; |
5b63b39161e7
remove routing environment, move router interface creation to global-route-manager
Craig Dowell <craigdo@ee.washington.edu>
parents:
1111
diff
changeset
|
307 |
for (Iterator i = NodeList::Begin (); i != NodeList::End (); i++) |
5b63b39161e7
remove routing environment, move router interface creation to global-route-manager
Craig Dowell <craigdo@ee.washington.edu>
parents:
1111
diff
changeset
|
308 |
{ |
5b63b39161e7
remove routing environment, move router interface creation to global-route-manager
Craig Dowell <craigdo@ee.washington.edu>
parents:
1111
diff
changeset
|
309 |
Ptr<Node> node = *i; |
5b63b39161e7
remove routing environment, move router interface creation to global-route-manager
Craig Dowell <craigdo@ee.washington.edu>
parents:
1111
diff
changeset
|
310 |
NS_DEBUG ("GlobalRouteManagerImpl::SelectRouterNodes (): " |
5b63b39161e7
remove routing environment, move router interface creation to global-route-manager
Craig Dowell <craigdo@ee.washington.edu>
parents:
1111
diff
changeset
|
311 |
"Adding GlobalRouter interface to node " << |
5b63b39161e7
remove routing environment, move router interface creation to global-route-manager
Craig Dowell <craigdo@ee.washington.edu>
parents:
1111
diff
changeset
|
312 |
node->GetId ()); |
5b63b39161e7
remove routing environment, move router interface creation to global-route-manager
Craig Dowell <craigdo@ee.washington.edu>
parents:
1111
diff
changeset
|
313 |
|
5b63b39161e7
remove routing environment, move router interface creation to global-route-manager
Craig Dowell <craigdo@ee.washington.edu>
parents:
1111
diff
changeset
|
314 |
Ptr<GlobalRouter> globalRouter = Create<GlobalRouter> (node); |
5b63b39161e7
remove routing environment, move router interface creation to global-route-manager
Craig Dowell <craigdo@ee.washington.edu>
parents:
1111
diff
changeset
|
315 |
node->AddInterface (globalRouter); |
5b63b39161e7
remove routing environment, move router interface creation to global-route-manager
Craig Dowell <craigdo@ee.washington.edu>
parents:
1111
diff
changeset
|
316 |
} |
5b63b39161e7
remove routing environment, move router interface creation to global-route-manager
Craig Dowell <craigdo@ee.washington.edu>
parents:
1111
diff
changeset
|
317 |
} |
5b63b39161e7
remove routing environment, move router interface creation to global-route-manager
Craig Dowell <craigdo@ee.washington.edu>
parents:
1111
diff
changeset
|
318 |
|
5b63b39161e7
remove routing environment, move router interface creation to global-route-manager
Craig Dowell <craigdo@ee.washington.edu>
parents:
1111
diff
changeset
|
319 |
// |
1111 | 320 |
// In order to build the routing database, we need to walk the list of nodes |
321 |
// in the system and look for those that support the GlobalRouter interface. |
|
322 |
// These routers will export a number of Link State Advertisements (LSAs) |
|
323 |
// that describe the links and networks that are "adjacent" (i.e., that are |
|
324 |
// on the other side of a point-to-point link). We take these LSAs and put |
|
325 |
// add them to the Link State DataBase (LSDB) from which the routes will |
|
326 |
// ultimately be computed. |
|
327 |
// |
|
328 |
void |
|
329 |
GlobalRouteManagerImpl::BuildGlobalRoutingDatabase () |
|
330 |
{ |
|
331 |
NS_DEBUG ("GlobalRouteManagerImpl::BuildGlobalRoutingDatabase()"); |
|
332 |
// |
|
333 |
// Walk the list of nodes looking for the GlobalRouter Interface. |
|
334 |
// |
|
335 |
typedef std::vector < Ptr<Node> >::iterator Iterator; |
|
336 |
for (Iterator i = NodeList::Begin (); i != NodeList::End (); i++) |
|
337 |
{ |
|
338 |
Ptr<Node> node = *i; |
|
339 |
||
340 |
Ptr<GlobalRouter> rtr = |
|
341 |
node->QueryInterface<GlobalRouter> (GlobalRouter::iid); |
|
342 |
// |
|
343 |
// Ignore nodes that aren't participating in routing. |
|
344 |
// |
|
345 |
if (!rtr) |
|
346 |
{ |
|
347 |
continue; |
|
348 |
} |
|
349 |
// |
|
350 |
// You must call DiscoverLSAs () before trying to use any routing info or to |
|
351 |
// update LSAs. DiscoverLSAs () drives the process of discovering routes in |
|
352 |
// the GlobalRouter. Afterward, you may use GetNumLSAs (), which is a very |
|
353 |
// computationally inexpensive call. If you call GetNumLSAs () before calling |
|
354 |
// DiscoverLSAs () will get zero as the number since no routes have been |
|
355 |
// found. |
|
356 |
// |
|
357 |
uint32_t numLSAs = rtr->DiscoverLSAs (); |
|
358 |
NS_DEBUG ("Discover LSAs: Found " << numLSAs << " LSAs"); |
|
359 |
||
360 |
for (uint32_t j = 0; j < numLSAs; ++j) |
|
361 |
{ |
|
362 |
GlobalRouterLSA* lsa = new GlobalRouterLSA (); |
|
363 |
// |
|
364 |
// This is the call to actually fetch a Link State Advertisement from the |
|
365 |
// router. |
|
366 |
// |
|
367 |
rtr->GetLSA (j, *lsa); |
|
368 |
NS_DEBUG ("LSA " << j); |
|
369 |
NS_DEBUG (*lsa); |
|
370 |
// |
|
371 |
// Write the newly discovered link state advertisement to the database. |
|
372 |
// |
|
373 |
m_lsdb->Insert (lsa->GetLinkStateId (), lsa); |
|
374 |
} |
|
375 |
} |
|
376 |
} |
|
377 |
||
378 |
// |
|
379 |
// For each node that is a global router (which is determined by the presence |
|
380 |
// of an aggregated GlobalRouter interface), run the Dijkstra SPF calculation |
|
381 |
// on the database rooted at that router, and populate the node forwarding |
|
382 |
// tables. |
|
383 |
// |
|
384 |
// This function parallels RFC2328, Section 16.1.1, and quagga ospfd |
|
385 |
// |
|
386 |
// This calculation yields the set of intra-area routes associated |
|
387 |
// with an area (called hereafter Area A). A router calculates the |
|
388 |
// shortest-path tree using itself as the root. The formation |
|
389 |
// of the shortest path tree is done here in two stages. In the |
|
390 |
// first stage, only links between routers and transit networks are |
|
391 |
// considered. Using the Dijkstra algorithm, a tree is formed from |
|
392 |
// this subset of the link state database. In the second stage, |
|
393 |
// leaves are added to the tree by considering the links to stub |
|
394 |
// networks. |
|
395 |
// |
|
396 |
// The area's link state database is represented as a directed graph. |
|
397 |
// The graph's vertices are routers, transit networks and stub networks. |
|
398 |
// |
|
399 |
// The first stage of the procedure (i.e., the Dijkstra algorithm) |
|
400 |
// can now be summarized as follows. At each iteration of the |
|
401 |
// algorithm, there is a list of candidate vertices. Paths from |
|
402 |
// the root to these vertices have been found, but not necessarily |
|
403 |
// the shortest ones. However, the paths to the candidate vertex |
|
404 |
// that is closest to the root are guaranteed to be shortest; this |
|
405 |
// vertex is added to the shortest-path tree, removed from the |
|
406 |
// candidate list, and its adjacent vertices are examined for |
|
407 |
// possible addition to/modification of the candidate list. The |
|
408 |
// algorithm then iterates again. It terminates when the candidate |
|
409 |
// list becomes empty. |
|
410 |
// |
|
411 |
void |
|
412 |
GlobalRouteManagerImpl::InitializeRoutes () |
|
413 |
{ |
|
414 |
NS_DEBUG ("GlobalRouteManagerImpl::InitializeRoutes ()"); |
|
415 |
// |
|
416 |
// Walk the list of nodes in the system. |
|
417 |
// |
|
418 |
typedef std::vector < Ptr<Node> >::iterator Iterator; |
|
419 |
for (Iterator i = NodeList::Begin (); i != NodeList::End (); i++) |
|
420 |
{ |
|
421 |
Ptr<Node> node = *i; |
|
422 |
// |
|
423 |
// Look for the GlobalRouter interface that indicates that the node is |
|
424 |
// participating in routing. |
|
425 |
// |
|
426 |
Ptr<GlobalRouter> rtr = |
|
427 |
node->QueryInterface<GlobalRouter> (GlobalRouter::iid); |
|
428 |
// |
|
429 |
// if the node has a global router interface, then run the global routing |
|
430 |
// algorithms. |
|
431 |
// |
|
432 |
if (rtr && rtr->GetNumLSAs () ) |
|
433 |
{ |
|
434 |
SPFCalculate (rtr->GetRouterId ()); |
|
435 |
} |
|
436 |
} |
|
437 |
} |
|
438 |
||
439 |
// |
|
440 |
// This method is derived from quagga ospf_spf_next (). See RFC2328 Section |
|
441 |
// 16.1 (2) for further details. |
|
442 |
// |
|
443 |
// We're passed a parameter <v> that is a vertex which is already in the SPF |
|
444 |
// tree. A vertex represents a router node. We also get a reference to the |
|
445 |
// SPF candidate queue, which is a priority queue containing the shortest paths |
|
446 |
// to the networks we know about. |
|
447 |
// |
|
448 |
// We examine the links in v's LSA and update the list of candidates with any |
|
449 |
// vertices not already on the list. If a lower-cost path is found to a |
|
450 |
// vertex already on the candidate list, store the new (lower) cost. |
|
451 |
// |
|
452 |
void |
|
453 |
GlobalRouteManagerImpl::SPFNext (SPFVertex* v, CandidateQueue& candidate) |
|
454 |
{ |
|
455 |
SPFVertex* w = 0; |
|
456 |
GlobalRouterLSA* w_lsa = 0; |
|
457 |
uint32_t distance = 0; |
|
458 |
||
459 |
NS_DEBUG ("GlobalRouteManagerImpl::SPFNext ()"); |
|
460 |
// |
|
461 |
// Always true for now, since all our LSAs are RouterLSAs. |
|
462 |
// |
|
463 |
if (v->GetVertexType () == SPFVertex::VertexRouter) |
|
464 |
{ |
|
465 |
if (true) |
|
466 |
{ |
|
467 |
NS_DEBUG ("SPFNext: Examining " << v->GetVertexId () << "'s " << |
|
468 |
v->GetLSA ()->GetNLinkRecords () << " link records"); |
|
469 |
// |
|
470 |
// Walk the list of link records in the link state advertisement associated |
|
471 |
// with the "current" router (represented by vertex <v>). |
|
472 |
// |
|
473 |
for (uint32_t i = 0; i < v->GetLSA ()->GetNLinkRecords (); ++i) |
|
474 |
{ |
|
475 |
// |
|
476 |
// (a) If this is a link to a stub network, examine the next link in V's LSA. |
|
477 |
// Links to stub networks will be considered in the second stage of the |
|
478 |
// shortest path calculation. |
|
479 |
// |
|
480 |
GlobalRouterLinkRecord *l = v->GetLSA ()->GetLinkRecord (i); |
|
481 |
if (l->GetLinkType () == GlobalRouterLinkRecord::StubNetwork) |
|
482 |
{ |
|
483 |
NS_DEBUG ("SPFNext: Found a Stub record to " << |
|
484 |
l->GetLinkId ()); |
|
485 |
continue; |
|
486 |
} |
|
487 |
// |
|
488 |
// (b) Otherwise, W is a transit vertex (router or transit network). Look up |
|
489 |
// the vertex W's LSA (router-LSA or network-LSA) in Area A's link state |
|
490 |
// database. |
|
491 |
// |
|
492 |
if (l->GetLinkType () == GlobalRouterLinkRecord::PointToPoint) |
|
493 |
{ |
|
494 |
// |
|
495 |
// Lookup the link state advertisement of the new link -- we call it <w> in |
|
496 |
// the link state database. |
|
497 |
// |
|
498 |
w_lsa = m_lsdb->GetLSA (l->GetLinkId ()); |
|
499 |
NS_ASSERT (w_lsa); |
|
500 |
NS_DEBUG ("SPFNext: Found a P2P record from " << |
|
501 |
v->GetVertexId () << " to " << w_lsa->GetLinkStateId ()); |
|
502 |
// |
|
503 |
// (c) If vertex W is already on the shortest-path tree, examine the next |
|
504 |
// link in the LSA. |
|
505 |
// |
|
506 |
// If the link is to a router that is already in the shortest path first tree |
|
507 |
// then we have it covered -- ignore it. |
|
508 |
// |
|
509 |
if (w_lsa->GetStatus () == |
|
510 |
GlobalRouterLSA::LSA_SPF_IN_SPFTREE) |
|
511 |
{ |
|
512 |
NS_DEBUG ("SPFNext: Skipping-> LSA "<< |
|
513 |
w_lsa->GetLinkStateId () << " already in SPF tree"); |
|
514 |
continue; |
|
515 |
} |
|
516 |
// |
|
517 |
// The link is to a router we haven't dealt with yet. |
|
518 |
// |
|
519 |
// (d) Calculate the link state cost D of the resulting path from the root to |
|
520 |
// vertex W. D is equal to the sum of the link state cost of the (already |
|
521 |
// calculated) shortest path to vertex V and the advertised cost of the link |
|
522 |
// between vertices V and W. |
|
523 |
// |
|
524 |
distance = v->GetDistanceFromRoot () + l->GetMetric (); |
|
525 |
||
526 |
NS_DEBUG ("SPFNext: Considering w_lsa " << |
|
527 |
w_lsa->GetLinkStateId ()); |
|
528 |
||
529 |
if (w_lsa->GetStatus () == |
|
530 |
GlobalRouterLSA::LSA_SPF_NOT_EXPLORED) |
|
531 |
{ |
|
532 |
// |
|
533 |
// If we haven't yet considered the link represented by <w> we have to create |
|
534 |
// a new SPFVertex to represent it. |
|
535 |
// |
|
536 |
w = new SPFVertex (w_lsa); |
|
537 |
// |
|
538 |
// We need to figure out how to actually get to the new router represented |
|
539 |
// by <w>. This will (among other things) find the next hop address to send |
|
540 |
// packets destined for this network to, and also find the outbound interface |
|
541 |
// used to forward the packets. |
|
542 |
// |
|
543 |
if (SPFNexthopCalculation (v, w, l, distance)) |
|
544 |
{ |
|
545 |
w_lsa->SetStatus ( |
|
546 |
GlobalRouterLSA::LSA_SPF_CANDIDATE); |
|
547 |
// |
|
548 |
// Push this new vertex onto the priority queue (ordered by distance from the |
|
549 |
// root node). |
|
550 |
// |
|
551 |
candidate.Push (w); |
|
552 |
NS_DEBUG ("SPFNext: Pushing " << |
|
553 |
w->GetVertexId () << ", parent vertexId: " << |
|
554 |
v->GetVertexId ()); |
|
555 |
} |
|
556 |
} |
|
557 |
} else if (w_lsa->GetStatus () == |
|
558 |
GlobalRouterLSA::LSA_SPF_CANDIDATE) |
|
559 |
{ |
|
560 |
// |
|
561 |
// We have already considered the link represented by <w>. What wse have to |
|
562 |
// do now is to decide if this new router represents a route with a shorter |
|
563 |
// distance metric. |
|
564 |
// |
|
565 |
// So, locate the vertex in the candidate queue and take a look at the |
|
566 |
// distance. |
|
567 |
w = candidate.Find (w_lsa->GetLinkStateId ()); |
|
568 |
if (w->GetDistanceFromRoot () < distance) |
|
569 |
{ |
|
570 |
// |
|
571 |
// This is not a shorter path, so don't do anything. |
|
572 |
// |
|
573 |
continue; |
|
574 |
} |
|
575 |
else if (w->GetDistanceFromRoot () == distance) |
|
576 |
{ |
|
577 |
// |
|
578 |
// This path is one with an equal cost. Do nothing for now -- we're not doing |
|
579 |
// equal-cost multipath cases yet. |
|
580 |
// |
|
581 |
} |
|
582 |
else |
|
583 |
{ |
|
584 |
// |
|
585 |
// this path represents a new, lower-cost path to <w> (the vertex we found in |
|
586 |
// the current link record of the link state advertisement of the current root |
|
587 |
// (vertex <v>) |
|
588 |
// |
|
589 |
// N.B. the nexthop_calculation is conditional, if it finds a valid nexthop |
|
590 |
// it will call spf_add_parents, which will flush the old parents |
|
591 |
// |
|
592 |
if (SPFNexthopCalculation (v, w, l, distance)) |
|
593 |
{ |
|
594 |
// |
|
595 |
// If we've changed the cost to get to the vertex represented by <w>, we |
|
596 |
// must reorder the priority queue keyed to that cost. |
|
597 |
// |
|
598 |
candidate.Reorder (); |
|
599 |
} |
|
600 |
} |
|
601 |
} // point-to-point |
|
602 |
} // for loop |
|
603 |
} |
|
604 |
} |
|
605 |
} |
|
606 |
||
607 |
// |
|
608 |
// This method is derived from quagga ospf_next_hop_calculation() 16.1.1. |
|
609 |
// |
|
610 |
// Calculate the next hop IP address and the outgoing interface required to |
|
611 |
// get packets from the root through <v> (parent) to vertex <w> (destination), |
|
612 |
// over a given distance. |
|
613 |
// |
|
614 |
// For now, this is greatly simplified from the quagga code |
|
615 |
// |
|
616 |
int |
|
617 |
GlobalRouteManagerImpl::SPFNexthopCalculation ( |
|
618 |
SPFVertex* v, |
|
619 |
SPFVertex* w, |
|
620 |
GlobalRouterLinkRecord* l, |
|
621 |
uint32_t distance) |
|
622 |
{ |
|
623 |
NS_DEBUG ("GlobalRouteManagerImpl::SPFNexthopCalculation ()"); |
|
624 |
// |
|
625 |
// The vertex m_spfroot is a distinguished vertex representing the node at |
|
626 |
// the root of the calculations. That is, it is the node for which we are |
|
627 |
// calculating the routes. |
|
628 |
// |
|
629 |
// There are two distinct cases for calculating the next hop information. |
|
630 |
// First, if we're considering a hop from the root to an "adjacent" network |
|
631 |
// (one that is on the other side of a point-to-point link connected to the |
|
632 |
// root), then we need to store the information needed to forward down that |
|
633 |
// link. The second case is if the network is not directly adjacent. In that |
|
634 |
// case we need to use the forwarding information from the vertex on the path |
|
635 |
// to the destination that is directly adjacent [node 1] in both cases of the |
|
636 |
// diagram below. |
|
637 |
// |
|
638 |
// (1) [root] -> [point-to-point] -> [node 1] |
|
639 |
// (2) [root] -> [point-to-point] -> [node 1] -> [point-to-point] -> [node 2] |
|
640 |
// |
|
641 |
// We call the propagation of next hop information down vertices of a path |
|
642 |
// "inheriting" the next hop information. |
|
643 |
// |
|
644 |
// The point-to-point link information is only useful in this calculation when |
|
645 |
// we are examining the root node. |
|
646 |
// |
|
647 |
if (v == m_spfroot) |
|
648 |
{ |
|
649 |
// |
|
650 |
// In this case <v> is the root node, which means it is the starting point |
|
651 |
// for the packets forwarded by that node. This also means that the next hop |
|
652 |
// address of packets headed for some arbitrary off-network destination must |
|
653 |
// be the destination at the other end of one of the links off of the root |
|
654 |
// node if this root node is a router. We then need to see if this node <w> |
|
655 |
// is a router. |
|
656 |
// |
|
657 |
if (w->GetVertexType () == SPFVertex::VertexRouter) |
|
658 |
{ |
|
659 |
// |
|
660 |
// In the case of point-to-point links, the link data field (m_linkData) of a |
|
661 |
// Global Router Link Record contains the local IP address. If we look at the |
|
662 |
// link record describing the link from the perspecive of <w> (the remote |
|
663 |
// node from the viewpoint of <v>) back to the root node, we can discover the |
|
664 |
// IP address of the router to which <v> is adjacent. This is a distinguished |
|
665 |
// address -- the next hop address to get from <v> to <w> and all networks |
|
666 |
// accessed through that path. |
|
667 |
// |
|
668 |
// SPFGetNextLink () is a little odd. used in this way it is just going to |
|
669 |
// return the link record describing the link from <w> to <v>. Think of it as |
|
670 |
// SPFGetLink. |
|
671 |
// |
|
672 |
GlobalRouterLinkRecord *linkRemote = 0; |
|
673 |
linkRemote = SPFGetNextLink (w, v, linkRemote); |
|
674 |
// |
|
675 |
// At this point, <l> is the Global Router Link Record describing the point- |
|
676 |
// to point link from <v> to <w> from the perspective of <v>; and <linkRemote> |
|
677 |
// is the Global Router Link Record describing that same link from the |
|
678 |
// perspective of <w> (back to <v>). Now we can just copy the next hop |
|
679 |
// address from the m_linkData member variable. |
|
680 |
// |
|
681 |
// The next hop member variable we put in <w> has the sense "in order to get |
|
682 |
// from the root node to the host represented by vertex <w>, you have to send |
|
683 |
// the packet to the next hop address specified in w->m_nextHop. |
|
684 |
// |
|
685 |
w->SetNextHop(linkRemote->GetLinkData ()); |
|
686 |
// |
|
687 |
// Now find the outgoing interface corresponding to the point to point link |
|
688 |
// from the perspective of <v> -- remember that <l> is the link "from" |
|
689 |
// <v> "to" <w>. |
|
690 |
// |
|
691 |
w->SetOutgoingInterfaceId ( |
|
692 |
FindOutgoingInterfaceId (l->GetLinkData ())); |
|
693 |
||
694 |
NS_DEBUG ("SPFNexthopCalculation: Next hop from " << |
|
695 |
v->GetVertexId () << " to " << w->GetVertexId () << |
|
696 |
" goes through next hop " << w->GetNextHop () << |
|
697 |
" via outgoing interface " << w->GetOutgoingInterfaceId ()); |
|
698 |
} |
|
699 |
} |
|
700 |
else |
|
701 |
{ |
|
702 |
// |
|
703 |
// If we're calculating the next hop information from a node (v) that is |
|
704 |
// *not* the root, then we need to "inherit" the information needed to |
|
705 |
// forward the packet from the vertex closer to the root. That is, we'll |
|
706 |
// still send packets to the next hop address of the router adjacent to the |
|
707 |
// root on the path toward <w>. |
|
708 |
// |
|
709 |
// Above, when we were considering the root node, we calculated the next hop |
|
710 |
// address and outgoing interface required to get off of the root network. |
|
711 |
// At this point, we are further away from the root network along one of the |
|
712 |
// (shortest) paths. So the next hop and outoing interface remain the same |
|
713 |
// (are inherited). |
|
714 |
// |
|
715 |
w->SetNextHop (v->GetNextHop ()); |
|
716 |
w->SetOutgoingInterfaceId (v->GetOutgoingInterfaceId ()); |
|
717 |
} |
|
718 |
// |
|
719 |
// In all cases, we need valid values for the distance metric and a parent. |
|
720 |
// |
|
721 |
w->SetDistanceFromRoot (distance); |
|
722 |
w->SetParent (v); |
|
723 |
||
724 |
return 1; |
|
725 |
} |
|
726 |
||
727 |
// |
|
728 |
// This method is derived from quagga ospf_get_next_link () |
|
729 |
// |
|
730 |
// First search the Global Router Link Records of vertex <v> for one |
|
731 |
// representing a point-to point link to vertex <w>. |
|
732 |
// |
|
733 |
// What is done depends on prev_link. Contrary to appearances, prev_link just |
|
734 |
// acts as a flag here. If prev_link is NULL, we return the first Global |
|
735 |
// Router Link Record we find that describes a point-to-point link from <v> |
|
736 |
// to <w>. If prev_link is not NULL, we return a Global Router Link Record |
|
737 |
// representing a possible *second* link from <v> to <w>. |
|
738 |
// |
|
739 |
// BUGBUG FIXME: This seems to be a bug. Shouldn't this function look for |
|
740 |
// any link records after pre_link and not just after the first? |
|
741 |
// |
|
742 |
GlobalRouterLinkRecord* |
|
743 |
GlobalRouteManagerImpl::SPFGetNextLink ( |
|
744 |
SPFVertex* v, |
|
745 |
SPFVertex* w, |
|
746 |
GlobalRouterLinkRecord* prev_link) |
|
747 |
{ |
|
748 |
NS_DEBUG ("GlobalRouteManagerImpl::SPFGetNextLink ()"); |
|
749 |
||
750 |
bool skip = true; |
|
751 |
GlobalRouterLinkRecord* l; |
|
752 |
// |
|
753 |
// If prev_link is 0, we are really looking for the first link, not the next |
|
754 |
// link. |
|
755 |
// |
|
756 |
if (prev_link == 0) |
|
757 |
{ |
|
758 |
skip = false; |
|
759 |
} |
|
760 |
// |
|
761 |
// Iterate through the Global Router Link Records advertised by the vertex |
|
762 |
// <v> looking for records representing the point-to-point links off of this |
|
763 |
// vertex. |
|
764 |
// |
|
765 |
for (uint32_t i = 0; i < v->GetLSA ()->GetNLinkRecords (); ++i) |
|
766 |
{ |
|
767 |
l = v->GetLSA ()->GetLinkRecord (i); |
|
768 |
if (l->GetLinkType () != GlobalRouterLinkRecord::PointToPoint) |
|
769 |
{ |
|
770 |
continue; |
|
771 |
} |
|
772 |
// |
|
773 |
// The link ID of a link record representing a point-to-point link is set to |
|
774 |
// the router ID of the neighboring router -- the router to which the link |
|
775 |
// connects from the perspective of <v> in this case. The vertex ID is also |
|
776 |
// set to the router ID (using the link state advertisement of a router node). |
|
777 |
// We're just checking to see if the link <l> is actually the link from <v> to |
|
778 |
// <w>. |
|
779 |
// |
|
780 |
if (l->GetLinkId () == w->GetVertexId ()) { |
|
781 |
NS_DEBUG ("SPFGetNextLink: Found matching link l: linkId = " << |
|
782 |
l->GetLinkId () << " linkData = " << l->GetLinkData ()); |
|
783 |
// |
|
784 |
// If skip is false, don't (not too surprisingly) skip the link found -- it's |
|
785 |
// the one we're interested in. That's either because we didn't pass in a |
|
786 |
// previous link, and we're interested in the first one, or because we've |
|
787 |
// skipped a previous link and moved forward to the next (which is then the |
|
788 |
// one we want). |
|
789 |
// |
|
790 |
if (skip == false) |
|
791 |
{ |
|
792 |
NS_DEBUG ("SPFGetNextLink: Returning the found link"); |
|
793 |
return l; |
|
794 |
} |
|
795 |
else |
|
796 |
{ |
|
797 |
// |
|
798 |
// Skip is true and we've found a link from <v> to <w>. We want the next one. |
|
799 |
// Setting skip to false gets us the next point-to-point global router link |
|
800 |
// record in the LSA from <v>. |
|
801 |
// |
|
802 |
NS_DEBUG ("SPFGetNextLink: Skipping the found link"); |
|
803 |
skip = false; |
|
804 |
continue; |
|
805 |
} |
|
806 |
} |
|
807 |
} |
|
808 |
return 0; |
|
809 |
} |
|
810 |
||
811 |
// |
|
812 |
// Used for unit tests. |
|
813 |
// |
|
814 |
void |
|
815 |
GlobalRouteManagerImpl::DebugSPFCalculate (Ipv4Address root) |
|
816 |
{ |
|
817 |
NS_DEBUG ("GlobalRouteManagerImpl::DebugSPFCalculate ()"); |
|
818 |
SPFCalculate (root); |
|
819 |
} |
|
820 |
||
821 |
// quagga ospf_spf_calculate |
|
822 |
void |
|
823 |
GlobalRouteManagerImpl::SPFCalculate (Ipv4Address root) |
|
824 |
{ |
|
825 |
NS_DEBUG ("GlobalRouteManagerImpl::SPFCalculate (): " |
|
826 |
"root = " << root); |
|
827 |
||
828 |
SPFVertex *v; |
|
829 |
// |
|
830 |
// Initialize the Link State Database. |
|
831 |
// |
|
832 |
m_lsdb->Initialize (); |
|
833 |
// |
|
834 |
// The candidate queue is a priority queue of SPFVertex objects, with the top |
|
835 |
// of the queue being the closest vertex in terms of distance from the root |
|
836 |
// of the tree. Initially, this queue is empty. |
|
837 |
// |
|
838 |
CandidateQueue candidate; |
|
839 |
NS_ASSERT(candidate.Size () == 0); |
|
840 |
// |
|
841 |
// Initialize the shortest-path tree to only contain the router doing the |
|
842 |
// calculation. Each router (and corresponding network) is a vertex in the |
|
843 |
// shortest path first (SPF) tree. |
|
844 |
// |
|
845 |
v = new SPFVertex (m_lsdb->GetLSA (root)); |
|
846 |
// |
|
847 |
// This vertex is the root of the SPF tree and it is distance 0 from the root. |
|
848 |
// We also mark this vertex as being in the SPF tree. |
|
849 |
// |
|
850 |
m_spfroot= v; |
|
851 |
v->SetDistanceFromRoot (0); |
|
852 |
v->GetLSA ()->SetStatus (GlobalRouterLSA::LSA_SPF_IN_SPFTREE); |
|
853 |
||
854 |
for (;;) |
|
855 |
{ |
|
856 |
// |
|
857 |
// The operations we need to do are given in the OSPF RFC which we reference |
|
858 |
// as we go along. |
|
859 |
// |
|
860 |
// RFC2328 16.1. (2). |
|
861 |
// |
|
862 |
// We examine the Global Router Link Records in the Link State |
|
863 |
// Advertisements of the current vertex. If there are any point-to-point |
|
864 |
// links to unexplored adjacent vertices we add them to the tree and update |
|
865 |
// the distance and next hop information on how to get there. We also add |
|
866 |
// the new vertices to the candidate queue (the priority queue ordered by |
|
867 |
// shortest path). If the new vertices represent shorter paths, we use them |
|
868 |
// and update the path cost. |
|
869 |
// |
|
870 |
SPFNext (v, candidate); |
|
871 |
// |
|
872 |
// RFC2328 16.1. (3). |
|
873 |
// |
|
874 |
// If at this step the candidate list is empty, the shortest-path tree (of |
|
875 |
// transit vertices) has been completely built and this stage of the |
|
876 |
// procedure terminates. |
|
877 |
// |
|
878 |
if (candidate.Size () == 0) |
|
879 |
{ |
|
880 |
break; |
|
881 |
} |
|
882 |
// |
|
883 |
// Choose the vertex belonging to the candidate list that is closest to the |
|
884 |
// root, and add it to the shortest-path tree (removing it from the candidate |
|
885 |
// list in the process). |
|
886 |
// |
|
887 |
// Recall that in the previous step, we created SPFVertex structures for each |
|
888 |
// of the routers found in the Global Router Link Records and added tehm to |
|
889 |
// the candidate list. |
|
890 |
// |
|
891 |
v = candidate.Pop (); |
|
892 |
NS_DEBUG ("SPFCalculate: Popped vertex " << v->GetVertexId ()); |
|
893 |
// |
|
894 |
// Update the status field of the vertex to indicate that it is in the SPF |
|
895 |
// tree. |
|
896 |
// |
|
897 |
v->GetLSA ()->SetStatus (GlobalRouterLSA::LSA_SPF_IN_SPFTREE); |
|
898 |
// |
|
899 |
// The current vertex has a parent pointer. By calling this rather oddly |
|
900 |
// named method (blame quagga) we add the current vertex to the list of |
|
901 |
// children of that parent vertex. In the next hop calculation called during |
|
902 |
// SPFNext, the parent pointer was set but the vertex has been orphaned up |
|
903 |
// to now. |
|
904 |
// |
|
905 |
SPFVertexAddParent (v); |
|
906 |
// |
|
907 |
// Note that when there is a choice of vertices closest to the root, network |
|
908 |
// vertices must be chosen before router vertices in order to necessarily |
|
909 |
// find all equal-cost paths. We don't do this at this moment, we should add |
|
910 |
// the treatment above codes. -- kunihiro. |
|
911 |
// |
|
912 |
// RFC2328 16.1. (4). |
|
913 |
// |
|
914 |
// This is the method that actually adds the routes. It'll walk the list |
|
915 |
// of nodes in the system, looking for the node corresponding to the router |
|
916 |
// ID of the root of the tree -- that is the router we're building the routes |
|
917 |
// for. It looks for the Ipv4 interface of that node and remembers it. So |
|
918 |
// we are only actually adding routes to that one node at the root of the SPF |
|
919 |
// tree. |
|
920 |
// |
|
921 |
// We're going to pop of a pointer to every vertex in the tree except the |
|
922 |
// root in order of distance from the root. For each of the vertices, we call |
|
923 |
// SPFIntraAddRouter (). Down in SPFIntraAddRouter, we look at all of the |
|
924 |
// point-to-point Global Router Link Records (the links to nodes adjacent to |
|
925 |
// the node represented by the vertex). We add a route to the IP address |
|
926 |
// specified by the m_linkData field of each of those link records. This will |
|
927 |
// be the *local* IP address associated with the interface attached to the |
|
928 |
// link. We use the outbound interface and next hop information present in |
|
929 |
// the vertex <v> which have possibly been inherited from the root. |
|
930 |
// |
|
931 |
// To summarize, we're going to look at the node represented by <v> and loop |
|
932 |
// through its point-to-point links, adding a *host* route to the local IP |
|
933 |
// address (at the <v> side) for each of those links. |
|
934 |
// |
|
935 |
SPFIntraAddRouter (v); |
|
936 |
// |
|
937 |
// RFC2328 16.1. (5). |
|
938 |
// |
|
939 |
// Iterate the algorithm by returning to Step 2 until there are no more |
|
940 |
// candidate vertices. |
|
941 |
// |
|
942 |
} |
|
943 |
// |
|
944 |
// Second stage of SPF calculation procedure's |
|
945 |
// NOTYET: ospf_spf_process_stubs (area, area->spf, new_table); |
|
946 |
// |
|
947 |
// We're all done setting the routing information for the node at the root of |
|
948 |
// the SPF tree. Delete all of the vertices and corresponding resources. Go |
|
949 |
// possibly do it again for the next router. |
|
950 |
// |
|
951 |
delete m_spfroot; |
|
952 |
m_spfroot = 0; |
|
953 |
} |
|
954 |
||
955 |
// |
|
956 |
// XXX This should probably be a method on Ipv4 |
|
957 |
// |
|
958 |
// Return the interface index corresponding to a given IP address |
|
959 |
// |
|
960 |
uint32_t |
|
961 |
GlobalRouteManagerImpl::FindOutgoingInterfaceId (Ipv4Address a) |
|
962 |
{ |
|
963 |
// |
|
964 |
// We have an IP address <a> and a vertex ID of the root of the SPF tree. |
|
965 |
// The question is what interface index does this address correspond to. |
|
966 |
// The answer is a little complicated since we have to find a pointer to |
|
967 |
// the node corresponding to the vertex ID, find the Ipv4 interface on that |
|
968 |
// node in order to iterate the interfaces and find the one corresponding to |
|
969 |
// the address in question. |
|
970 |
// |
|
971 |
Ipv4Address routerId = m_spfroot->GetVertexId (); |
|
972 |
// |
|
973 |
// Walk the list of nodes in the system looking for the one corresponding to |
|
974 |
// the node at the root of the SPF tree. This is the node for which we are |
|
975 |
// building the routing table. |
|
976 |
// |
|
977 |
std::vector<Ptr<Node> >::iterator i = NodeList::Begin (); |
|
978 |
for (; i != NodeList::End (); i++) |
|
979 |
{ |
|
980 |
Ptr<Node> node = *i; |
|
981 |
||
982 |
Ptr<GlobalRouter> rtr = |
|
983 |
node->QueryInterface<GlobalRouter> (GlobalRouter::iid); |
|
984 |
// |
|
985 |
// If the node doesn't have a GlobalRouter interface it can't be the one |
|
986 |
// we're interested in. |
|
987 |
// |
|
988 |
if (rtr == 0) |
|
989 |
{ |
|
990 |
continue; |
|
991 |
} |
|
992 |
||
993 |
if (rtr->GetRouterId () == routerId) |
|
994 |
{ |
|
995 |
// |
|
996 |
// This is the node we're building the routing table for. We're going to need |
|
997 |
// the Ipv4 interface to look for the ipv4 interface index. Since this node |
|
998 |
// is participating in routing IP version 4 packets, it certainly must have |
|
999 |
// an Ipv4 interface. |
|
1000 |
// |
|
1001 |
Ptr<Ipv4> ipv4 = node->QueryInterface<Ipv4> (Ipv4::iid); |
|
1002 |
NS_ASSERT_MSG (ipv4, |
|
1003 |
"GlobalRouteManagerImpl::FindOutgoingInterfaceId (): " |
|
1004 |
"QI for <Ipv4> interface failed"); |
|
1005 |
// |
|
1006 |
// Look through the interfaces on this node for one that has the IP address |
|
1007 |
// we're looking for. If we find one, return the corresponding interface |
|
1008 |
// index. |
|
1009 |
// |
|
1010 |
for (uint32_t i = 0; i < ipv4->GetNInterfaces (); i++) |
|
1011 |
{ |
|
1012 |
if (ipv4->GetAddress (i) == a) |
|
1013 |
{ |
|
1014 |
NS_DEBUG ( |
|
1015 |
"GlobalRouteManagerImpl::FindOutgoingInterfaceId (): " |
|
1016 |
"Interface match for " << a); |
|
1017 |
return i; |
|
1018 |
} |
|
1019 |
} |
|
1020 |
} |
|
1021 |
} |
|
1022 |
// |
|
1023 |
// Couldn't find it. |
|
1024 |
// |
|
1025 |
return 0; |
|
1026 |
} |
|
1027 |
||
1028 |
// |
|
1029 |
// This method is derived from quagga ospf_intra_add_router () |
|
1030 |
// |
|
1031 |
// This is where we are actually going to add the host routes to the routing |
|
1032 |
// tables of the individual nodes. |
|
1033 |
// |
|
1034 |
// The vertex passed as a parameter has just been added to the SPF tree. |
|
1035 |
// This vertex must have a valid m_root_oid, corresponding to the outgoing |
|
1036 |
// interface on the root router of the tree that is the first hop on the path |
|
1037 |
// to the vertex. The vertex must also have a next hop address, corresponding |
|
1038 |
// to the next hop on the path to the vertex. The vertex has an m_lsa field |
|
1039 |
// that has some number of link records. For each point to point link record, |
|
1040 |
// the m_linkData is the local IP address of the link. This corresponds to |
|
1041 |
// a destination IP address, reachable from the root, to which we add a host |
|
1042 |
// route. |
|
1043 |
// |
|
1044 |
void |
|
1045 |
GlobalRouteManagerImpl::SPFIntraAddRouter (SPFVertex* v) |
|
1046 |
{ |
|
1047 |
NS_DEBUG ("GlobalRouteManagerImpl::SPFIntraAddRouter ()"); |
|
1048 |
||
1049 |
NS_ASSERT_MSG (m_spfroot, |
|
1050 |
"GlobalRouteManagerImpl::SPFIntraAddRouter (): Root pointer not set"); |
|
1051 |
// |
|
1052 |
// The root of the Shortest Path First tree is the router to which we are |
|
1053 |
// going to write the actual routing table entries. The vertex corresponding |
|
1054 |
// to this router has a vertex ID which is the router ID of that node. We're |
|
1055 |
// going to use this ID to discover which node it is that we're actually going |
|
1056 |
// to update. |
|
1057 |
// |
|
1058 |
Ipv4Address routerId = m_spfroot->GetVertexId (); |
|
1059 |
||
1060 |
NS_DEBUG ("GlobalRouteManagerImpl::SPFIntraAddRouter (): " |
|
1061 |
"Vertex ID = " << routerId); |
|
1062 |
// |
|
1063 |
// We need to walk the list of nodes looking for the one that has the router |
|
1064 |
// ID corresponding to the root vertex. This is the one we're going to write |
|
1065 |
// the routing information to. |
|
1066 |
// |
|
1067 |
std::vector<Ptr<Node> >::iterator i = NodeList::Begin (); |
|
1068 |
for (; i != NodeList::End (); i++) |
|
1069 |
{ |
|
1070 |
Ptr<Node> node = *i; |
|
1071 |
// |
|
1072 |
// The router ID is accessible through the GlobalRouter interface, so we need |
|
1073 |
// to QI for that interface. If there's no GlobalRouter interface, the node |
|
1074 |
// in question cannot be the router we want, so we continue. |
|
1075 |
// |
|
1076 |
Ptr<GlobalRouter> rtr = |
|
1077 |
node->QueryInterface<GlobalRouter> (GlobalRouter::iid); |
|
1078 |
||
1079 |
if (rtr == 0) |
|
1080 |
{ |
|
1081 |
NS_DEBUG ("GlobalRouteManagerImpl::SPFIntraAddRouter (): " |
|
1082 |
"No GlobalRouter interface on node " << node->GetId ()); |
|
1083 |
continue; |
|
1084 |
} |
|
1085 |
// |
|
1086 |
// If the router ID of the current node is equal to the router ID of the |
|
1087 |
// root of the SPF tree, then this node is the one for which we need to |
|
1088 |
// write the routing tables. |
|
1089 |
// |
|
1090 |
NS_DEBUG ("GlobalRouteManagerImpl::SPFIntraAddRouter (): " |
|
1091 |
"Considering router " << rtr->GetRouterId ()); |
|
1092 |
||
1093 |
if (rtr->GetRouterId () == routerId) |
|
1094 |
{ |
|
1095 |
NS_DEBUG ("GlobalRouteManagerImpl::SPFIntraAddRouter (): " |
|
1096 |
"setting routes for node " << node->GetId ()); |
|
1097 |
// |
|
1098 |
// Routing information is updated using the Ipv4 interface. We need to QI |
|
1099 |
// for that interface. If the node is acting as an IP version 4 router, it |
|
1100 |
// should absolutely have an Ipv4 interface. |
|
1101 |
// |
|
1102 |
Ptr<Ipv4> ipv4 = node->QueryInterface<Ipv4> (Ipv4::iid); |
|
1103 |
NS_ASSERT_MSG (ipv4, |
|
1104 |
"GlobalRouteManagerImpl::SPFIntraAddRouter (): " |
|
1105 |
"QI for <Ipv4> interface failed"); |
|
1106 |
// |
|
1107 |
// Get the Global Router Link State Advertisement from the vertex we're |
|
1108 |
// adding the routes to. The LSA will have a number of attached Global Router |
|
1109 |
// Link Records corresponding to links off of that vertex / node. We're going |
|
1110 |
// to be interested in the records corresponding to point-to-point links. |
|
1111 |
// |
|
1112 |
GlobalRouterLSA *lsa = v->GetLSA (); |
|
1113 |
NS_ASSERT_MSG (lsa, |
|
1114 |
"GlobalRouteManagerImpl::SPFIntraAddRouter (): " |
|
1115 |
"Expected valid LSA in SPFVertex* v"); |
|
1116 |
||
1117 |
uint32_t nLinkRecords = lsa->GetNLinkRecords (); |
|
1118 |
// |
|
1119 |
// Iterate through the link records on the vertex to which we're going to add |
|
1120 |
// routes. To make sure we're being clear, we're going to add routing table |
|
1121 |
// entries to the tables on the node corresping to the root of the SPF tree. |
|
1122 |
// These entries will have routes to the IP addresses we find from looking at |
|
1123 |
// the local side of the point-to-point links found on the node described by |
|
1124 |
// the vertex <v>. |
|
1125 |
// |
|
1126 |
for (uint32_t j = 0; j < nLinkRecords; j += 2) |
|
1127 |
{ |
|
1128 |
// |
|
1129 |
// We are only concerned about point-to-point links |
|
1130 |
// |
|
1131 |
GlobalRouterLinkRecord *lr = lsa->GetLinkRecord (j); |
|
1132 |
if (lr->GetLinkType () != GlobalRouterLinkRecord::PointToPoint) |
|
1133 |
{ |
|
1134 |
continue; |
|
1135 |
} |
|
1136 |
||
1137 |
NS_DEBUG ("GlobalRouteManagerImpl::SPFIntraAddRouter (): " |
|
1138 |
" Node " << node->GetId () << |
|
1139 |
" add route to " << lr->GetLinkData () << |
|
1140 |
" using next hop " << v->GetNextHop () << |
|
1141 |
" via interface " << v->GetOutgoingInterfaceId ()); |
|
1142 |
// |
|
1143 |
// Here's why we did all of that work. We're going to add a host route to the |
|
1144 |
// host address found in the m_linkData field of the point-to-point link |
|
1145 |
// record. In the case of a point-to-point link, this is the local IP address |
|
1146 |
// of the node connected to the link. Each of these point-to-point links |
|
1147 |
// will correspond to a local interface that has an IP address to which |
|
1148 |
// the node at the root of the SPF tree can send packets. The vertex <v> |
|
1149 |
// (corresponding to the node that has these links and interfaces) has |
|
1150 |
// an m_nextHop address precalculated for us that is the address to which the |
|
1151 |
// root node should send packets to be forwarded to these IP addresses. |
|
1152 |
// Similarly, the vertex <v> has an m_rootOif (outbound interface index) to |
|
1153 |
// which the packets should be send for forwarding. |
|
1154 |
// |
|
1155 |
ipv4->AddHostRouteTo (lr->GetLinkData (), v->GetNextHop (), |
|
1156 |
v->GetOutgoingInterfaceId ()); |
|
1157 |
} |
|
1158 |
// |
|
1159 |
// Done adding the routes for the selected node. |
|
1160 |
// |
|
1161 |
return; |
|
1162 |
} |
|
1163 |
} |
|
1164 |
} |
|
1165 |
||
1166 |
// Derived from quagga ospf_vertex_add_parents () |
|
1167 |
// |
|
1168 |
// This is a somewhat oddly named method (blame quagga). Although you might |
|
1169 |
// expect it to add a parent *to* something, it actually adds a vertex |
|
1170 |
// to the list of children *in* each of its parents. |
|
1171 |
// |
|
1172 |
// Given a pointer to a vertex, it links back to the vertex's parent that it |
|
1173 |
// already has set and adds itself to that vertex's list of children. |
|
1174 |
// |
|
1175 |
// For now, only one parent (not doing equal-cost multipath) |
|
1176 |
// |
|
1177 |
void |
|
1178 |
GlobalRouteManagerImpl::SPFVertexAddParent (SPFVertex* v) |
|
1179 |
{ |
|
1180 |
v->GetParent ()->AddChild (v); |
|
1181 |
} |
|
1182 |
||
1183 |
} // namespace ns3 |
|
1184 |
||
1185 |
#ifdef RUN_SELF_TESTS |
|
1186 |
||
1187 |
// --------------------------------------------------------------------------- |
|
1188 |
// |
|
1189 |
// Unit Tests |
|
1190 |
// |
|
1191 |
// --------------------------------------------------------------------------- |
|
1192 |
||
1193 |
#include "ns3/test.h" |
|
1194 |
||
1195 |
namespace ns3 { |
|
1196 |
||
1197 |
class GlobalRouterTestNode : public Node |
|
1198 |
{ |
|
1199 |
public: |
|
1200 |
GlobalRouterTestNode (); |
|
1201 |
||
1202 |
private: |
|
1203 |
virtual void DoAddDevice (Ptr<NetDevice> device) const {}; |
|
1204 |
virtual TraceResolver *DoCreateTraceResolver (TraceContext const &context); |
|
1205 |
}; |
|
1206 |
||
1207 |
GlobalRouterTestNode::GlobalRouterTestNode () |
|
1208 |
{ |
|
1209 |
// Ptr<Ipv4L3Protocol> ipv4 = Create<Ipv4L3Protocol> (this); |
|
1210 |
} |
|
1211 |
||
1212 |
TraceResolver* |
|
1213 |
GlobalRouterTestNode::DoCreateTraceResolver (TraceContext const &context) |
|
1214 |
{ |
|
1215 |
return 0; |
|
1216 |
} |
|
1217 |
||
1218 |
class GlobalRouteManagerImplTest : public Test { |
|
1219 |
public: |
|
1220 |
GlobalRouteManagerImplTest (); |
|
1221 |
virtual ~GlobalRouteManagerImplTest (); |
|
1222 |
virtual bool RunTests (void); |
|
1223 |
}; |
|
1224 |
||
1225 |
GlobalRouteManagerImplTest::GlobalRouteManagerImplTest () |
|
1226 |
: Test ("GlobalRouteManagerImpl") |
|
1227 |
{ |
|
1228 |
} |
|
1229 |
||
1230 |
GlobalRouteManagerImplTest::~GlobalRouteManagerImplTest () |
|
1231 |
{} |
|
1232 |
||
1233 |
bool |
|
1234 |
GlobalRouteManagerImplTest::RunTests (void) |
|
1235 |
{ |
|
1236 |
bool ok = true; |
|
1237 |
||
1238 |
CandidateQueue candidate; |
|
1239 |
||
1240 |
for (int i = 0; i < 100; ++i) |
|
1241 |
{ |
|
1242 |
SPFVertex *v = new SPFVertex; |
|
1243 |
v->SetDistanceFromRoot (rand () % 100); |
|
1244 |
candidate.Push (v); |
|
1245 |
} |
|
1246 |
||
1247 |
uint32_t lastDistance = 0; |
|
1248 |
||
1249 |
for (int i = 0; i < 100; ++i) |
|
1250 |
{ |
|
1251 |
SPFVertex *v = candidate.Pop (); |
|
1252 |
if (v->GetDistanceFromRoot () < lastDistance) |
|
1253 |
{ |
|
1254 |
ok = false; |
|
1255 |
} |
|
1256 |
lastDistance = v->GetDistanceFromRoot (); |
|
1257 |
delete v; |
|
1258 |
v = 0; |
|
1259 |
} |
|
1260 |
||
1261 |
// Build fake link state database; four routers (0-3), 3 point-to-point |
|
1262 |
// links |
|
1263 |
// |
|
1264 |
// n0 |
|
1265 |
// \ link 0 |
|
1266 |
// \ link 2 |
|
1267 |
// n2 -------------------------n3 |
|
1268 |
// / |
|
1269 |
// / link 1 |
|
1270 |
// n1 |
|
1271 |
// |
|
1272 |
// link0: 10.1.1.1/30, 10.1.1.2/30 |
|
1273 |
// link1: 10.1.2.1/30, 10.1.2.2/30 |
|
1274 |
// link2: 10.1.3.1/30, 10.1.3.2/30 |
|
1275 |
// |
|
1276 |
// Router 0 |
|
1277 |
GlobalRouterLinkRecord* lr0 = new GlobalRouterLinkRecord ( |
|
1278 |
GlobalRouterLinkRecord::PointToPoint, |
|
1279 |
"0.0.0.2", // router ID 0.0.0.2 |
|
1280 |
"10.1.1.1", // local ID |
|
1281 |
1); // metric |
|
1282 |
||
1283 |
GlobalRouterLinkRecord* lr1 = new GlobalRouterLinkRecord ( |
|
1284 |
GlobalRouterLinkRecord::StubNetwork, |
|
1285 |
"10.1.1.1", |
|
1286 |
"255.255.255.252", |
|
1287 |
1); |
|
1288 |
||
1289 |
GlobalRouterLSA* lsa0 = new GlobalRouterLSA (); |
|
1290 |
lsa0->SetLinkStateId ("0.0.0.0"); |
|
1291 |
lsa0->SetAdvertisingRouter ("0.0.0.0"); |
|
1292 |
lsa0->AddLinkRecord (lr0); |
|
1293 |
lsa0->AddLinkRecord (lr1); |
|
1294 |
||
1295 |
// Router 1 |
|
1296 |
GlobalRouterLinkRecord* lr2 = new GlobalRouterLinkRecord ( |
|
1297 |
GlobalRouterLinkRecord::PointToPoint, |
|
1298 |
"0.0.0.2", |
|
1299 |
"10.1.2.1", |
|
1300 |
1); |
|
1301 |
||
1302 |
GlobalRouterLinkRecord* lr3 = new GlobalRouterLinkRecord ( |
|
1303 |
GlobalRouterLinkRecord::StubNetwork, |
|
1304 |
"10.1.2.1", |
|
1305 |
"255.255.255.252", |
|
1306 |
1); |
|
1307 |
||
1308 |
GlobalRouterLSA* lsa1 = new GlobalRouterLSA (); |
|
1309 |
lsa1->SetLinkStateId ("0.0.0.1"); |
|
1310 |
lsa1->SetAdvertisingRouter ("0.0.0.1"); |
|
1311 |
lsa1->AddLinkRecord (lr2); |
|
1312 |
lsa1->AddLinkRecord (lr3); |
|
1313 |
||
1314 |
// Router 2 |
|
1315 |
GlobalRouterLinkRecord* lr4 = new GlobalRouterLinkRecord ( |
|
1316 |
GlobalRouterLinkRecord::PointToPoint, |
|
1317 |
"0.0.0.0", |
|
1318 |
"10.1.1.2", |
|
1319 |
1); |
|
1320 |
||
1321 |
GlobalRouterLinkRecord* lr5 = new GlobalRouterLinkRecord ( |
|
1322 |
GlobalRouterLinkRecord::StubNetwork, |
|
1323 |
"10.1.1.2", |
|
1324 |
"255.255.255.252", |
|
1325 |
1); |
|
1326 |
||
1327 |
GlobalRouterLinkRecord* lr6 = new GlobalRouterLinkRecord ( |
|
1328 |
GlobalRouterLinkRecord::PointToPoint, |
|
1329 |
"0.0.0.1", |
|
1330 |
"10.1.2.2", |
|
1331 |
1); |
|
1332 |
||
1333 |
GlobalRouterLinkRecord* lr7 = new GlobalRouterLinkRecord ( |
|
1334 |
GlobalRouterLinkRecord::StubNetwork, |
|
1335 |
"10.1.2.2", |
|
1336 |
"255.255.255.252", |
|
1337 |
1); |
|
1338 |
||
1339 |
GlobalRouterLinkRecord* lr8 = new GlobalRouterLinkRecord ( |
|
1340 |
GlobalRouterLinkRecord::PointToPoint, |
|
1341 |
"0.0.0.3", |
|
1342 |
"10.1.3.2", |
|
1343 |
1); |
|
1344 |
||
1345 |
GlobalRouterLinkRecord* lr9 = new GlobalRouterLinkRecord ( |
|
1346 |
GlobalRouterLinkRecord::StubNetwork, |
|
1347 |
"10.1.3.2", |
|
1348 |
"255.255.255.252", |
|
1349 |
1); |
|
1350 |
||
1351 |
GlobalRouterLSA* lsa2 = new GlobalRouterLSA (); |
|
1352 |
lsa2->SetLinkStateId ("0.0.0.2"); |
|
1353 |
lsa2->SetAdvertisingRouter ("0.0.0.2"); |
|
1354 |
lsa2->AddLinkRecord (lr4); |
|
1355 |
lsa2->AddLinkRecord (lr5); |
|
1356 |
lsa2->AddLinkRecord (lr6); |
|
1357 |
lsa2->AddLinkRecord (lr7); |
|
1358 |
lsa2->AddLinkRecord (lr8); |
|
1359 |
lsa2->AddLinkRecord (lr9); |
|
1360 |
||
1361 |
// Router 3 |
|
1362 |
GlobalRouterLinkRecord* lr10 = new GlobalRouterLinkRecord ( |
|
1363 |
GlobalRouterLinkRecord::PointToPoint, |
|
1364 |
"0.0.0.2", |
|
1365 |
"10.1.2.1", |
|
1366 |
1); |
|
1367 |
||
1368 |
GlobalRouterLinkRecord* lr11 = new GlobalRouterLinkRecord ( |
|
1369 |
GlobalRouterLinkRecord::StubNetwork, |
|
1370 |
"10.1.2.1", |
|
1371 |
"255.255.255.252", |
|
1372 |
1); |
|
1373 |
||
1374 |
GlobalRouterLSA* lsa3 = new GlobalRouterLSA (); |
|
1375 |
lsa3->SetLinkStateId ("0.0.0.3"); |
|
1376 |
lsa3->SetAdvertisingRouter ("0.0.0.3"); |
|
1377 |
lsa3->AddLinkRecord (lr10); |
|
1378 |
lsa3->AddLinkRecord (lr11); |
|
1379 |
||
1380 |
// Test the database |
|
1381 |
GlobalRouteManagerLSDB* srmlsdb = new GlobalRouteManagerLSDB (); |
|
1382 |
srmlsdb->Insert (lsa0->GetLinkStateId (), lsa0); |
|
1383 |
srmlsdb->Insert (lsa1->GetLinkStateId (), lsa1); |
|
1384 |
srmlsdb->Insert (lsa2->GetLinkStateId (), lsa2); |
|
1385 |
srmlsdb->Insert (lsa3->GetLinkStateId (), lsa3); |
|
1386 |
NS_ASSERT (lsa2 == srmlsdb->GetLSA (lsa2->GetLinkStateId ())); |
|
1387 |
||
1388 |
// next, calculate routes based on the manually created LSDB |
|
1389 |
GlobalRouteManagerImpl* srm = new GlobalRouteManagerImpl (); |
|
1390 |
srm->DebugUseLsdb (srmlsdb); // manually add in an LSDB |
|
1391 |
// Note-- this will succeed without any nodes in the topology |
|
1392 |
// because the NodeList is empty |
|
1393 |
srm->DebugSPFCalculate (lsa0->GetLinkStateId ()); // node n0 |
|
1394 |
||
1395 |
// This delete clears the srm, which deletes the LSDB, which clears |
|
1396 |
// all of the LSAs, which each destroys the attached LinkRecords. |
|
1397 |
delete srm; |
|
1398 |
||
1399 |
return ok; |
|
1400 |
} |
|
1401 |
||
1402 |
// Instantiate this class for the unit tests |
|
1403 |
// XXX here we should do some verification of the routes built |
|
1404 |
static GlobalRouteManagerImplTest g_globalRouteManagerTest; |
|
1405 |
||
1406 |
} // namespace ns3 |
|
1407 |
||
1408 |
#endif |